Change a specific color in an image to another one
Mostrar comentarios más antiguos
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);
Respuesta aceptada
Más respuestas (1)
Dhruv G
el 14 de Jul. de 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
3 comentarios
Khoa Tran
el 14 de Jul. de 2021
Dhruv G
el 14 de Jul. de 2021
Right, so you would classify a pixel as red if it's red channel is higher than some threshold and the other channels lower than a threshold. Then you would want to make that pixel black (setting all channels of that pixel to 0)
Khoa Tran
el 14 de Jul. de 2021
Categorías
Más información sobre Images en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!