How can i detect irregular shapes in an binary image?

Hello everyone,
I have the following Problem: I need to find irrgular shapes in an image so I can exclude them from my calculations. I attached an example picture. In it I want to do calculations with the square-like and triangle-like shapes and i want to ignore the rest. The regular shapes can be smaller than the irregular ones but they can also be bigger, so the only thing I have are the shapes. I tried using a few different properties of regionprops but none can really help me, the only one I was able to use was 'MajorAxisLength' for the ones that are bigger than the regular shapes, because they have a maximum size. The others I wasn't able to use. Either because it's impossible or i just lack the knowledge. I hope someone can help me with it. Thanks in advance
Johannes Schmidt

 Réponse acceptée

I think one simple way is to use regionprops function, and tune some conditions to detect irregular shape. The following is one example.
BW = imread('Test.bmp');
BW = imclearborder(BW);
stats = struct2table(regionprops(BW,{'Area','Solidity','PixelIdxList'}));
idx = stats.Solidity < 0.9 | stats.Area <350;
for kk = find(idx)'
BW(stats.PixelIdxList{kk}) = false;
end
imshow(BW)

Plus de réponses (1)

The nice code given by Akira will work as long as the blobs to be removed are all not very convex (low solidity) or small. It can be done in fewer lines using bwpropfilt() (introduced in R2014b):
BW = imread('Test.bmp');
outputImage = bwpropfilt(BW, 'Solidity', [.9, inf]);
outputImage = bwpropfilt(outputImage, 'Area', [350, inf]);
However if there are some large blobs that are smooth (not irregular), it will allow these to survive. To me it actually looks like you have blobs on a known, regular grid and just want to take blobs if they overlap those grid point locations. If so you could use bwselect(), passing it known grid locations, and get only the blobs that are there and reject blobs that are not on the grid. This simple code will do that:
outputImage = bwselect(binaryImage, X, Y, 8);
See attached code for a full demo. It produces the desired output image. The drawback is that if you have a giant undesired blob that falls across the grid locations, it could be selected. However you could follow up with a call to bwpropfilt() to exclude blobs with an area more than some amount. Bottom line, with either Akira's or my code, to be super robust, you should decide what to do if the undesired blobs touch/overlap/are connected to the desired blobs. Do you keep such a blob or not? Or are you 100% certain that you never have to worry about that situation?

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by