Matlab - How to hide a three-dimensional image using a binary image

I have an image with a red, green, blue channel and a binary version of the image.

What I want to do is combine these 2 images so that the binary image works like a mask for a regular image.

I want to select only pixels from a color image that are 1 in binary format.

I know this should work with cat or even repmat , but since I'm pretty new to Matlab, I can't figure out how to do this even after reading the function docs.

+1
source share
3 answers

If you have a three-dimensional image I and a binary mask M , you can mask the non-essential bits by zero or by multiplying the image by the mask:

 I = bsxfun(@times, I, M); 

or by logical indexing:

 I(~mask(:, :, ones(1, size(I, 3)))) = 0; 
+6
source

I am not 100% sure, I understand your problem, but here comes one suggestion:

Suppose rgbIm is your RGB image and bwIm is your binary image;

You can try to โ€œdeployโ€ your binary image to โ€œ3Dโ€ (so that its size matches the original RGB image) with the following line of code:

 bwImAux = bwIm(:,:,[1 1 1]); 

And then do a simple multiplication to โ€œeliminateโ€ all the pixels that are not in the binary image:

 rgbImNew = rgbIm.*bwImAux; 

Hope this helps.

+1
source

You can use a binary image as a logical index in a 3dim image. To reset all pixels in an image that are zero in the binary mask , you can use the following code for each dimension: image(~mask)=0;

+1
source

Source: https://habr.com/ru/post/1233273/


All Articles