How to mask out the object in the image?

조회 수: 52 (최근 30일)
Kuvvat Garayev
Kuvvat Garayev 2019년 4월 17일
댓글: Robert Corns 2022년 8월 11일
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
Image Analyst
Image Analyst 2019년 4월 18일
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 2019년 4월 18일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Akira Agata
Akira Agata 2019년 4월 19일
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
Kuvvat Garayev
Kuvvat Garayev 2020년 6월 12일
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 2022년 8월 11일
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);

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by