how to change specific color define by the user to black in matlab?

5 views (last 30 days)
it is working for red ,blue ,green but not for yellow and orange
need modification but not now can any one help
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')
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) < 10;
% i'm not dealing with a tedious prompt. feel free to change
% colormode = 'red';
% you can also specify a color other than black
newcolor = [0 0 0];
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')

Answers (2)

Walter Roberson
Walter Roberson on 28 Apr 2022
maskY= originalImage(:,:,1) < 200 & originalImage(:,:,2) > 200 & originalImage(:,:,3) < 10;
That is incorrect. Look at
In that top swath, "Hazelnut" #BDA55D is the only shade of yellow in that swath that has red component < 200 (#C8) -- so that first test eliminates a lot of yellows. Including #FFFF00, bright yellow
If you scroll further down, a number of shades such as Honey #FFC30B have green component < 200
Honey also has blue component > 10 .

DGM
DGM on 28 Apr 2022
Edited: DGM on 28 Apr 2022
I answered a similar question earlier:
If you want yellow as well, then adapt it to include
rangeY = [0.153 0.188; 0.338 1; 0.000 1];
Both of these questions are based off the answer to this older question:
  2 Comments
Muhammad
Muhammad on 28 Apr 2022
yes but i want the user select the color which he want to change and also define a color that he want to be in repalce of that and then image change the color acordingly
DGM
DGM on 28 Apr 2022
Edited: DGM on 28 Apr 2022
If you must, you can use input() to get input interactively from the user. I think it's bad design practice, but I wouldn't be surprised if the assignment actually required you to write it that way. If I recall correctly, the original question used an input() statement as described.
If you want the user to also specify the replacement color, you'll have to include another input() statement to get that as well. You'll have to decide how the replacement color should be specified. If it's to be specified as a numeric tuple, you might be able to do:
newcolor = ''; % initialize
while ~isnumeric(newcolor) || numel(newcolor)~=3
newcolor = input('hey buddy, hey uh listen i hate to bother you right now but uh what color do you want me to make these things when i''m done? ');
end
If it's to be specified as a color name, you'll have to build a list of valid color names and a corresponding color table and then do something more elaborate.
outcolornames = {'black','white','red','yellow','green','cyan','blue','magenta'};
colors = [0 0 0;
1 1 1;
1 0 0;
1 1 0;
0 1 0;
0 1 1;
0 0 1;
1 0 1];
% pestering the user to manually type out every parameter value again and again is unnecessary, annoying, and a great way to collect typos
repcolorname = ''; % initialize
while ~ismember(lower(repcolorname),outcolornames)
repcolorname = input('hey buddy, hey uh listen i hate to bother you right now but uh what color do you want me to make these things when i''m done? ','s');
end
newcolor = im2uint8(colors(strcmp(repcolorname,outcolornames),:))
If you're going to use input() to get the mask color as well, you'll have to do something similar by making a list of valid mask colors and running input() in a loop until a valid input is obtained.
EDIT: forgot the demo was set up for uint8-scale colors

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!