20
$\begingroup$

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 ComponentMeasurementsto 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?

Result of the above Show command

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.

ArrayPlot 1

ArrayPlot 2

$\endgroup$
2
  • $\begingroup$ Would a simple correction of the effect help? If yes, then you could try something like 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$
    – partial81
    Commented Jun 25, 2012 at 12:51
  • $\begingroup$ Thanks everyone for your valuable input. I am convinced that 'ComponentMeasurement' delivers the right result but the circle I draw to visually inspect the result is slightly off. This offset disapears when I enlarge the plot. $\endgroup$
    – Matariki
    Commented Jun 26, 2012 at 0:40

3 Answers 3

9
$\begingroup$

I think the shift of the circle with respect to the disk is at least partially due to antialiasing effects. Compare for example:

disk = Binarize@Rasterize[Graphics[Disk[]]]; 
ndisk = 1 /. ComponentMeasurements[ColorNegate@disk, {"Centroid", "EquivalentDiskRadius"}];

Show[{disk, Graphics[{{Red, Antialiasing -> True, Circle @@ ndisk}}]}]

Mathematica graphics

with

Show[{disk, Graphics[{{Red, Antialiasing -> False, Circle @@ ndisk}}]}]

Mathematica graphics

$\endgroup$
4
  • 4
    $\begingroup$ How come an option can be inserted in a list of directives and primitives? Is this some undocumented usage, or did I just miss something from the documentation? $\endgroup$ Commented Jun 25, 2012 at 13:53
  • 3
    $\begingroup$ @IstvánZachar I don't think it's documented. I came across this usage here $\endgroup$
    – Heike
    Commented Jun 25, 2012 at 13:58
  • $\begingroup$ @Heike Interesting effect. For some reason I didn't see the effect when I tried it. The binarized image of the sun has however no antialiasing. $\endgroup$
    – Matariki
    Commented Jun 25, 2012 at 20:54
  • $\begingroup$ @Matariki I was referring to the influence of antialiasing on the circle drawn on top of the binarized image, not of the image itself. $\endgroup$
    – Heike
    Commented Jun 25, 2012 at 23:02
7
$\begingroup$

Compare:

l = Rasterize[Show[Graphics[{Disk[{1, 1}, 10], Disk[{20, 20}, 10]}]]];
ComponentMeasurements[ColorNegate@l, {"Centroid", "EquivalentDiskRadius"}]
ComponentMeasurements[l, {"Centroid", "EquivalentDiskRadius"}]

enter image description here

ComponentMeasurements does not work for black objects ... they are not components ...

$\endgroup$
1
  • $\begingroup$ Yes, that's what I suspected when I tried it for various objects, however the documentation isn't mentioning this. It only hinds at it in its examples.So the algorithm object search needs the objects to be 0 to find them. Thanks. $\endgroup$
    – Matariki
    Commented Jun 25, 2012 at 18:34
4
$\begingroup$

I feel the doc page for ComponentMeasurements contains the solution:

Position, area, and length measurements are taken in the standard image coordinate system where position {0,0} corresponds to the bottom-left corner, x runs from 0 to width, and y runs from 0 to height.

You are counting whole pixels and ComponentMeasurements measures pixel positions. In this system, the center of the bottom left pixel is at {1/2,1/2}.

I have been looking somewhat closer to Heike's answer to bring out the differences caused by anti-aliasing better.

Without anti-aliasing:

disk = Rasterize[Graphics[{Antialiasing -> False, Disk[]}], ImageSize -> 61];
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]]]}}]}, 
 Method -> {"ShrinkWrap" -> True}, ImageSize -> 610]

Mathematica graphics

With anti-aliasing:

Mathematica graphics

Both circles seem to be good fits to the disks as they are rasterized, but clearly rasterizing and anti-aliasing both introduce some asymmetries.

$\endgroup$
1
  • $\begingroup$ I understand that but shouldn't I still expect the circle to be in the middle of the component when I plot it where ComponentMeasurements deems the Centroid to be? $\endgroup$
    – Matariki
    Commented Jun 25, 2012 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.