I got about 100 images of the sun and have to find the centre of the star in the images. I have binarized the images and used ComponentMeasurements
to find the Centroid
and the EquivalentDiskRadius
. So far so good, but I have noticed that in Mma's calculation the centroid appears to have the tendency to gravitate slightly towards the right bottom edge. I first thought that is due to the fact the the disk of the sun is a slight ellipse due to objective imperfection etc but I can replicate the effect with an image I create in Mma using the Disk
command.
disk = Rasterize[Graphics[{Disk[]}], ImageResolution -> 100];
ndisk = 1 /.
ComponentMeasurements[
ColorNegate@disk, {"Centroid", "EquivalentDiskRadius"}];
pdisk = 1 /.
ComponentMeasurements[disk, {"Centroid", "EquivalentDiskRadius"}];
Show[{disk,
Graphics[{{Red, Circle[ndisk[[1]], ndisk[[2]]]}, {Green,
Circle[pdisk[[1]], pdisk[[2]]]}}]}]
The result of the centroid calculation depends also on whether the disk is positive or negative. What am I missing here or how could I fit a circle to a binary image which contains a slightly distorted disk?
Edit: Based on some of the answers I simpified the input image by using an even and an odd sized array.
m = {{0, 0, 0, 0, 0, 0, 0},{0, 0, 0, 1, 0, 0, 0},{0, 0, 1, 1, 1, 0, 0},
{0, 1, 1, 1, 1, 1, 0},{0, 0, 1, 1, 1, 0, 0},{0, 0, 0, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0}};
k = {{0, 0, 1, 1, 0, 0},{0, 1, 1, 1, 1, 0},{1, 1, 1, 1, 1, 1},
{1, 1, 1, 1, 1, 1},{0, 1, 1, 1, 1, 0},{0, 0, 1, 1, 0, 0}};
circ = 1 /.
ComponentMeasurements[m, {"Centroid", "EquivalentDiskRadius"}]
(* {{3.5, 3.5}, 2.03421} *)
circ2 = 1 /.
ComponentMeasurements[k, {"Centroid", "EquivalentDiskRadius"}]
(* {{3., 3.}, 2.76395} *)
Show[{ArrayPlot[m],
Graphics[{Red, Thick, Circle[circ[[1]], circ[[2]]]}]}]
Show[{ArrayPlot[k],
Graphics[{Red, Thick, Circle[circ2[[1]], circ2[[2]]]}]}]
Gives the results below. ComponentMeasurements finds the right centre in both cases what is off is the Circle that I draw over it. Look at the sizes of the small triangle the circle cuts off the black squares.
Show[{disk,Graphics[{{Red,Circle[ndisk[[1]],ndisk[[2]]]},{Green,Circle[pdisk[[1]]+ndisk[[1,1]]-pdisk[[1,1]],pdisk[[2]]+ndisk[[1,2]]-pdisk[[1,2]]]}}]}]
$\endgroup$