필터 지우기
필터 지우기

how to mask green pixels

조회 수: 1 (최근 30일)
Elysi Cochin
Elysi Cochin 2012년 11월 19일
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
Jan
Jan 2012년 11월 19일
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.

댓글을 달려면 로그인하십시오.

채택된 답변

Walter Roberson
Walter Roberson 2012년 11월 19일
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
Elysi Cochin
Elysi Cochin 2012년 11월 21일
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 2012년 11월 21일
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]));

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2012년 11월 21일
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
Image Analyst
Image Analyst 2012년 11월 26일
In other words, you can use 142.8 or 114.75 or whatever you want.
Elysi Cochin
Elysi Cochin 2012년 11월 26일
thank u sir....

댓글을 달려면 로그인하십시오.

Community Treasure Hunt

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

Start Hunting!

Translated by