Change specific colour in an image

27 ビュー (過去 30 日間)
Phat
Phat 2024 年 3 月 28 日
コメント済み: Dyuman Joshi 2024 年 3 月 30 日 4:11
Hi everybody
I have to change the colors of an object in a picture but when i change red to black, it also change the orange colour. Can you help me solve this please ?
a = imread('coloredChips.png');
figure(1), imshow(a);
% Creat menu of color
disp('Color selection')
disp('r for red')
disp('b for blue')
disp('y for yellow')
% Now we want input from user to tell us which color to change
x = input(' Please select the color you want to change: ','s');
copy = a;
dimension = size(a);
row_n = dimension (1);
col_n = dimension (2);
switch x
case 'r'
for row = 1:row_n
for col = 1:col_n
pixel_red = a(row,col,1);
pixel_green = a(row,col,2);
pixel_blue = a(row,col,3);
if (pixel_red >= 200) && (pixel_green <= 50) && (pixel_green >= 5) && (pixel_blue <= 80) && (pixel_blue >=15)
copy(row,col,1) = 0;
copy(row,col,2) = 0;
copy(row,col,3) = 0;
end
end
end
otherwise
disp('Incorrect selection, please choose from the menu');
end
% display the modified image
figure(2),imshow(copy);
  6 件のコメント
DGM
DGM 2024 年 3 月 28 日 13:58
編集済み: DGM 2024 年 3 月 28 日 14:04
coloredChips.png is a standard image that comes with MATLAB.
which coloredChips.png
/MATLAB/toolbox/images/imdata/coloredChips.png
The copy that OP attached is a screenshot of the original, which has been resized, padded, and converted to a degraded indexed-color copy.
The proposal to use JPG will only make the damage worse. To expand upon the comment I gave below:
% read the original image
goodpict = im2double(imread('coloredChips.png'));
% read the screenshot and try to fix it
[indpict map] = imread('screenshot.png');
badpict = ind2rgb(indpict,map); % convert to RGB
imwrite(badpict,'worsescreenshot.jpg'); % convert to a trash-quality JPG
badpict = im2double(imread('worsescreenshot.jpg'));
badpict = imcrop(badpict,[90.51 29.51 517.98 390.98]); % crop off the padding
badpict = imresize(badpict,size(goodpict,1:2)); % return to original size
% the error is strongest near edges
err = normalize(abs(goodpict-badpict),'range');
imshow(err,'border','tight')
I'm not showing it here, but MSE only tripled by using JPG. That might almost seem like a small change, but look at the apparent resolution of the error map. The image saved by MATLAB is a 75% 4:2:0 JPG. You're throwing away 75% of your chroma information, when your task is critically dependent on using that chroma information.
Using JPG as part of an image processing routine is never the answer.
Dyuman Joshi
Dyuman Joshi 2024 年 3 月 30 日 4:11
"coloredChips.png is a standard image that comes with MATLAB."
I was not aware about this. Thanks for the info!

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

回答 (2 件)

DGM
DGM 2024 年 3 月 28 日 13:40
移動済み: DGM 2024 年 3 月 28 日 14:07
See also:
While this same assignment has been answered before, this is probably the first time that someone tried doing it with an indexed-color screenshot of the test image. The version of the file that comes with MATLAB is already RGB.
inpict = imread('coloredChips.png');
size(inpict) % you should already have a clean RGB copy
ans = 1×3
391 518 3
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
This is worth pointing out, since I see no point in using a degraded screenshot when you already have the original image.
% read the original image
goodpict = im2double(imread('coloredChips.png'));
% read the screenshot and try to fix it
[indpict map] = imread('screenshot.png');
badpict = ind2rgb(indpict,map); % convert to RGB
badpict = imcrop(badpict,[90.51 29.51 517.98 390.98]); % crop off the padding
badpict = imresize(badpict,size(goodpict,1:2)); % return to original size
% the error is strongest near edges
err = normalize(abs(goodpict-badpict),'range');
imshow(err,'border','tight')

Pramil
Pramil 2024 年 3 月 28 日 10:31
編集済み: Pramil 2024 年 3 月 28 日 10:32
Hi Phat,
What you are doing is correct, but in MATLAB pixel values are normalized that means each pixel ranges from “0” to “1” and not from “0” to “255” as you can see in the following link :
When extracting individual pixel, multiply each value with “255” and your code works just fine.
Here is an updated code that works fine in MATLAB R2023b:
[img, map] = imread('coloredChips.png');
a = ind2rgb(img, map);
if size(a, 3) ~= 3
error('The image must be an RGB image.');
end
figure(1), imshow(a);
% Creat menu of color
disp('Color selection')
Color selection
disp('r for red')
r for red
disp('b for blue')
b for blue
disp('y for yellow')
y for yellow
% Now we want input from user to tell us which color to change
%x = input(' Please select the color you want to change: ','s');
x = 'r';
copy = a;
dimension = size(a);
row_n = dimension (1);
col_n = dimension (2);
switch x
case 'r'
for row = 1:row_n
for col = 1:col_n
pixel_red = a(row,col,1)*255;
pixel_green = a(row,col,2)*255;
pixel_blue = a(row,col,3)*255;
if (pixel_red >= 200) && (pixel_green <= 50) && (pixel_green >= 5) && (pixel_blue <= 80) && (pixel_blue >=15)
copy(row,col,1) = 0;
copy(row,col,2) = 0;
copy(row,col,3) = 0;
end
end
end
otherwise
disp('Incorrect selection, please choose from the menu');
end
% display the modified image
figure(2),imshow(copy);
Hope this helps.

カテゴリ

Help Center および File ExchangeImage Processing Toolbox についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by