How do I create an RGB image mask using a 2-D logical matrix?

I am trying to create an image mask using a logical 2-D matrix as the mask. The 3-D image matrix and the 2-D mask matrix are the same dimensions. Wherever the logical mask is true, I want to keep the image as is. Where the mask is false, I want to change the intensity of the image. How do I go about doing this?
Here is my attempt so far:
[ maskedImage ] = maskImage( input_image, mask_image )
red = input_image(:, :, 1);
green = input_image(:, :, 2);
blue = input_image(:, :, 3);
if mask == 1
input_image(:, :, 1) = red;
input_image(:, :, 2) = green;
input_image(:, :, 3) = blue;
else
input_image(:, :, 1) = red .* 0.3;
input_image(:, :, 2) = green .* 0.3;
input_image(:, :, 3) = blue .* 0.3;
end
maskedImage = input_image;
end
I know that my indexing is incorrect, because I want to change the values in the image matrix where the values in the mask matrix are false, but I don't know how. I apologize if this has been asked, but I could not find an answer regarding a transparent mask.

Answers (1)

Try this instead:
function imgDat = maskImage(imgDat, maskIdx)
maskIdx = repmat(maskIdx,[1,1,3]);
imgDat(~maskIdx) = imgDat(~maskIdx) * 0.3;

2 Comments

Thanks! That worked like a charm!
Glad to help. You can also Accept answers that solve your question... this is always appreciated :)

Sign in to comment.

Products

Asked:

on 14 Feb 2015

Commented:

on 14 Feb 2015

Community Treasure Hunt

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

Start Hunting!