Image Segmentation Bounding Box
Show older comments
I have a image that contains the topview of an animal. The objective is to isolate the animal from the image. The original image can be seen in the left upper picture form the following figure:

I do some preprocessing steps that lead to the image (inverseBW_filtered) shown in the right lower corner. Using this image, I run the following line of code:
stats = regionprops(inverseBW_filtered, 'BoundingBox', 'Centroid');
Sadly, just one bounding box is found (plotted as the red box). I would have expected to get a few boxes at least with one of them surrounding the animal. Can I get some suggestions how to make sure that the animal gets segmented using regionprops (or any other technique)?
3 Comments
Image Analyst
on 31 May 2023
That does not look like an original image in the upper left. At the very least it's pseudocolored. Do you have an image of hte background without the animal in it that we can subtract using imabsdiff?
Don't invert your mask. You want the animal/foreground to be white like in the lower left image. Because you inverted the image, that's why your bounding box is the whole entire image.
Henk-Jan Ramaker
on 1 Jun 2023
Pratham Shah
on 1 Jun 2023
Great!
I think this is what you wanted, right?
Answers (2)
Pratham Shah
on 1 Jun 2023
Hi!
To get the bounding box around the animal; If you know the area animal the animal will cover use blobAnalysis function. To remove the white pixels from bottom-right corner you can use 'imclearborder' morphological function.
newBW=imclearborder(ImgBW,8); %You can change '8' depending on your application
blob = vision.BlobAnalysis('BoundingBoxOutputPort', true,...
'AreaOutputPort', false, 'CentroidOutputPort', false, ...
'MinimumBlobArea', 50);
box= step(blob, newBW);
Output = insertShape(Image, 'Rectangle', box, 'Color', 'red','Linewidth',2);
1 Comment
Henk-Jan Ramaker
on 1 Jun 2023
Image Analyst
on 1 Jun 2023
Edited: Image Analyst
on 1 Jun 2023
0 votes
If you don't have a background image, you can create one by taking the mode of every pixel over all frames in the video (or as many of them as you can fit in memory).
You can even get an estimate for the background if you can hand trace the animal in one frame. Then you can use regionfill to estimate the background underneath the animal. Demos for hand tracing regions are attached.\
It's kind of weird that the height of the animal is both higher and lower than the background at the left of the scene. Do you have a visible light image of the scene you can share? The animal looks like it's shaped like a trough.
2 Comments
Henk-Jan Ramaker
on 1 Jun 2023
Image Analyst
on 1 Jun 2023
If you have an empty/no-animal image, then you can find the animal by using imabsdiff followed by thresholding.
diffImage = imabsdiff(emptyFrame, currentFrame);
mask = diffImage > someValue; % Threshold to find "tall" things in the scene.
% Take largest blob only.
mask = bwareafilt(mask, 1);
Categories
Find more on Image Arithmetic 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!