MATLAB: Remove pixels with certain rgb values

Image Processing Toolboxmaskmaskingpixel

Hi, I want to remove pixels with R value >100 and G value>100, here is my code:
[height width rgb] = size (I); % I is RGB image
for coloum = 1:width
for row =1:height
if (I(row,coloum,1)> 100 && I(row,coloum,2)> 100)
I(row,coloum,1)= 0;
I(row,coloum,2)= 0;
I(row,coloum,3)= 0;
end
end
The error is "Index exceeds matrix dimensions". Does anyone have an idea about this? Thanks in advance.

Best Answer

Instead of all your code (nested for loops), try this vectorized approach:
mask = (I(:, :, 1) > 100) & (I(:, :, 2) > 100);
% Mask the image using bsxfun() function to multiply the mask by each channel individually.
maskedRgbImage = bsxfun(@times, I, cast(mask, 'like', I));
Also, I very much recommend you don't name your variable I because it looks too much like l (lower case L) and 1 (one).