how to mask green pixels
Show older comments
i wanted to mask green pixels of an image...
that is...
if the green component of pixel intenties is less than the compute threshold value
then, clear red, green, blue components of this pixel...
and then delete both pixel with zeros components and pixel on the boundaries....
i calculated the threshold... now i' m stuck with the masking part.... please could somebody help me to solve it...
1 Comment
Jan
on 19 Nov 2012
Please post, what you have tried so far. It is much easier to improve an existing code and fix problems, than to create from the scratch.
Accepted Answer
More Answers (1)
Image Analyst
on 21 Nov 2012
Try this:
clc;
clearvars;
close all;
imtool close all; % Close all imtool figures.
workspace;
format longg;
format compact;
fontSize = 20;
% Read in a standard MATLAB color demo image.
folder = fullfile(matlabroot, '\toolbox\images\imdemos');
baseFileName = 'peppers.png';
% 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;
end
end
rgbImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows columns numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 3, 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);
% Display
subplot(2, 3, 2);
imshow(redChannel, []);
title('Red Channel Image', 'FontSize', fontSize);
subplot(2, 3, 3);
imshow(greenChannel, []);
title('Green Channel Image', 'FontSize', fontSize);
subplot(2, 3, 4);
imshow(blueChannel, []);
title('Blue Channel Image', 'FontSize', fontSize);
% Get a mask based on the green threshold
greenThreshold = 70; % whatever...
mask = greenChannel < greenThreshold;
subplot(2, 3, 5);
imshow(mask, []);
title('Mask Image', 'FontSize', fontSize);
maskedRed = redChannel; % Initialize
maskedGreen = greenChannel; % Initialize
maskedBlue = blueChannel; % Initialize
% Mask
maskedRed(mask) = 0; % Initialize
maskedGreen(mask) = 255; % Initialize
maskedBlue(mask) = 0; % Initialize
maskedRgbImage = cat(3, maskedRed, maskedGreen, maskedBlue);
subplot(2, 3, 6);
imshow(maskedRgbImage);
title('Masked RGB Image', 'FontSize', fontSize);
4 Comments
Image Analyst
on 21 Nov 2012
What does that mean? You can't just get rid of pixels that are zero because the image must remain rectangular if it's to remain an image. If you just want a 1D list of non-zero pixel values, then you can get that:
nonZeroPixels = yourImage(yourImage ~=0);
but I don't know what good that would do you.
Image Analyst
on 26 Nov 2012
It was arbitrary. You can use 0.56*255 or 0.45*255 or whatever you want.
Image Analyst
on 26 Nov 2012
In other words, you can use 142.8 or 114.75 or whatever you want.
Elysi Cochin
on 26 Nov 2012
Categories
Find more on Red in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!