Isolating objects detected in a binary image

8 views (last 30 days)
Hello,
I am currently working on my dissertation, in which I am using machine learning to classify different objects in binary images. I have gotten pretty far and am trying to get individual photos for all the objects in the binary image. I am doing this by using a combination of getting the bounding boxes from regionprops and by using imcrop/imwrite. My hope is to get a library of these images which I can use to train a deep learning classification algorithm and see how it compares to supervised machine learning methods. My only issue is smaller objects keep getting caught in the background of cropped images for bigger objects. I've attached images as examples below. My question is how can I isolate each object, so that a cropped image only has the object I want without the 'background' objects.
After having a slight think, I wonder if theres a way to iterate through the library of objects in the image based on it's area, then filtering out the smallest objects first and removing them from the binary image. I believe this might work, but I've no idea how to implement it.
Thanks for your help in advance!
E

Accepted Answer

DGM
DGM on 13 Mar 2024
Edited: DGM on 13 Mar 2024
You can use the 'image' property from regionprops() to avoid a lot of hassle here.
% a logical image with two blobs whose bounding boxes overlap
mask = imread('tinyblobs.png');
imshow(mask,'border','tight')
% get all the blobs as close-cropped isolated images
S = regionprops(mask,'image');
% show the extracted blobs for sake of clarity
imshow(S(1).Image,'border','tight')
figure
imshow(S(2).Image,'border','tight')
% write all the images in whatever way is appropriate for your needs
% PNG is lossless and compact for logical inputs
for k = 1:numel(S)
thisimg = S(1).Image;
thisname = sprintf('myimage_%04d.png',k);
imwrite(thisimg,thisname)
end
If you want to exclude some blobs based on area or other properties, you can simply fetch those properties in the same call to regionprops() and then process only those blobs which meet your requirements.

More Answers (0)

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!