change color of an object

41 ビュー (過去 30 日間)
corsairs
corsairs 2019 年 11 月 30 日
コメント済み: Adam Danz 2022 年 5 月 31 日
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 !
  4 件のコメント
Adam Danz
Adam Danz 2019 年 11 月 30 日
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
corsairs 2019 年 11 月 30 日
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
clc
I=imread('stawberry.jpg');
%% RGB
R=I(:,:,1);
G=I(:,:,2);
B=I(:,:,3);
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');

サインインしてコメントする。

採用された回答

Adam Danz
Adam Danz 2019 年 11 月 30 日
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)
I=imread('strawberry.jpeg');
% 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;
figure(1)
imshow(I_temp)
% 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.
figure(3)
subplot(1,2,1)
imshow(I)
title('Original')
subplot(1,2,2)
imshow(I_new)
title('Pinker')
191130 170146-Figure 3.png
  8 件のコメント
DGM
DGM 2022 年 5 月 29 日
Extending the example to masking and adjustment in HSV:
% Load image (file name changed)
rgbpict = imread('sberry.jpg');
% Look for reddish colors which are relatively vibrant.
% Play around with the thresholds until you get what you want.
% [Hmin Hmax; Smin Smax; Vmin Vmax]
% yes, Hmin is higher than Hmax for red. H is circular
thresholds = [0.9 0.06; 0.5 1; 0 1];
hsvpict = rgb2hsv(rgbpict);
isRed = all(hsvpict >= permute(thresholds(:,1),[2 3 1]),3) ...
| all(hsvpict <= permute(thresholds(:,2),[2 3 1]),3);
% despeckle the mask
isRed = bwareaopen(isRed,50); % remove small objects
isRed = ~bwareaopen(~isRed,50); % remove small holes
% expand to simplify addressing later
isRedArray = repmat(isRed,1,1,3);
% use imfuse() to highlight the areas of the image which are selected by the mask
figure(1)
imshow(imfuse(rgbpict,isRed,'blend'))
% Once you're happy with your thresholds above, adjust the image content.
hsvpict(:,:,1) = mod(hsvpict(:,:,1)-0.05,1); % shift hue toward blue
hsvpict(:,:,2) = 0.8*hsvpict(:,:,2); % reduce saturation
hsvpict(:,:,3) = hsvpict(:,:,3)*1.3; % increase V "exposure"
adjpict = hsv2rgb(hsvpict);
% combine the adjusted image and the original using logical composition
% images must be the same class and scale
outpict = rgbpict;
outpict(isRedArray) = im2uint8(adjpict(isRedArray));
% Compare the original and new image.
figure(3)
subplot(1,2,1)
imshow(rgbpict)
title('Original')
subplot(1,2,2)
imshow(outpict)
title('Pinker')
... I guess it depends what pink strawberries are supposed to look like.
This example does the color adjustment and composition using base MATLAB tools. There are more flexible, robust, and convenient approaches, and there are alternative interpretations of what "change the color of an object" might mean. This other answer covers some of those options/alternatives.
Adam Danz
Adam Danz 2022 年 5 月 31 日
Thanks @DGM, smart to use HSV space!

サインインしてコメントする。

その他の回答 (2 件)

KALYAN ACHARJYA
KALYAN ACHARJYA 2019 年 11 月 30 日
"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.
I=imread('stawberry.jpeg');
%% RGB
I(:,:,2)=I(:,:,2)-30;
I(:,:,1)=I(:,:,1)+50;
I(:,:,3)=I(:,:,1)-50;
imshow(I)
  2 件のコメント
corsairs
corsairs 2019 年 11 月 30 日
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 2019 年 11 月 30 日
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

サインインしてコメントする。


Image Analyst
Image Analyst 2019 年 12 月 1 日
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.

カテゴリ

Help Center および File ExchangeOrange についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by