Here is an image with an inhomogeneous illuminated disk shaped object in the center.

enter image description here

It is illuminated from left. Therefore on the left it is brighter and at right it is dark.

By eye one can guess where the edges of the object are.

How would you detect this oject's center and radius?

What I tried is not very successfull:

img = Import["https://i.stack.imgur.com/RKFDd.png"];

binimg = DeleteSmallComponents[Binarize[img, 0.45], 70]

enter image description here

pts = Values@ComponentMeasurements[binImg, {"Centroid", "EquivalentDiskRadius"}]

{{{80.99, 106.566}, 18.2557}}

radius = pts[[1, 2]]


center = pts[[1, 1]]

{80.99, 106.566}

Show[img, Graphics[{Red, Point[center]}], Graphics[{Red, Circle[center, r]}]]

enter image description here

The center is too far left and the radius is too large.

Manually I would draw the following circle:

enter image description here


I don't think you can get a perfectly unbiased estimate of the center you're interested in. If you look at a 3d plot of the image data, it's clear that the "disk" isn't symmetric, and probably overexposed on the light side:

img = ColorConvert[Import["https://i.stack.imgur.com/RKFDd.png"], 
ListPlot3D[ImageData[img], PlotRange -> All]

enter image description here

I think the best (and simplest) thing to do would be to use a linear filter kernel with a similar shape, i.e. a Gaussian derivative kernel:

Image[Rescale@GaussianMatrix[25, {0, 1}]]

enter image description here

I would expect that filtering the image with a filter kernel that looks like this should give the highest value where the "bright sides" and the "dark sides" of the filter kernel and the object match:

match = ImageAdjust@ColorNegate@GaussianFilter[img, 25, {0, 1}]

enter image description here

the bright peak looks close to the position you're interested in:

center = PixelValuePositions[match, "Max"][[1]]
HighlightImage[img, Circle[center, 25]]

enter image description here

  • $\begingroup$ Perfect solution. Thank you very much for the answer. $\endgroup$
    – mrz
    Mar 13 '17 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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