MATLAB Answers

How can i Change pixel color?

40 views (last 30 days)
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.


Show 2 older comments
Mehmed Saad
Mehmed Saad on 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 on 7 Jul 2020
Better replace loop by logical indexing and upload an example image.
Mehmed Saad
Mehmed Saad on 8 Jul 2020
jonas is right, understand the code and try to replace for loops with logical indexing

Sign in to comment.

Accepted Answer

Mehmed Saad
Mehmed Saad on 7 Jul 2020
Edited: Mehmed Saad on 8 Jul 2020
I = imread('red.jpg');
th = 20;
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;
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


Show 1 older comment
Mehmed Saad
Mehmed Saad on 10 Jul 2020
Thanks a lot Sir.
Ansam Nazar
Ansam Nazar on 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'."
Image Analyst
Image Analyst on 12 Jul 2020
You can do it manually:
r = rgbImage(:, :, 1);
g = rgbImage(:, :, 2);
b = rgbImage(:, :, 3);

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!