Create composite mask, and apply this mask to an image

33 views (last 30 days)
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

Accepted Answer

Tarunbir Gambhir
Tarunbir Gambhir on 2 Dec 2020
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 Comments
Shane Browne
Shane Browne on 3 Dec 2020
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 on 4 Dec 2020
Yes. After getting the mask you could use a "for" loop in this way to check if the mask worked for every slice.

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!