Filter löschen
Filter löschen

how to mask green pixels

1 Ansicht (letzte 30 Tage)
Elysi Cochin
Elysi Cochin am 19 Nov. 2012
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 Kommentar
Jan
Jan am 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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 19 Nov. 2012
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 Kommentare
Elysi Cochin
Elysi Cochin am 21 Nov. 2012
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??
Image Analyst
Image Analyst am 21 Nov. 2012
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]));

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Image Analyst
Image Analyst am 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 Kommentare
Image Analyst
Image Analyst am 26 Nov. 2012
In other words, you can use 142.8 or 114.75 or whatever you want.
Elysi Cochin
Elysi Cochin am 26 Nov. 2012
thank u sir....

Melden Sie sich an, um zu kommentieren.

Community Treasure Hunt

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

Start Hunting!

Translated by