Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
overlapping circles - find the form-faktor

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 9 Dec, 2010 10:04:04

Message: 1 of 12

Hello @ll,

i am stuck with a problem, where my mind keeps running in circles (no pun intended).

I have an image of circular objects and I am trying to find the "roundness" of each one, viz. the form faktor.

Everything would work perfectly fine (in accordance with the demo on "identifying round objects" http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipexroundness.html), if my objects weren´t so close that their boundaries overlap.

In order to calculate the form-faktor, i need to know the area and the circumference of my circle (that´s why overlapping circles give such a huge error). So as far as i can see, it is probably easiest to apply "bwboundaries(bw,'holes')" in the process and use this to calculate the perimeter and area. However, I need clearly separated "circles" in order to do this correctly.

I have uploaded the image, so that you can imagine more clearly my problem: http://s1195.photobucket.com/albums/aa382/rtaker/

My question is, how am I able to separate the circular objects in my image? I am pretty inexperienced in matlab so any code-examples would also be highly appreciated.

An idea that I had, was to use a sobel-filter and then apply the ellipse fit by Nikolai Chernov (see http://www.mathworks.com/matlabcentral/fileexchange/22683). However i am still having the problem that i am not able to segment my image properly so that i can apply the fit properly (and hence define clearly separated borders).

It would be great if anybody could help me out.
Thank you very much for your time.

Subject: overlapping circles - find the form-faktor

From: ImageAnalyst

Date: 9 Dec, 2010 11:39:16

Message: 2 of 12

One way is to use marker controlled watershed segmentation. There's a
demo for that in the help, or online at their website, or both.

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 9 Dec, 2010 15:17:05

Message: 3 of 12

thanks a lot for your suggestion.
I have gone through the tutorial and applied the steps to my image, but unfortunately the foreground-marker are way off target and therefore the whole segmentation is completely messed up again ... do you have any idea how to fix this?

PS: I uploaded the messy result again under http://s1195.photobucket.com/albums/aa382/rtaker/?action=view¤t=marker-controlled_watershed_segmentation.jpg .The white spots represent the "foreground objects" as mentioned in the tutorial

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 9 Dec, 2010 16:01:05

Message: 4 of 12

i have talked to a friend in the meantime and he suggested to me "region growing with shape prior "

what do you guys think? have you ever tried it?

best regards

Subject: overlapping circles - find the form-faktor

From: Sean de

Date: 9 Dec, 2010 16:44:08

Message: 5 of 12

"Rafael " <mad_hanf@web.de> wrote in message <idq9ik$pt4$1@fred.mathworks.com>...
> Hello @ll,
>
> i am stuck with a problem, where my mind keeps running in circles (no pun intended).
>
> I have an image of circular objects and I am trying to find the "roundness" of each one, viz. the form faktor.
>
> Everything would work perfectly fine (in accordance with the demo on "identifying round objects" http://www.mathworks.com/products/image/demos.html?file=/products/demos/shipping/images/ipexroundness.html), if my objects weren´t so close that their boundaries overlap.
>
> In order to calculate the form-faktor, i need to know the area and the circumference of my circle (that´s why overlapping circles give such a huge error). So as far as i can see, it is probably easiest to apply "bwboundaries(bw,'holes')" in the process and use this to calculate the perimeter and area. However, I need clearly separated "circles" in order to do this correctly.
>
> I have uploaded the image, so that you can imagine more clearly my problem: http://s1195.photobucket.com/albums/aa382/rtaker/
>
> My question is, how am I able to separate the circular objects in my image? I am pretty inexperienced in matlab so any code-examples would also be highly appreciated.
>
> An idea that I had, was to use a sobel-filter and then apply the ellipse fit by Nikolai Chernov (see http://www.mathworks.com/matlabcentral/fileexchange/22683). However i am still having the problem that i am not able to segment my image properly so that i can apply the fit properly (and hence define clearly separated borders).
>
> It would be great if anybody could help me out.
> Thank you very much for your time.


Give this a shot! It will only do the circles not touching the borders. If you want the ones on the borders it would be quite easy do use an exclusive or and recover them.

%%%
%Read Image
I = imread('test2.jpg'); %your original image

Iri = 255-I(:,:,1); %I red inverse
[M] = shrinkWrap(Iri,'objthresh',2750); %Map
%shrinkWrap available here:
%http://www.mathworks.com/matlabcentral/fileexchange/29175-shrinkwrap

%Blobs defined by the distance from an eroded image via distance transform,
%small stuff removed
Iblobs = bwareaopen(bwdist(~M)>60,1000);

%Label individual objects
Ilabel = bwlabel(Iblobs);

%Make each object its own slice in 3D volume, when doing so, expand back to
%original size.
Iblobs3 = false(size(M,1),size(M,2),max(Ilabel(:)));
for ii = 1:max(Ilabel(:))
    Islice = Ilabel == ii;
    Islice(bwdist(Islice)<60&M) = true;
    Iblobs3(:,:,ii) = Islice;
end
%Map of parts in more than one object (violation of interpenetration
%constraint) and remove them (bsxfun operation)
idx = sum(Iblobs3,3,'double')<=1;
Iblobs3 = bsxfun(@times,Iblobs3,idx);

%There will now be dents where there was overlapping, use convex hull to
%fill them
for ii = 1:size(Iblobs3,3)
    [r c] = find(Iblobs3(:,:,ii));
    k = convhulln([r c]);
    Iblobs3(:,:,ii) = poly2mask(c(k(:,1)),r(k(:,1)),size(Iblobs3,1),size(Iblobs3,2));
end

%Each slice of Iblobs3 will be an object!
implay(Iblobs3) %visualize!

%SCd
%%%

Subject: overlapping circles - find the form-faktor

From: ImageAnalyst

Date: 9 Dec, 2010 17:43:28

Message: 6 of 12

On Dec 9, 11:01 am, "Rafael " <mad_h...@web.de> wrote:
> i have talked to a friend in the meantime and he suggested to me "region growing with shape prior "
>
> what do you guys think? have you ever tried it?
>
> best regards

------------------------------
I think that level of complication is unneeded. This is very simple.
1. take the correct color channel to maximize contrast
2. threshold to create a binary image
3. imfill
4. erode to create markers
5. marker controlled watershed, like in the demo. You may have to
adjust some of the parameters for your image.

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 9 Dec, 2010 18:39:05

Message: 7 of 12

@Sean De:
thanks a lot for your effort! I ran your code (is it normal that it takes more than 20 minutes?), but frankly I am a bit lost what I am supposed to do with a video as output. Is there an easy way to put it back into one complete picture?

@ImageAnalyst:
thanks again for your quick reply ... moving directly to a binary image improves the segmentation a lot. However, the overlapping circles are still in one segment, so unfortunately i am still not able to apply the circular/ellipse fit

The code i have so far is:

rgb = imread('test1.jpg');

%1.)create binary image and imfill
I=rgb2gray(rgb);
threshold=graythresh(I);
bw=im2bw(I,threshold);

bw=bwareaopen(bw,10);
bw=imfill(~bw,'holes');

%2.) erode and dilate
se = strel('disk',20);
Ie=imerode(bw,se);
Iobr=imreconstruct(Ie,bw);

Iobrd = imdilate(Iobr,se);
Iobrcbr=imreconstruct(imcomplement(Iobrd),imcomplement(Iobr));
Iobrcbr=imcomplement(Iobrcbr);

%3.) find the foreground and use the watershed
fgm=imregionalmax(Iobrcbr);

