I have the following two images:

Image 1:

enter image description here

Image 2:

enter image description here

How can I determine the center of the seen circle in both images?

I found different answers, which are similar (links) to my question, but due to the squared structure around the circle I am not able to find a solution for my images.


An alternative can be using ImageCorrelate. First I create the target object by cropping the original image - it doesn't have to be precise. Here I use ImageTake so that you can verify the results.

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

circ = ImageTake[img, {120, 210}, {70, 159}]

enter image description here

f=ImageCorrelate[img, circ, NormalizedSquaredEuclideanDistance];
pos = Binarize[f, 0.2];
cen=Mean[ImageValuePositions[pos, 0]]

Show[img, Epilog -> {Red, Point[cen]}]

enter image description here

So stepwise it is

enter image description here

and cen is the average of pos.

Using the same circ on the second image

img = Import["https://i.stack.imgur.com/p7r2V.png"]
f = ImageCorrelate[img, circ, NormalizedSquaredEuclideanDistance];
pos = Binarize[f, 0.2];
cen = Mean[ImageValuePositions[pos, 0]]

Show[img, Epilog -> {Red, Point[cen]}]

enter image description here

As you can see, it works although the full circle is not visible here.

| improve this answer | |
  • $\begingroup$ Fun,since we can get the circ,why not use Mean[{{120, 210}, {70, 159}}] to get the center? :) $\endgroup$ – yode Jul 4 '17 at 12:19
  • $\begingroup$ @yode, you need to do that only once from any image of a huge stack of images. And it does not have to be very accurate (I have a little extra space at bottom of circ). Now imagine you have 100 different images - you don't want to do this manual cropping 100 times ;) $\endgroup$ – Sumit Jul 4 '17 at 12:22
  • $\begingroup$ Thank you ... please see also my comment to yode concerning the center and the crossing point of the lines ... $\endgroup$ – lio Jul 5 '17 at 20:34
  • $\begingroup$ If you print f, you will see that it is actually the crossing point (the darkest point). pos gives you the center location - not the full ring. Since it is a bunch of closely placed points - I took the average. $\endgroup$ – Sumit Jul 6 '17 at 8:24

There maby be a better method can hough detect circle.Just some thinking strike to my mind.So I think the method based on GradientOrientationFilter also can serve this target.

Use the method from Simon Woods in this answer to get a ridge image

img = Import["https://i.stack.imgur.com/p7r2V.png"];
binImg = DeleteSmallComponents[
     MorphologicalBinarize[PeronaMalikFilter[img, 10], .2], 100]]];
ske = Thinning[
   ImageAdjust[binImg~Blur~12~Erosion~6~RidgeFilter~1], {.3, .5}]]

Mathematica graphics

Use the direction information to show the ske

orientation = GradientOrientationFilter[ske, 2] // ImageAdjust;
color = ImageAdjust[
  ImageMultiply[ColorCombine[{orientation, ske, ske}, "HSB"], ske]]

Mathematica graphics

Select that segment include most color

circle = Pruning[DeleteSmallComponents[
   ImageFilter[If[#[[2, 2]] != {0, 0, 0}, 
      If[Count[Catenate[#], #[[2, 2]]] >= 5, {0, 0, 
        0}, #[[2, 2]]], {0, 0, 0}] &, color, 2, Interleaving -> True]], 10]

Mathematica graphics

Let's show the center of the circle

pos = ImageValuePositions[Binarize[circle, 0], 1]
HighlightImage[img, {PointSize[.05],Point@Values[Most[FindFit[
      PadRight[#, 3] & /@ pos, (x - m)^2 + (y - n)^2 - r^2, {m, n, r}, {x, y}]]]}]

Mathematica graphics

By the same code also can be applied to another image and will get a perfect result. Mathematica graphics

| improve this answer | |
  • $\begingroup$ great ... thank you $\endgroup$ – lio Jul 4 '17 at 11:26
  • $\begingroup$ @lio See the update.I have finished it. :) $\endgroup$ – yode Jul 4 '17 at 13:14
  • $\begingroup$ Thanks for your help .. instead of selecting the circle it would be interesting to compare with the crossing point of the vertical and horizontal line which are going through the circle and also defining its center. May be this is more accurate. $\endgroup$ – lio Jul 5 '17 at 20:30

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.