Using this code a third "spurious" disk is detected.

fig = Rasterize[Graphics[{Black, Rectangle[{0, 0}, {1, 1}], White,
     Disk[{0.5, 0.5}, 0.1], Disk[{0.75, 0.75}, 0.05]}],
   ImageResolution -> 25];
data = ComponentMeasurements[fig, {"Centroid", "EquivalentDiskRadius"}]
 Graphics[{Red, Circle[#[[1]], #[[2]]]}] & /@ data[[;; , 2]]

Quite weird since it arises from a completely black region.

Any comments?

enter image description here

  • $\begingroup$ Interestingly, I get a slightly different result (OS X): i.sstatic.net/5Lb8L.png I see this in all versions since 9.0.1. $\endgroup$
    – Szabolcs
    Commented Nov 18, 2015 at 13:11
  • $\begingroup$ I am using version 10.1. If you go for ellipsis detection it is not giving that. $\endgroup$
    – Fabio
    Commented Nov 18, 2015 at 13:13

1 Answer 1


This happens because:

  • The region is not completely black. It has a white margin.

  • As I understand, EquivalentDiskRadius does not detect a disk. It takes every component, regardless of shape, and constructs a disk of the same area.

The third circle is in the middle of the white image border, but it is much smaller than it as it must match the area of white pixels.

Why is there a white border?

First, notice that fig is not an Image. It is a Graphics. When you apply an image processing function, it automatically gets converted to an Image. Let us avoid this and create the Image directly using

Rasterize[..., "Image"]

Mathematica graphics

It's easier to see if we invert the image, but you can also see if if you just click it in the notebook and observe the orange border.

What can we do to fix this?

First of all, to set the background of a Graphics, use the Background option instead of placing a rectangle in the background. Graphics have padding and a margin too.

This is what we get if we try it:

Mathematica graphics

There is still a white line at the bottom. This is likely due to rounding things to integer pixel dimensions. The white comes from the DefaultStyleDefinitions of the front end. To fix this, we must set the rasterization background, not the graphics background. If we do this, we might as well remove Background from within Graphics. Now it's good:

Mathematica graphics

To sum up, we needed to:

  • Make sure that Rasterize creates an Image
  • Set the rasterization background (normally inherited determined by front end settings) to black.

Final code:

fig = Rasterize[
  Graphics[{White, Disk[{0.5, 0.5}, 0.1], Disk[{0.75, 0.75}, 0.05]}], 
  "Image", ImageResolution -> 25, Background -> Black]

data = ComponentMeasurements[fig, {"Centroid", "EquivalentDiskRadius"}]
Show[fig, Graphics[{Red, Circle[#[[1]], #[[2]]]}] & /@ data[[;; , 2]]]

Mathematica graphics

You might notice that above the red circles appear shifted relative to the white disks. This is just a rendering artefact again due to rounding to integer screen pixels. In reality there is no shift, this becomes clear when magnifying the figure.

  • $\begingroup$ Oh yeah, Thanks. $\endgroup$
    – Fabio
    Commented Nov 18, 2015 at 13:32
  • 2
    $\begingroup$ (+1) I have one correction: the Background is inherited not from the styles of the EvaluationNotebook[] but seemingly from the global DefaultStyleDefinitions accessible from Options[$FrontEndSession, DefaultStyleDefinitions]. $\endgroup$ Commented Nov 18, 2015 at 14:19
  • $\begingroup$ @AlexeyPopkov Thanks for the correction! $\endgroup$
    – Szabolcs
    Commented Nov 18, 2015 at 14:23
  • $\begingroup$ A truly outstanding answer. I enjoyed reading it and learned a few new things in the process.Thank you! (+1, obviously) $\endgroup$
    – MarcoB
    Commented Nov 18, 2015 at 14:43

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.