0

let us consider following image enter image description here

i have found one topic about how to keep one of the RGB color(for instance red) and remove all others, here is code in matlab which does the same task

   I=imread('fruit.jpg');
m=size(I,1);
n=size(I,2);
for mm=1:m
for nn=1:n
if I(mm,nn,1)<80 || I(mm,nn,2)>80 || I(mm,nn,3)>100
gsc=0.3*I(mm,nn,1)+0.59*I(mm,nn,2)+0.11*I(mm,nn,3);
I(mm,nn,:)=[gsc gsc gsc];
end
end
end
imshow(I);

after running, i have got following result enter image description here

result seems ok , but is that only way i can change colors? can i do it without loops?what i need is more intuitive way of implementing colors changing

1 Answers1

1

You should be using matrix operations as in the Matlab documentation here: https://www.mathworks.com/help/matlab/matlab_prog/find-array-elements-that-meet-a-condition.html

I = rand(5,5,3).*256;
Red = I(:,:,1)>=80 & I(:,:,2)<=80 & I(:,:,3)<100;
Red = [Red,Red,Red];
NotRed = !Red;
Gsc = I(:,:,1)*0.3 + I(:,:,2)*0.59 + I(:,:,3)*0.11;
I(NotRed) = [Gsc;Gsc;Gsc];
Gabi Lee
  • 1,193
  • 8
  • 13