how to separate objects in image using matlab
Show older comments
Accepted Answer
More Answers (2)
Jonas
on 21 Jun 2021
if your input image has white background you can easily generate a binare image after conversion to grayscale and using thresholding
grayIm=rgb2gray(im);
binIm=grayIm(grayIm<0.9);
on the binary image you can use bwconvhull with the 'objects' option or/and finally bwlabel(); this gives you the positions in which you can find different objects. you can then cut the objects out of the original image and save them as separate image
Image Analyst
on 23 Jun 2021
What I'd do is to binarize the image then call imfill() to fill holes in the objects.
grayImage = rgb2gray(rgbImage);
binaryImage = grayImage < 220;
binaryImage = imfill(binaryImage, 'holes');
Then call regionprops to get the bounding box of each image.
props = regionprops(binaryImage, 'BoundingBox');
Then I'd crop out each sub-image. Then I'd use a double for loop with isequal() to compare every image with every other image to see which subimage is unique.
numObjects = length(props)
alreadyUsed = false(numObjects); % Keep track if we've already seen this object or not.
for k1 = 1 : numObjects
image1 = imcrop(grayImage, props(k1).BoundingBox);
foundIt = false;
for k2 = 1 : numObjects
if k1 == k2
% Don't bother comparing object to itself.
continue;
end
image2 = imcrop(grayImage, props(k2).BoundingBox);
foundIt = false;
if isequal(image1, image2)
foundIt = true;
break; % Don't bother checking against the others, unless you want a count.
end
end
if ~foundIt
% We have not seen this object yet. So paste it onto our
% output canvass.
% Code for you to complete to paste image1 onto canvass
end
end
If it's unique, paste it onto your output canvass. If it's not unique and it's the second instance of the object, then don't paste it onto the output image. It should work as long as it's a computer graphics image and all objects are identical, i.e. no varying compression artifacts. If there are slight differences, then make sure they're the same size and use corrcoeff() to see how similar the images are.
Some demos are included which may be informative.
Categories
Find more on Region and Image Properties in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

