MATLAB Answers


How to mask out the object in the image?

Asked by Kuvvat Garayev on 17 Apr 2019
Latest activity Answered by Akira Agata
on 19 Apr 2019
I have an image of an animal (mantis shrimp) along with seeding particles (white dots). I want to mask out the animal only and change the background into white. I will use the resultant image as a mask in PIV analysis.
I have tried intensity thresholding, and minimum, median filters but they are of less help because the animal tail has hair with low intensity value and as a result it is also deleted with white dots. I think thresholding based on intensity value doesn't work.
I can simply crop the animal by outlining it but because I have hundreds of images this method is pointless.
I attached the original image along with the desired one done through outlining by hand.


What does it matter if the tail hair is deleted? Why do you need it? What are you going to do with the tail hair anyway? Just threshold, dilate a little bit if you want to, then call bwareafilt(BW, 1) to extract the largest blob.
Thanks for the reply.
If the animal is not deleted completely including its tail hair then the PIV software would find wrong velocity vectors on the tail. In PIV, vector is found by comparing displacement of seeding particles (white dots in the image) in adjacent images. Since the animal is also illuminated by the laser light, the software finds vectors on the tail hair, antenna etc. incorrectly. The reason I want to get the correct silhouette of the animal is to have perfect velocity vectors in the flow on the images. When animating the images (few 1000) one can see the moving animal along with velocity vectors around it.

Sign in to comment.

1 Answer

Answer by Akira Agata
on 19 Apr 2019

How about the following?
% Read and binarize your image
I = imread('ms.jpg');
Igray = rgb2gray(I);
BW = imbinarize(Igray);
% Delete regions connecting to the image border
BW = imclearborder(BW);
% Fill holes
BW = imfill(BW,'hole');
% Extract the region with maximum size
BW = bwareafilt(BW,1);
% Make a mask
BWmask = ~BW;
% Show the result


Sign in to comment.