Asked by Carine
on 23 Jan 2014

Hello,

I want to be able to detect and draw a square around some circles. These circles are taken with a camera so depending of the angle of view and the distance, the radius vary and they can be deformed.Here are two examples: First example and Second example.

The last thing I have tried is using the CircularHough_Grd function from Tao Peng but it finds circles that are tangent of the sought circles: Result with CircularHough_Grd. And I am not sure how resistant this function is to deformation.

Any suggestion would be welcome as I am running a bit out of ideas at the moment.

Cheers.

Answer by Image Analyst
on 23 Jan 2014

Accepted Answer

Image Analyst
on 28 Jan 2014

Carine
on 28 Jan 2014

Hi again, I am of course spending the day on it... But the day is ending her in Europe. I have tried the another example but the result is rather worse. Your code give this plot:

</matlabcentral/answers/uploaded_files/7489/60b.jpg> and the boundary part:

More tomorrow.

I really appreciate your help! Cheers-

Carine
on 30 Jan 2014

Hello, I have finally something quite satisfying (history will see if fame is approaching dear Image Analyst ;-)):

nomFile = 'Piscine80b.JPG';

rgbImage = imread(nomFile);

% Get the dimensions of the image. numberOfColorBands should be = 3.

[rows, columns, numberOfColorBands] = size(rgbImage);

redChannel = rgbImage(:, :, 1);

level = graythresh(rgbImage);

binaryImage = im2bw(redChannel,level);

binaryImage = bwareaopen(binaryImage, 20000);

binaryImage = ~bwareaopen(~binaryImage, 20000);

[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);

% Label each blob so we can make measurements of it

fprintf('Found %d blobs.\n', numberOfBlobs);

coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');

% pseudo random color labels

% Measure the regions

measurements = regionprops(labeledImage, 'Perimeter', 'Area');

% Select certain blobs based using the ismember function.

% Let's say that we wanted to find only those blobs

% with a circularity more than 5 or so.

allBlobPerimeters = [measurements.Perimeter];

allBlobAreas = [measurements.Area];

allCircularities = allBlobPerimeters .^ 2 ./ (4*pi*allBlobAreas)

% Get a list of the blobs that meet our criteria and we need to keep.

% keeperIndexes = find(allCircularities < 1.5)

[~,keeperIndexes] = min(allCircularities)

% Extract only those blobs that meet our criteria, and

% eliminate those blobs that don't meet our criteria.

% Note how we use ismember() to do this.

keeperBlobsImage = ismember(labeledImage, keeperIndexes);

% Measure the regions, this time getting equivalent circular diameter and centroid.

measurements = regionprops(logical(keeperBlobsImage), ...

'Perimeter', 'Area', 'EquivDiameter', 'Centroid','BoundingBox');

ecd = measurements.EquivDiameter

equivDiameter(k,lettre) = ecd;

c = measurements.Centroid;

centroid(k,lettre,:) = c;

BoundingBox = [measurements.BoundingBox]

% Create a square mask including the circle

x0 = BoundingBox(1); y0 = BoundingBox(2); % Upper left corner

w = BoundingBox(3); h = BoundingBox(4); % Width and heigth

dw = 0.1*w; dh = 0.1*h; % To let some margin

mask = zeros(rows,columns);

mask(max(1,ceil(y0-dh)):min(rows,ceil(y0+h+dh)), ...

max(1,ceil(x0-dw)):min(columns,ceil(x0+w+dw))) = 1;

mask = logical(mask);

Imask = rgbImage;

Imask(~mask) = 255;

IbinMask = ~(~binaryImage.*mask);

close

togglefig(nomFile)

imshow(rgbImage)

pause

imshow(IbinMask)

hold on

plot(c(1), c(2), 'r+', 'MarkerSize', 20, 'LineWidth', 3);

plot(c(1)-ceil(ecd/2):c(1)+ceil(ecd/2),c(2),'r','LineWidth', 3);

hold off

pause

end

end

Using that, I can define the box around my "nearly circle" and select it, draw the equivalent diameter and the centre. Thank you again for you help!

Sign in to comment.

Answer by Carine
on 24 Mar 2014

Hello,

I would like to reopen this question as, dealing with a noiser picture, it does not work anymore. For example, if I take a picture which I process and transform to black and white. I get this one:

Now, when I try to find regions with regionprops, it just finds one in red. Whereas I need the system to dectect my black circle.

</matlabcentral/answers/uploaded_files/10333/region.jpg> To get it, following the previous idea of this thread I do:

[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);

coloredLabels = label2rgb (labeledImage, 'hsv', 'k', 'shuffle');

imshow(coloredLabels)

Any idea on how I could detect the circle?

Cheers.

Image Analyst
on 24 Mar 2014

Fix your link, and post your original image.

Sign in to comment.

Answer by Carine
on 26 Mar 2014

Edited by Carine
on 26 Mar 2014

OK, here is a full example: I have this picture:

</matlabcentral/answers/uploaded_files/10400/Turbio10b.JPG> or link here. I process it using adapthisteq with Rayleigh distribution and clip of 1 and get:

</matlabcentral/answers/uploaded_files/10401/Turbio10bHistRay1.jpg> or link here: here or en formato fig.

Now I transform it to black and white and use bwareaopen to clean the image a bit:

Then if I try to find the blobs with

[labeledImage, numberOfBlobs] = bwlabel(binaryImage, 8);

I get this, link here or in fig . I find it very surprising as in very similar example, it works and detect both circles.

Cheers,

Carine.

P.S. I put hyperlink as it seems the direct links to the images don't work. I don't know why as when I download them, I am able visualize them but then it stop working.

Image Analyst
on 26 Mar 2014

So you got it working?

And to insert images, did you use the image icon? Did you pick an image on the web or one from your hard drive?

Carine
on 27 Mar 2014

Image Analyst
on 27 Mar 2014

Your links are broken. Let me see if I can upload one from my hard drive from the image icon and see if the link it inserts is broken.

It seems *I* can insert one. Please try again.

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 2 Comments

## Image Analyst (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/113477-detect-concentric-empty-rough-circles#comment_191768

## Carine (view profile)

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/113477-detect-concentric-empty-rough-circles#comment_191809

Above there is one and here is the second one

Sign in to comment.