D=bwdist(Iobrcbr);
DL=watershed(D);
bgm=DL==0;

%4.) find the background and use the watershed

hy=fspecial('sobel');
hx=hy';
Iy=imfilter(double(I),hy,'replicate');
Ix=imfilter(double(I),hx,'replicate');
gradmag=sqrt(Ix.^2 + Iy.^2);

gradmag2=imimposemin(gradmag,bgm | fgm);
L=watershed(gradmag2);

%5.)apply them on the image
I2=I;
I2(imdilate(L==0, ones(3,3))|bgm|fgm)=255;
imshow(I2)

Subject: overlapping circles - find the form-faktor

From: Sean de

Date: 9 Dec, 2010 18:55:14

Message: 8 of 12

"Rafael " <mad_hanf@web.de> wrote in message <idr7o9$964$1@fred.mathworks.com>...
> @Sean De:
> thanks a lot for your effort! I ran your code (is it normal that it takes more than 20 minutes?), but frankly I am a bit lost what I am supposed to do with a video as output. Is there an easy way to put it back into one complete picture?
>

It takes about 15 seconds on my machine, so no 20 mins is wrong. Save it to an mfile and run it all at once. Make sure you change the image name to your actual image name.

There is an easy way to bring it back to 2D but that's not what you want! You want to do math on each object, if we concatenate all of the objects back to one picture they'll be overlapping again which would defeat the whole purpose! I played the movie strictly so you could see what each object looked like after segmentation. Now you have to come up with your form factor algorithm and apply it to each slice of Iblob3 (i.e. every unique object).

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 9 Dec, 2010 21:09:05

