MATLAB Answers

change color of an object

23 views (last 30 days)
corsairs on 30 Nov 2019
Edited: Adam Danz on 1 Dec 2019
Hi everybody
I have to change the colors of an object in a picture but I don't know how to do that. Can you help me please ?
Thanks a lot !


Show 1 older comment
corsairs on 30 Nov 2019
Yes in fact I'm sorry. When I said an object, it's like on that picture the strawberry, let's me explain : I have to change the color of the strawberry (for example I have to optain pink strawberry) without changing the strawberry tail color. So I need to "play" on the RGB of the stawberry pixels but I don't know how to do that.
Adam Danz
Adam Danz on 30 Nov 2019
You'll need to read in the image. Then you'll need to extract the color values of each pixel. Then you can locate pixels that are within a certain threshold of red. That will give you a logical array identifying all pixesl that are red-ish. Then you can rescale their color value.
Since you're asking how to rescale the color value I assume you've already gotten started on the other stuff. So, how far are you along the way? What do you have so far?
corsairs on 30 Nov 2019
yeah in fact, I do that but I don't know if it's the good start and next how to lacate pixels that are within a certain threshold of red
clear all
close all
%% RGB
figure; colormap('gray');
subplot(2,5,1); imagesc(I, [0 255]); axis off; title('Image','FontSize',8);
subplot(2,5,2); image(R); title('plan R');
subplot(2,5,3); image(G); title('plan G');
subplot(2,5,4); image(B); title('plan B');

Sign in to comment.

Accepted Answer

Adam Danz
Adam Danz on 30 Nov 2019
Here are the steps taken in the code below. There may be a better approach but this should get you started. See inline comments for more detail.
  1. Isolate pixels that are red-ish
  2. confirm that you've done a decent job with that selection
  3. tweek the colors of the selected pixels only.
% Load image (file name changed)
% Look for red-ish value. Play around with this to get what you want.
% Play around with these 3 thresholds until you get what you want.
isRed = I(:,:,1)>100 & I(:,:,2)<150 & I(:,:,3)<150;
isRedArray = repmat(isRed,1,1,3);
% Replace red-ish values with black just to see how well your thresholds work
I_temp = I;
I_temp(isRedArray) = 0;
% Once you're happy with your thresholds above, rescale the selected RGB values
% toward white ([255,255,255]). There may be a better approach such as scaling
% the rgb values separately.
I_new = I;
I_new(isRedArray) = I_new(isRedArray)+80;
% Compare the original and new image.
191130 170146-Figure 3.png


Show 3 older comments
corsairs on 30 Nov 2019
I_new = I;
I_new(isRedArraynew) = I_new(isRedArraynew)+0;
I tried something like that to change the red color into apple green color but it doesn't work and I don't have idea otherwise
Adam Danz
Adam Danz on 1 Dec 2019
isRedArray is a logical array (0/1) that identifies the pixels that are red-ish. Adding values to them destroys their purpose.
Take time look at the variables and the code to understand what they represent and what they are doing. Unfortunately it's very common for people to copy-paste answers into their code without know what the code is doing. I'd be glad to answer questions if some part of the code is not clear.
To create a logical array that only effects the Red, Green, or Blue values,
isRed = I(:,:,1)>100 & I(:,:,2)<150 & I(:,:,3)<150;
zeroArray = zeros(size(isRed));
isRed_R = cat(3,isRed,zeroArray,zeroArray);
isRed_G = cat(3,zeroArray,isRed,zeroArray);
isRed_B = cat(3,zeroArray,zeroArray,isRed);
Take a few minutes to explore those 3 arrays. Which layers have 1s and which layers are all 0s? Use them to make changes to the R, G, or B values like this
I_new(isRed_R) = I_new(isRed_R)+10; % only affects the R layer of the RGB values.
Adam Danz
Adam Danz on 1 Dec 2019
@corsairs, the answer provided by Image Analyst is a better approach (after all, he is the imaging expert here). Consider his approach and if that works for you, it may be a good idea to accept his answer so future visitors can more quickly see the best appraoch.
Another answer by Image Analyst shows how to pull the hsv values from the image:
hsv = rgb2hsv(rgbImage);
h = hsv(:, :, 1); % Hue image.
s = hsv(:, :, 2); % Saturation image.
v = hsv(:, :, 3); % Value (intensity) image.

Sign in to comment.

More Answers (2)

KALYAN ACHARJYA on 30 Nov 2019
"change color of an object" or "for example I have to optain pink strawberry"
Please change those values, you will get the different colours. Also requested to read about color models (Gonzalez Book Image Processing). Also note that pink is a lighter side hue of red colour. R,G,B are primary colors, and any color produvce by generating by mixing of R,G and B are known as secondary colors.
%% RGB


corsairs on 30 Nov 2019
Thanks a lot for your answer. I try your code but my issue is that I don't want to change the value of all the pixel but just the pixel red. When I said pink, it was just an example but for an easier exercice we can try to obtain green strawberry
Adam Danz
Adam Danz on 30 Nov 2019
The problem with this approach is that it scales all colors, not just the red-ish ones (even the picture boundary is a shade of pink). But nice shade of pink, for sure!! ;)
191130 170321-Figure 1.png

Sign in to comment.

Image Analyst
Image Analyst on 1 Dec 2019
What I would do to change it from red to pink is to identify the red regions in HSV color space. Then multiply or subtract a value from the saturation channel only. Then go back to RGB color space.
To change the color, like from red to blue, add about .5 or so to the red pixels only. Let me know if you need a demo.


Sign in to comment.

Sign in to answer this question.