MATLAB: Masking certain RGB values for an image

Image Processing Toolboxmaskingrgb

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

Best Answer

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.