MATLAB Answers


Hi, is there a way to detect irregular shapes, then select the ones that are closest to circles and use those for analysis?

Asked by Jehangir
on 6 Nov 2013
Latest activity Commented on by Jehangir
on 6 Nov 2013

My problem can be seen in the figure attached. I am looking to detect dark circular objects that are flowing in an experiment surrounded by fluorescent material (bright). Due to the flow, they can get distorted and elongated. I need to determine the intensity in their centers, and measure the radius (and center position).

I have been using imfindcircles as follows (I load a tif stack into FinalImage):

for i=3:NumberImages
I= FinalImage(:,:,i);
Ibg= FinalImage(:,:,i-2);
I1= Ibg-I; %so now my dark circles are bright 
I2= imadjust(I1);
level= graythresh(I2);
bw= im2bw(I2,level);
bw= bwareaopen(bw,5);
BWdfill = imfill(bw,4, 'holes');
BWnobord = imclearborder(BWdfill, 4);
seD = strel('diamond',1);
BWfinal = imerode(BWnobord,seD);
BWfinal = imerode(BWfinal,seD);
if test>500 %this is my condition for event detection
Rmin = 15;
Rmax = 45;
[centersBright, radiiBright] = imfindcircles(BWfinal,[Rmin Rmax],'ObjectPolarity','bright','Sensitivity',0.9);
if numel(centersBright)~=0
circ=viscircles(centersBright, radiiBright,'EdgeColor','b');
f = getframe(gcf);
imwrite(f.cdata, outputFileName1, 'WriteMode', 'append',  'Compression','none'); %this is the part that I'm showing you in the image

I have a more sophisticated background subtraction loop as well, but that is irrelevant here.

The image on the RHS is an acceptable detection for me, but the one on the LHS is not. Is there a way for me to detect all these events, whatever their shape, and then select the ones closest to circles and use only those for analysis?

I'm rather new to Matlab, so any help would be much appreciated.

Thanks and regards Jehangir


Log in to comment.

1 Answer

Answer by Image Analyst
on 6 Nov 2013
 Accepted answer

Then I would not use imfindcircles. Just threshold and find the blobs. Then ask regionprops to give you the area and perimeter. Then calculate the circularities and keep the ones that you think are round enough. Here's some untested code just off the top of my head.

binaryImage = grayImage > 128; % or whatever.
labeledImage = bwlabel(binaryImage);
measurements = regionprops(labeledImage, 'Area', 'Perimeter');
allAreas = [measurements.Area];
allPerimeters = [measurements.Perimeter];
circularities = (allPerimeters .^ 2) ./ (4 * pi * allAreas);
isRoundEnough = circularities < 4; % or whatever value you want.
% Get new labeled image with only the round blobs.
keeperLabels = find(isRoundEnough);
labeledImage = ismember(labeledImage, keeperLabels);
% Re-measure only the round blobs.
measurements = regionprops(labeledImage, 'Area', 'Perimeter');


Thanks a lot, will give it a try and see how it goes.

But I don't understand just the first line of your code. What does grayImage > 128 do? Thanks

It thresholds to give a binary image - foreground and background - so that you can get the area and perimeter of the foreground.

Log in to comment.

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

MATLAB Academy

New to MATLAB?

Learn MATLAB today!