How would you create a mask on an image?

I am doing a project and one of the tasks is to create a matrix with number of pixels x number of pixels x 3 as its size. In the previous task I created a matrix using the green container of the RGB image and I am suppose to duplicate this along the 3 dimensions. My question is what functions would I use in order to do this. I am not allowed to use any implicit functions only explicit.

6 Comments

Define implicit and explicit. Which are cat(), repmat(), bsxfun(), and class()? Walter and I used those in our Answers below. Are you allowed to use them?
Yes those I can use, I cannot use conditional statements or define a new function that I create. With the green container matrix that I made I was to replace any values of green that were smaller than the user was supposed to input a number 0 to 255 which I coded as
m1 = g>=n1;
will the code that you suggested be able to tell whether the green channel intensity was greater than the threshold?
That will get you a logical mask, m1, from which you can use it as the mask in my code below to mask out pixels brighter than the user specified value. You can set the values in any of the color channels to anything you want, e.g. 0 or 255, or anything else.
redChannel(m1) = 0; % Mask inside to %255 or whatever.
or
redChannel(~m1) = 0; % Mask outside.
Feynman
Feynman on 5 Oct 2013
Edited: Feynman on 5 Oct 2013
I need to keep the values that are greater than the value the user entered. Will the code you suggested keep the values greater than the one the user inputs?
I tried implementing the code you suggested and it keeps giving me an error of integers can only be combined with integers of the same class or scale doubles
mark and redChannel have to be the same type of integer like it said. So you'd need to do
maskedRed = redChannel .* uint8(mask);
if redChannel is a uint8.

Sign in to comment.

 Accepted Answer

Here's one method:
% Multiply the mask by each color channel individually.
maskedRed = redChannel .* mask;
maskedGreen = greenChannel .* mask;
maskedBlue = blueChannel .* mask;
% Recombine separate masked color channels into a single, true color RGB image.
maskedRgbImage = cat(3, maskedRed, maskedGreen, maskedBlue);
Here's another method, suggested by Sean D.
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));

More Answers (1)

Community Treasure Hunt

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

Start Hunting!