Change a specific color in an image to another one
40 views (last 30 days)
Show older comments
I want my program to change a color in the user's input in coloredChips.png to black. How can i do it? I have tried but it failed. As you can see, i input RED but the image is still red, not black.
format compact
clc;
close all;
clear;
originalImage = imread('coloredChips.png'); % Load original ColoredChips.png image
[rows, columns, numberOfColorBands] = size(originalImage); % Get the dimensions of the image
% Display the original images
subplot(2,1,1);
imshow(originalImage);
% Extract the individual red, green, and blue color channels.
redChannel = originalImage(:, :, 1);
greenChannel = originalImage(:, :, 2);
blueChannel = originalImage(:, :, 3);
black = redChannel == 0 & greenChannel == 0 & blueChannel == 0;
color = input("Enter color to remove(RED,GREEN,BLUE): ", 's'); % taking input from user
for row = 1 : rows %iterating over each pizel of image
for column = 1 : columns
if color == "RED"
originalImage(black) = 0; % if input is red we'll make red channel value for this pixel to 0
elseif color == "GREEN"
originalImage(black) = 0; % if input is green we'll make red channel value for this pixel to 0
else
originalImage(black) = 0; % if input is green we'll make red channel value for this pixel to 0
end
end
end
rgb = cat(3, redChannel, greenChannel, blueChannel);
subplot(2,1,2);
imshow(rgb);
Accepted Answer
DGM
on 14 Jul 2021
Edited: DGM
on 2 May 2022
EDIT 5/2022: I've posted a number of variations of answers on this same question. For a more comprehensive answer of this same assignment, see this post. For a more elaborate color replacement task using the same image, see this post.
ORIGINAL ANSWER:
Bear with me. My connection is worse than normal, and the scripts on this site simply break below about 300 kbps. I can't afford to include any images, and this will probably take a dozen attempts to post.
originalImage = imread('coloredChips.png'); % Load original ColoredChips.png image
[rows, columns, channels] = size(originalImage); % Get the dimensions of the image
if channels~=3
error('the image needs to be RGB')
end
% Display the original images
subplot(2,1,1);
imshow(originalImage);
% 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;
% i'm not dealing with a tedious prompt. feel free to change
colormode = 'Green';
% you can also specify a color other than black
newcolor = [250 50 220];
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]);
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);
EDIT: modified to fix copypaste error and added option to specify replacement color.
More Answers (1)
Dhruv G
on 14 Jul 2021
The issue here is you aren't changing redChannel, blueChannel or greenChannel anywhere. I'm not sure what you are trying to do with the black variable. If you want to do something like make a pixel black if it's redChannel is higher than a threshold and the others are lower, you should do something like the following in your if statement (the code I've written is for RED):
highthresh = 180
lowthresh = 60
if color=='RED'
if redChannel(row,column) > highthresh & blueChannel(row,column) < lowthresh & greenChannel(row,column) < lowthresh
redChannel(row,column) = 0;
blueChannel(row,column) = 0;
greenChannel(row,column) = 0;
end
end
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!