Message: 9 of 12

@Sean De: Thanks again for the reply. I just wanted to put all slices back into one image, so that I could varify the accurateness of your segmentation-algorithm. For example, taking a glimpse at the single slides also reveals the overlapping circles are not separated (for example this one: http://s1195.photobucket.com/albums/aa382/rtaker/?action=view¤t=shrinkWrap_trial.jpg).

Therefore the overlapping circle-problem still persists :(

PS: Concerning the runtime of your algorithm, it doesn´t change ... applying the "shrinkWrap"-function takes about 20 minutes for me. Maybe my picture resolution is too high? my final Iblobs3-matrix has the dimension 2592x3888x6

Subject: overlapping circles - find the form-faktor

From: Sean de

Date: 9 Dec, 2010 21:24:05

Message: 10 of 12

"Rafael " <mad_hanf@web.de> wrote in message <idrghg$o4l$1@fred.mathworks.com>...
> @Sean De: Thanks again for the reply. I just wanted to put all slices back into one image, so that I could varify the accurateness of your segmentation-algorithm. For example, taking a glimpse at the single slides also reveals the overlapping circles are not separated (for example this one: http://s1195.photobucket.com/albums/aa382/rtaker/?action=view¤t=shrinkWrap_trial.jpg).
>
> Therefore the overlapping circle-problem still persists :(
>
> PS: Concerning the runtime of your algorithm, it doesn´t change ... applying the "shrinkWrap"-function takes about 20 minutes for me. Maybe my picture resolution is too high? my final Iblobs3-matrix has the dimension 2592x3888x6

Don't use the shrinkWrap function then, it only creates the mask and is a small part of whole whole algorithm. Do what you were doing to acquire the binary mask. (threshold, imfill etc.) I just used it because it was quick, accurate and versatile, at least for the image you had posted.

Try this:
Download your "original image" (the colored one) from the website (like we had to do) and run the above code with it. You'll see it works. The image on the website was originally 682x1023 - much smaller than the one you just described.

Good Luck!
-Sean

Subject: overlapping circles - find the form-faktor

From: Rafael

Date: 18 Feb, 2011 12:13:03

Message: 11 of 12

I had almost forgotten this thread, since I have already implemented a working solution for my problem. In case anyone is browsing through this post with a similar problem (like i did before with others), I just wanted to let you know what worked for me, namely the "circular Hough Transform"

someone posted a really useful file on file-exchange for that: http://www.mathworks.com/matlabcentral/fileexchange/9168-detect-circles-with-various-radii-in-grayscale-image-via-hough-transform

thanks again for everybody´s suggestions

Subject: overlapping circles - find the form-faktor

From: Ali

Date: 11 Apr, 2013 00:29:08

Message: 12 of 12

"Rafael" wrote in message <ijlnof$2g5$1@fred.mathworks.com>...
> I had almost forgotten this thread, since I have already implemented a working solution for my problem. In case anyone is browsing through this post with a similar problem (like i did before with others), I just wanted to let you know what worked for me, namely the "circular Hough Transform"
>
> someone posted a really useful file on file-exchange for that: http://www.mathworks.com/matlabcentral/fileexchange/9168-detect-circles-with-various-radii-in-grayscale-image-via-hough-transform
>
> thanks again for everybody´s suggestions

Hi Rafael,
I just wanted to ask you how you solved your problem with overlapping circles.I have the same problem and I want to find the size of each overlapping circles in my image. My image (gray scale) includes many overlapping shapes like ellipse and circle. That would be nice if you could show me the right path.
Thank you

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us