how to mask green pixels

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
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.

Sign in to comment.

 Accepted Answer

tG = IM(:,:,2) > GreenThreshold;
newIM = IM .* repmat(tG,[1 1 3]);
You need to figure out the deletion part yourself, as there might be irregular patterns of pixels with value (0,0,0) and MATLAB does not support arrays with "holes" in them.

4 Comments

Elysi Cochin
Elysi Cochin on 21 Nov 2012
Edited: Elysi Cochin on 21 Nov 2012
sir few doubts what does the variables stand for...
is GreenThreshold the threshold of the full image??
what does IM stand for??
GreenThreshold refers to the threshold value you mentioned in the sentence "if the green component of pixel intenties is less than the compute threshold value"
"IM" stands for "image". As in your RGB image array.
sir.. but when i run it this error is coming
??? Error using ==> times Integers can only be combined with integers of the same class, or scalar doubles.
Error in ==> Untitled3 at 31 newIM = IM .* repmat(tG,[1 1 3]);
why that error??
IM and the results of repmat are different classes and that's not allowed. IM is probably uint8, and tg is a logical, so repmat(tg) is also a logical. You could do it this way:
newIM = IM .* uint8(repmat(tG,[1 1 3]));

Sign in to comment.

More Answers (1)

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

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.
It was arbitrary. You can use 0.56*255 or 0.45*255 or whatever you want.
In other words, you can use 142.8 or 114.75 or whatever you want.
thank u sir....

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!