A glaring error with your code is this:
P=[blobMeasurements(i).PixelList];
Daurat(P)=0;
PixelList is a 2d-array of [columns rows], you then use as a 1d-array to set pixels black. Instead of PixelList, you need to use PixelIdxList which is a 1d-array of pixel indices. So change your regionprops to:
blobMeasurements = regionprops(labeledImage, Daurat, 'Area', 'Centroid', 'Perimeter', 'PixelIdxList');
A more important issue is that the test you're doing does not guarantee you a shape close to circle. The r you're calculating is
- for a circle of radius R: r = 2*pi*R/(pi*R^2) = 2/R
- for a square of side 2R: r = 4*2*R/(2R * 2R) = 2/R
- for a thin rectangle of sides a=202R/200 and b = 100*a: r = 2/R
Thus, you can find any shape with a r >0.3.
You would be better off using the Eccentricity property returned by regionprops. An Eccentricity of 0 is a perfect cirle, 1 is a line. Thus I would have:
blobMeasurements = regionprops(labeledImage, Daurat, 'Area', 'Centroid', 'Perimeter', 'PixelIdxList', 'Eccentricity');
for i=1:numberOfBlobs
if blobMeasurements(i).Eccentricity > 0.85
Daurat(blobMeasurements(i).PixelIdxList) = 0;
end
end
One final thing:
per(:,1) = [blobMeasurements.Perimeter];
is equivalent to:
per = [blobMeasurements.Perimeter]';
which looks less odd to me. In any case you don't need it as a column vector so I would just have:
per = [blobMeasurements.Perimeter];
Best Answer