Creating a "mask" over an image

40 views (last 30 days)
Feynman
Feynman on 8 Oct 2013
Commented: Walter Roberson on 12 May 2017
I am given a project using matlab. With an image I am supposed to separate the RGB of the image into 3 2 dimensional matrices. This is the current code that I have used
r = f1(:,:,1); %assigns first color layer to variable r
g = f1(:,:,2); %assigns second color layer to variable g
b = f1(:,:,3); %assigns third color layer to variable b
The next step of the project is to use the matrix set for green values and by using only implicit commands create a new matrix that contains indices for green that are larger than the intensity threshold that is assigned by the user when asked. The matrix should be "number of pixels" x "number of pixels". How would you do this? This is the current method I am using, is it correct?
m1 = g>=n;
figure(3)
image(m1)
Furthermore, I am suppose to create a new matrix with "number of pixels" x "number of pixels" x 3 where the matrix created with the intensity threshold is duplicated along the 3 dimensions and generates a "mask" that tells whether or not the RGB content at any pixel met the given value. How would you do this? I have no code thus far for it.
  2 Comments
Bhumika Patel
Bhumika Patel on 12 May 2017
how to explain how the mask is created??? please answer me the explantion of creating the mask.
Walter Roberson
Walter Roberson on 12 May 2017
Bhumika Patel:
There is no one way of creating masks.
One of the typical ways of creating a mask is to compute the value of some property for each location in the image, and then use comparisons to determine whether the value of that property is in some range.
For example, you might start with an RGB image, and convert it to HSV (Hue, Saturation, Value), and then check to see whether the Hue is within some particular range that corresponds to (for example) orange.
Or you might decide to find the maximum value in a data array, and compute the Euclidean distance from each pixel in the array to the location of the maximum value, and then check to see whether the euclidean distance is within some particular range -- this would correspond to taking a circle around the maximum location.
There are a lot of other ways to compute masks. For example, you can have a mask computed by watershed analysis, or by texture analysis, or by correlation with a known shape, or by circle finding...

Sign in to comment.

Accepted Answer

Image Analyst
Image Analyst on 8 Oct 2013
See these snippets:
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Create the mask
mask = greenChannel > n;
% Mask image must be converted to the same integer type
% as the integer image we want to mask.
mask = cast(binaryImage, class(rgbImage));
% Masking method #1:
% 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);
% Masking method #2:
% An alternate method to multiplication channel by channel.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask, class(rgbImage)));

More Answers (1)

Matt J
Matt J on 8 Oct 2013
cat(3,m1,m1,m1)
or
repmat(m1,1,1,3);

Community Treasure Hunt

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

Start Hunting!