remove pixels with certain rgb values

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.

 採用された回答

Image Analyst
Image Analyst 2018 年 2 月 17 日

7 投票

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).

5 件のコメント

Tian Tian
Tian Tian 2018 年 2 月 17 日
Thank you it works!
Image Analyst
Image Analyst 2018 年 2 月 17 日
You're welcome. Since we're done, then can you "Accept this answer" and vote for it?
Perry Taneja
Perry Taneja 2018 年 7 月 9 日
What do you mean by removing? What value will say a pixel with value "101" acquire after you remove it?
Image Analyst
Image Analyst 2018 年 7 月 9 日
In my example, pixels with colors meeting the criteria (red > 100 and green > 200) will be "true" in the mask and therefore will be set to pure black (0,0,0).
A pixel with color of (101, 101, 101) will remain unchanged. Why? It's not in the mask because it's green value was only 101 (not greater than 200). Only the red channel met the criteria but the criteria required that both red and green meet the criteria to be considered as part of the mask.
Jona Boender
Jona Boender 2020 年 4 月 28 日
This worked great, thank you!

サインインしてコメントする。

その他の回答 (1 件)

Jos (10584)
Jos (10584) 2018 年 2 月 16 日

1 投票

Is I really a 3D array or is the third dimension just 1?
Simply take a look at the value of rgb, after the size(I) command :)

6 件のコメント

Tian Tian
Tian Tian 2018 年 2 月 16 日
Thank you for comments. It's actually a 2D image, 2D array then. The 3rd dimension 1 2 3 is for rgb color.I am a little confused about the dimension and the rgb size, is rgb from 0-255 for each color channel? May you please explain my code problem? Thanks a lot.
Jos (10584)
Jos (10584) 2018 年 2 月 16 日
There is a difference between indexed images (2D, like yours) and true colour images (3D, with RGB triplets in the 3rd dimension). This is explained here:
Tian Tian
Tian Tian 2018 年 2 月 16 日
Hi Jos, thanks for the information. I think mine is true color image, I said '2D' because of width and height of the image (physical dimension).My code was also based the link that you sent to me: pixel (2,3,1) where the last 1 is the color. Do you know where is the problem? Thank you in advance.
Jos (10584)
Jos (10584) 2018 年 2 月 16 日
What does size(I) return?
What does a small portion of I, like I(1:4,1:4,:) contain?
Tian Tian
Tian Tian 2018 年 2 月 17 日
Also, I tried 'colormap', and got the error: 'Colormap must have 3 columns: [R,G,B].'. So it is not an indexed image.
Tian Tian
Tian Tian 2018 年 2 月 17 日
Thank you I imported a wrong image that's why. Thanks a lot for your help!!

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by