Find Circle Parameter

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
gowribala
Posts: 14
Joined: 2013-06-03T02:43:37-07:00
Authentication code: 6789

Find Circle Parameter

Post by gowribala »

Hai...

I have a image with two circles.i need to find the center and radius of these two circles.

I tried hough transform but it wont produces exact output.

please guide me how to find center and radius of these two circles in my image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Find Circle Parameter

Post by fmw42 »

IM does not have Hough Transforms. If the two circles do not overlap and are on a constant background, you can crop them and trim the image to squares. The width or height of the square image will be the diameter. Take half of that and it will be the radius and center.

The only other way I know is by trial and error by guessing the radius and center and drawing a circle with -draw on your image and see how it aligns. Repeat with corrections until it matches. See http://www.imagemagick.org/Usage/draw/#circles
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Find Circle Parameter

Post by snibgo »

gowribala: Can you post a sample image? Put it somewhere like dropbox and post the URL here.
snibgo's IM pages: im.snibgo.com
gowribala
Posts: 14
Joined: 2013-06-03T02:43:37-07:00
Authentication code: 6789

Re: Find Circle Parameter

Post by gowribala »

Thanks for your reply...

But i need find the parameter of circles by program (c or c++)not manually.my project is purely based on image processing.

I need to find whether input image having circles or not ,if this having circles means have to find circles parameter.

Input image having two concentric circles (black color) on white background with some noise.

I have attached the image for your reference.

Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Find Circle Parameter

Post by snibgo »

The image contains no circles at all.

It does contain a number of marks that are reasonably close to being circles. You need to decide what determines "nearly a circle".

A possible approach would be to choose a possible centre and radius, draw a circle, and count how many corresponding pixels are black. If this is above a certain proportion, record this as "nearly a circle". Repeat for all possible centres and radii.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Find Circle Parameter

Post by fmw42 »

If you can locate the exact center of a circle in the image, you an do a -distort depolar to convert from cartesian to polar coordinates. The result will show a (nearly) horizontal line for the circle. Then a Straight Line Hough Transform should be able to find it. Note IM does not have Hough Transforms.

I would have thought that your hough transform for circle would have worked. Did you try thickening the lines with morphologic operators or perhaps invert the black <-> white using -negate.

see http://www.imagemagick.org/Usage/morphology/
gowribala
Posts: 14
Joined: 2013-06-03T02:43:37-07:00
Authentication code: 6789

Re: Find Circle Parameter

Post by gowribala »

Thanks for your reply...

I already used hough transform and another method which you mentioned above(assume set of centers and radius and draw) ,but it produced poor output.

My Region Of Interest(outer circle - inner circle) is located between these two circles.I want to separate that particular area alone,so only i need to calculate radius and center of these two circles.

please guide me how can i segment these image and get my Region Of Interest....
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Find Circle Parameter

Post by snibgo »

My suggestion is brute-force. It needs three nested loops: radius, x and y.

fmw42's suggestion is more intelligent, needing just two nested loops: x and y, but the processing is probably more complex. It could (if desired) find ellipses more easily than my suggestion.

Here's an example of my suggestion, for just one radius (35). I make circ.png with a white circle on a transparent background. Within the loop, I crop the source image to the same size and copy the red channel from circ.png. Where the circ.png circle intersects a black pixel from the source, the pixel becomes red. So I count the number of red pixels.

Windows script:

Code: Select all

setlocal enabledelayedexpansion

set SRC=2circles.jpg

set RAD=35

%IM%convert -size 71x71 xc:None ^
  -stroke White -fill None ^
  +antialias ^
  -draw "translate 35,35 circle 0,0 0,35" ^
  circ.png

del circFnd.txt

for /L %%x in (0,1,58) do (
for /L %%y in (0,1,58) do (
echo %%x, %%y, : >>circFnd.txt
%IM%convert %SRC% ^
  -crop 71x71+%%x+%%y +repage ^
  circ.png ^
  -compose CopyRed -composite ^
  txt:- | find /C "red" >>circFnd.txt
)
)

chBin /icircFnd.txt /f": \r\n" /ocf.csv
cSort /icf.csv /ocf2.csv /k2 /r
The best result is 64 red pixels, at location 53,43. Here it is:

Code: Select all

convert 2circles.jpg -crop 71x71+53+43 +repage circ.png -compose CopyRed -composite circ35fnd.png
Image
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Find Circle Parameter

Post by snibgo »

Or, very much easier and faster:

Code: Select all

D:\web\im>c:\im\ImageMagick-6.8.6-Q16fix\convert -size 71x71 xc:White -stroke Black -fill None +antialias -draw "translate 35,35 circle 0,0 0,35" circB.png

D:\web\im>c:\im\ImageMagick-6.8.6-Q16fix\compare -metric RMSE -subimage-search 2 circles.jpg circB.png NULL:
8902.08 (0.135837) @ 53,43
snibgo's IM pages: im.snibgo.com
Post Reply