# Finding center of circle in image

I have the following two images:

Image 1:

Image 2:

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.sstatic.net/AzFtr.png"];

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

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

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

So stepwise it is

and cen is the average of pos.

Using the same circ on the second image

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

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

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

• Fun,since we can get the circ,why not use Mean[{{120, 210}, {70, 159}}] to get the center? :)
– yode
Jul 4, 2017 at 12:19
• @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 ;) Jul 4, 2017 at 12:22
• Thank you ... please see also my comment to yode concerning the center and the crossing point of the lines ...
– lio
Jul 5, 2017 at 20:34
• 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. Jul 6, 2017 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.sstatic.net/p7r2V.png"];
binImg = DeleteSmallComponents[
ColorNegate[
DeleteSmallComponents[
MorphologicalBinarize[PeronaMalikFilter[img, 10], .2], 100]]];
ske = Thinning[
MorphologicalBinarize[

Use the direction information to show the ske

ImageMultiply[ColorCombine[{orientation, ske, ske}, "HSB"], ske]]

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]

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}]]]}]

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

• great ... thank you
– lio
Jul 4, 2017 at 11:26
• @lio See the update.I have finished it. :)
– yode
Jul 4, 2017 at 13:14
• 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.
– lio
Jul 5, 2017 at 20:30