# How to find circular objects in an image?

How could I use morphological processing to find circular objects in an image? More specifically, I need to detect road signs.

I need to get the "80 speed limit" sign from this image:

-
Welcome to Mathematica.SE, Elfet! It would help if you described what you have tried so far and where you got stuck. – Verbeia Oct 8 '12 at 10:47
This blog post might be useful. – VLC Oct 8 '12 at 11:06
Also this one – cormullion Oct 8 '12 at 12:01
Because the road signs will rarely be seen head-on, you probably want to generalize the question to detecting elliptical objects--perhaps just ellipses with near-vertical major axes. – whuber Oct 8 '12 at 16:13

The following method doesn't require parameters and discovers also oblique views.

obl[transit_Image] :=
(SelectComponents[
MorphologicalComponents[
DeleteSmallComponents@ChanVeseBinarize[#, "TargetColor" -> Red],
Method -> "ConvexHull"],
{"Count", "SemiAxes"}, Abs[Times @@ #2 Pi - #1] < #1/100 &]) & @ transit;

GraphicsGrid[{#, obl@# // Colorize, ImageMultiply[#, Image@Unitize@obl@#]} & /@
(Import /@ ("http://tinyurl.com/" <> # &/@ {"aw74tvc", "aycppg4", "9vnfrko", "bak4uzx"}))]


If you want to detect non-reddish edged ellipses just remove the "TargetColor" -> Red option.

-
 That's more like it...! Will it work with red birds on signs, though? :) – cormullion Oct 8 '12 at 16:19 @cormullion Your raven is one of my examples :D – belisarius Oct 8 '12 at 17:07 :) I meant that, if the bird was bright red (like a parrot) it might upset things. But your code is too clever to be fooled, I think! – cormullion Oct 8 '12 at 17:46 @cormullion Now we only have to find a photograph of a bright red bird sitting on a transit signal and test it :) – belisarius Oct 8 '12 at 17:58 will an amphibian do? – cormullion Oct 8 '12 at 18:59
show 8 more comments

Basically, you import the image:

i = Import["http://upload.wikimedia.org/wikipedia/commons/2/2c/Crow_on_the_sign_of_no_parking.jpg"]


Tidy it up:

mb = MorphologicalBinarize[i]


Isolate the areas of interest:

cn = ColorNegate[Closing [mb, 10]]


Use component analysis:

sc = SelectComponents[
cn, {"Eccentricity",
"Circularity"}, #1 < .5  && #2 < 0.8 &];
Colorize[sc]


Now use this information to process your original image...

ImageApply[# * 0.25 &, i, Masking -> ColorNegate[sc]]


Of course, I've cheated in this answer: to process arbitrary images - or to detect crows on road signs - is much much harder!

(I chose this image because, when I wrote this answer, you hadn't supplied your example.)

-
This is good! Try to do this also for image in my question. Is there are is way to use EdgeDetect? – Elfet Oct 8 '12 at 12:00
Have a go, show us what happens ... :) – cormullion Oct 8 '12 at 12:01
How now get using this information (Colorized) to select sign from original image? – Elfet Oct 8 '12 at 12:34
have a look at some of the relevant tutorials and videos: reference.wolfram.com/mathematica/tutorial/ImageProcessing.html should be built-in to the help; wolfram.com/broadcast/video.php?channel=97&video=839 is a good video introduction – cormullion Oct 8 '12 at 14:04