Masking certain RGB values for an image

Hello. I was wondering if there is a way to mask/delete certain pixels in an image if you know the range of RGB values you want to remove. Thanks for any help, Olly

 Akzeptierte Antwort

Jan
Jan am 26 Sep. 2011

0 Stimmen

Yes.
RGB = rand(400, 200, 3);
R = RGB(:, :, 1);
G = RGB(:, :, 2);
B = RGB(:, :, 3);
mask2D = (0.2 < R & R < 0.3) & ... % [EDITED]: && -> &
(0.1 < G & G < 0.4) & ...
(0.6 < B & B < 0.65);
mask = cat(3, mask2D, mask2D, mask2D);
RGB(mask) = 0;
I do not know how a pixel could be deleted without destroying the shape of the image.

4 Kommentare

Andrei Bobrov
Andrei Bobrov am 26 Sep. 2011
Hi Jan!
Can so?
&& -> &
Jan
Jan am 26 Sep. 2011
@Andrei: Thanks, fixed. I'm using a C-mex for that, which short-circuits: no test of G and B components, if R does not match, etc. I expect that such a function is included in the Image Processing Toolbox.
Image Analyst
Image Analyst am 8 Okt. 2011
An alternate way if you want to use mask2D and not create the 3D mask:
% Mask the image.
maskedRgbImage = bsxfun(@times, rgbImage, cast(mask2D, class(rgbImage)));
In the end, it doesn't make any difference though - this is just the style that Sean prefers.
Walter Roberson
Walter Roberson am 8 Okt. 2011
Hmmm, which is faster, cat(3,X,X,X) or repmat(X,1,1,3) ?
Logically cat(3) should be, but I can't test at the moment.

Melden Sie sich an, um zu kommentieren.

Tags

Gefragt:

am 26 Sep. 2011

Community Treasure Hunt

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

Start Hunting!

Translated by