Hello
I have a color image of type RGB and I need to change the color of each point (red, yellow, orange) to green. I hope that there is a programmatic solution to this problem.

5 Kommentare

jonas
jonas am 7 Jul. 2020
What do you mean "of each point"? Better upload an example image and explain what type of output you're looking for.
Ansam Nazar
Ansam Nazar am 7 Jul. 2020
I mean I need amatlab code to convert every red or yellow or orange pixel into green pixel.
Mehmed Saad
Mehmed Saad am 7 Jul. 2020
Bearbeitet: Mehmed Saad am 7 Jul. 2020
Suppose i have a picture of size 400x400. When i import it in MATLAB (using imread or directly) the matrix size will be 400x400x3 uint8. Now the 3rd dimension depicts R,G and B. Now you can apply conditions on these three colors for 1 pixel and change there values as you want
For example True red is [255 0 0], now apply a check that if this condition satisfies change pixel to
[0 255 0]
jonas
jonas am 7 Jul. 2020
Better replace loop by logical indexing and upload an example image.
Mehmed Saad
Mehmed Saad am 8 Jul. 2020
jonas is right, understand the code and try to replace for loops with logical indexing

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Mehmed Saad
Mehmed Saad am 7 Jul. 2020
Bearbeitet: Mehmed Saad am 8 Jul. 2020

1 Stimme

I = imread('red.jpg');
figure,subplot(121),imshow(I)
th = 20;
[x,y,~]=size(I);
for ii=1:x
for jj=1:y
if((I(ii,jj,1)-th)> I(ii,jj,2)&& (I(ii,jj,1)-th)>I(ii,jj,3))
I(ii,jj,1) = 0;I(ii,jj,2) = 255;I(ii,jj,3) = 0;
end
end
end
subplot(122),imshow(I)
For same effect you can try this
I(ii,jj,1:2) = circshift(I(ii,jj,1:2),1);
I(:,:,1:2) = circshift(I(:,:,1:2),1,3);%without for loop

4 Kommentare

Looks like you switched x and y. Usually it's good not to go against well entrenched naming conventions or else confusion will result. Remember, arrays are indexed (row, column) which is (y, x), not (x,y).
Also you can do it without loops like this:
rgbImage = imread('peppers.png');
subplot(2, 2, 1);
imshow(rgbImage);
title('Original Image', 'FontSize', 15);
% Split the RGB image into individual color channels.
[r, g, b] = imsplit(rgbImage);
% Define what is orange.
th = 20;
orangeMask = r > (g + th) & r > (b + th);
subplot(2, 2, 2);
imshow(orangeMask);
title('Orange Mask', 'FontSize', 15);
% Assign 0 to red and blue, and 255 to green to make those regions green.
r(orangeMask) = 0;
g(orangeMask) = 255;
b(orangeMask) = 0;
% Rebuild the RGB image from the individual color channels.
rgbImage = cat(3, r, g, b);
subplot(2, 2, 3);
imshow(rgbImage);
title('Altered Image', 'FontSize', 15);
Mehmed Saad
Mehmed Saad am 10 Jul. 2020
Thanks a lot Sir.
Ansam Nazar
Ansam Nazar am 12 Jul. 2020
Thanks alot for all of your help. but the function is not working in Matlab R2018a. the error was:
"Undefined function or variable 'imsplit'."
You can do it manually:
r = rgbImage(:, :, 1);
g = rgbImage(:, :, 2);
b = rgbImage(:, :, 3);

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by