필터 지우기
필터 지우기

remove pixels with certain rgb values

조회 수: 4 (최근 30일)
Tian Tian
Tian Tian 2018년 2월 15일
댓글: Jona Boender 2020년 4월 28일
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일
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
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일
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월 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