Create composite mask, and apply this mask to an image

조회 수: 32 (최근 30일)
Shane Browne
Shane Browne 2020년 11월 29일
댓글: Tarunbir Gambhir 2020년 12월 4일
Hi,
Scanning samples using NIR, processing the spectra in matlab.
I have an image, and I need to create a mask to remove the background.
I have been instructed to snip sections of the image, creating masks.
I then have to combine these masks (x3) into one composite and apply it to the overall image. Removing the background.
CL below is a crop of image I5.
CL=I5(131:149,36:43,:);
[xC,yC,zC]=size(CL);
figure,imshow(mean(CL,3),[])
I searched through a loop showing each slice of the hypercube. Slice for example 69 has contrast between the item and the background.
The threshold excludes the background.
%%CL snip one
figure,
subplot(1,2,1),imshow(CL(:,:,69)<0.26,[])
subplot(1,2,2),histogram(unfold(CL(:,:,69)),100)
CL_mask=CL(:,:,69)<0.26;
%% CL3 my second crop of the image
CL3=I5(125:150, 15:34,:);
[xC3,yC3,zC3]=size(CL3);
figure,imshow(mean(CL3,3),[])
CL_mask3=CL3(:,:,81)<0.1;
%% CL4 snip four
CL4=I5(153:168,25:41,:);
[xC4,yC4,zC4]=size(CL4);
figure,imshow(mean(CL4,3),[])
CL_mask4=CL4(:,:,65)<0.24;
Is there a way to combine the three masks? CL_mask4 + CLmask3 + CL_mask ?
I am a beginner level user, but I can provide more info if it helps.
Help would be hugely appreciated.
Shane

채택된 답변

Tarunbir Gambhir
Tarunbir Gambhir 2020년 12월 2일
A better approach would be to use a single mask for your entire image. Start with a matrix of same size as your image and update it at locations where the threshold condition suffices inside the cropped regions.
Something like this could be done:
% Assuming the dimensions of image to be Spacial x Spacial x Spectral
mask = zeros(size(I5,[1,2])); % Initialize mask with zero across spacial dimensions
%% CL
mask(131:149,36:43)=I5(131:149,36:43,69)<0.26;
%% CL3
mask(125:150,15:34)=I5(125:150,15:34,81)<0.1;
%% CL4
mask(153:168,25:41)=I5(153:168,25:41,65)<0.24;
Repeating the mask value across all spectral slices and then dot-multiplication with the original image should give you the masked image.
%% Applying the mask to I5
final_image = I5 .* repmat(mask,[1 1 size(I5,3)]);
  댓글 수: 2
Shane Browne
Shane Browne 2020년 12월 3일
Thank you very much for the help it is greatly appreciated.
The image size was defined as:
[x,y,z]=size(I5);
I previously had a loop to check if a mask worked on all slices of the hypercube.
CL_mask=CL(:,:,69)<0.26;
CL_masked=zeros(size(CL));
for i=1:z
CL_masked(:,:,i)=CL(:,:,i).*CL_mask;
end
%taking a look through the slices to check the mask worked ok
figure,
for i=1:z
imshow(CL_masked(:,:,i),[]),title(sprintf('%d',i))
pause (0.1)
end
Is it possible to run a loop like this checking each slice with the new mask?
Tarunbir Gambhir
Tarunbir Gambhir 2020년 12월 4일
Yes. After getting the mask you could use a "for" loop in this way to check if the mask worked for every slice.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Image Data Workflows에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by