How to mask out the object in the image?

50 views (last 30 days)
Kuvvat Garayev
Kuvvat Garayev on 17 Apr 2019
Commented: Robert Corns on 11 Aug 2022
Hello,
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.
  2 Comments
Image Analyst
Image Analyst on 18 Apr 2019
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.
Kuvvat Garayev
Kuvvat Garayev on 18 Apr 2019
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.

Answers (1)

Akira Agata
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
figure
imshow(BWmask)
mask.png
  2 Comments
Kuvvat Garayev
Kuvvat Garayev on 12 Jun 2020
Hi Akira. Thanks for the simple solution. Although the above lines of code worked well for this particular image, it doesn't work for others. For example, I have attached another image of the animal with different illumination. After running the code on this image I got unacceptable mask image which is attached too.
Since I asked the question, I came up with the solution using ImageJ. The result is attached, though not perfect. I will leave the problem open for now, with the hope that even better solutions might be found.
Robert Corns
Robert Corns on 11 Aug 2022
Akira has a suggestion. You can use thresholding in the binarizatin to fine tune selections. The only problems are (1) there may not be a single global threshold, (2) What value to pick for a threshold.
%read image
I = imread('ms.jpg');
Igray = rgb2gray(I);
BW = imbinarize(Igray, 0.03); %0.03 picks off the large shrimp
% 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;
BW2 = imbinarize(Igray, 0.022); %0.22 picks off the small object
% Delete regions connecting to the image border
BW2 = imclearborder(BW2);
% Fill holes
BW2 = imfill(BW2,'hole');
% Extract the region with maximum size
BW2 = bwareafilt(BW2,1);
% Make a mask
BWmask = ~or(BW2, BW);

Sign in to comment.

Categories

Find more on Images 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!