how to replace the specific color in the image

1 view (last 30 days)
Muhammad
Muhammad on 28 Apr 2022
Commented: DGM on 28 Apr 2022
i want to change the specific color define by the user with the color of user choice
i think for this i need some modification in this code
originalImage = imread('coloredChips.png'); % Load original ColoredChips.png image
[rows, columns, channels] = size(originalImage); % Get the dimensions of the image
colormode=input('enter the specific color ','s')
color=input('enter the new color','s')
if channels~=3
error('the image needs to be RGB')
end
% Display the original images
subplot(2,1,1);
imshow(originalImage);
title('orignal image')
% loops are unnecessary
% your mask does not depend on color selection
% and your color selection does not select what you think it selects
% these masks (very) roughly select the chips in the image
maskR = originalImage(:,:,1) > 200 & originalImage(:,:,2) < 100 & originalImage(:,:,3) < 100;
maskG = originalImage(:,:,1) < 50 & originalImage(:,:,2) > 100 & originalImage(:,:,3) < 150;
maskB = originalImage(:,:,1) < 10 & originalImage(:,:,2) < 100 & originalImage(:,:,3) > 220;
maskY= originalImage(:,:,1) >200 & originalImage(:,:,2) > 200 & originalImage(:,:,3) < 130;
% i'm not dealing with a tedious prompt. feel free to change
% colormode = 'red';
% you can also specify a color other than black
% rgb=rgb('purple');
newcolor = [100.5469 25.5938 25.5938];
outpict = originalImage;
switch lower(colormode)
case 'red'
selectedmask = repmat(maskR,[1 1 3]);
case 'green'
selectedmask = repmat(maskG,[1 1 3]);
case 'blue'
selectedmask = repmat(maskB,[1 1 3]);
case 'yellow'
selectedmask = repmat(maskY,[1 1 3]);
otherwise
error('ha ha you typed the wrong thing and got an error')
end
outpict(selectedmask) = 0;
outpict = outpict + uint8(selectedmask.*permute(newcolor,[1 3 2]));
% of course, that depends on the class of the input image
subplot(2,1,2);
imshow(outpict);
title('image with specific color black')
  1 Comment
DGM
DGM on 28 Apr 2022
Duplicate of:
If you want to notify specific users to get their attention, you can do so on the prior post. Creating duplicate posts with variable amounts of context only divides attention and multiplies the group effort required to understand the scope and history of the problem and its discussion.

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!