MATLAB: How can i detect irregular shapes in an binary image

image processingimage segmentation

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

Best Answer

  • 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)