mask = redChannel <= 20 & greenChannel <= 20 & blueChannel <= 20; this statement is in the test.m programe so any one can explain me why we use this line code i attached the code
MATLAB: Can any one tell me what is the meaning of mask = redChannel <= 20 & greenChannel <= 20 & blueChannel <= 20;
Related Solutions
It definitely DOES work, but you need to make your mask in the range 0-1. Here's proof:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;workspace; % Make sure the workspace panel is showing.
fontSize = 20;format compact;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'peppers.png';fullFileName = fullfile(folder, baseFileName);% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage, []);title('Original Color Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]); % Maximize figure.
mask = zeros(rows, columns, 'uint8');mask(30:300, 50:150) = 1;% Normalize to 0 - 1.
mask = mask / 255;% Display the mask.
subplot(2, 2, 2);imshow(mask, []);title('Mask', 'FontSize', fontSize);maskedRgbImage = bsxfun(@times, rgbImage, cast(mask,class(rgbImage)));% Display the mask.subplot(2, 2, 3);imshow(maskedRgbImage, []);title('MaskedImage', 'FontSize', fontSize);
Is one of these what you are looking for? Walter's right about the ambiguity so I picked a couple of possibilities (grayscale changing the image and not using a colormap, and RGB thresholding each channel individually) and programmed up a demo for you. If neither case is what you want, then you must clarify. Don't worry about the length, just copy, paste, and run, then study the code later.
clc; % Clear command window.
clear; % Delete all variables.
close all; % Close all figure windows except those created by imtool.
imtool close all; % Close all figure windows created by imtool.
workspace; % Make sure the workspace panel is showing.
fontSize = 16;% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'football.jpg';% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);if ~exist(fullFileName, 'file') % Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file') % Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName); uiwait(warndlg(errorMessage)); return; endendrgbImage = imread(fullFileName);% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);% Display the original color image.
subplot(2, 2, 1);imshow(rgbImage, []);title('Original Color Image', 'FontSize', fontSize);% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);greenChannel = rgbImage(:, :, 2);blueChannel = rgbImage(:, :, 3);% Threshold each color channel to get pixels darker than 40.
redMask = redChannel < 40;greenMask = greenChannel < 40;blueMask = blueChannel < 40;% Apply the mask
redChannel(redMask) = 255;greenChannel(greenMask) = 0;blueChannel(blueMask) = 0;% Recombine into RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);% Display
subplot(2, 2, 2);imshow(rgbImage, []);title('Masked Color Image', 'FontSize', fontSize);promptMessage = sprintf('Now we will do it for a gray scale image?');titleBarCaption = 'Continue with gray scale example?';button = questdlg(promptMessage, titleBarCaption, 'Continue', 'Cancel', 'Continue');if strcmp(button, 'Cancel') return;end% Read in a standard MATLAB gray scale demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');baseFileName = 'cameraman.tif';% Get the full filename, with path prepended.fullFileName = fullfile(folder, baseFileName);% Check if file exists.
if ~exist(fullFileName, 'file') % File doesn't exist -- didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time. if ~exist(fullFileName, 'file') % Still didn't find it. Alert user. errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName); uiwait(warndlg(errorMessage)); return; endendgrayImage = imread(fullFileName);% Get the dimensions of the image.
% numberOfColorBands should be = 1.
[rows columns numberOfColorBands] = size(grayImage);% Display the original gray scale image.
subplot(2, 2, 3);imshow(grayImage, []);title('Original Grayscale Image', 'FontSize', fontSize);% Get red, green, and blue channels.
redChannel = grayImage;greenChannel = grayImage;blueChannel = grayImage;% Threshold each color channel to get pixels darker than 40.mask = grayImage < 40;% Apply the maskredChannel(mask) = 255;greenChannel(mask) = 0;blueChannel(mask) = 0;% Recombine into RGB image.rgbImage = cat(3, redChannel, greenChannel, blueChannel);% Displaysubplot(2, 2, 4);imshow(rgbImage, []);title('Masked Color Image', 'FontSize', fontSize);
Best Answer