How to mask out the object in the image?
50 views (last 30 days)
Show older comments
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
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.
Answers (1)
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)
2 Comments
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);
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!