将图像中的特定颜色更改为黑色
Change a specific color in an image to black
我希望我的程序将 coloredChips.png
中用户输入的颜色更改为黑色。我该怎么做?我试过了,但是失败了。
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);
如您所见,我输入了红色,但图像仍然是红色,而不是黑色:
您的代码的第一个问题是,在您的 for 循环中,您更改了 originalImage 而不是您的红色、绿色、蓝色通道。其次,您不使用迭代参数,这意味着您实际上没有设置相应的像素,实际上在您的 for 循环中,您将黑色像素设置为黑色。
下面是我如何解决问题的示例代码。希望这能给你一些指示。
orgImg = imread('image.jpg');
% threshold
t = 0.5;
colThresUp = t * 255;
colThresLow = (1-t) * 255;
% init color mat
rMat = orgImg(:,:,1);
gMat = orgImg(:,:,2);
bMat = orgImg(:,:,3);
% build an index matrix of all pixels that are above the threshold
% resp. below for the other colors
% TODO depending on user selection - example for RED
idxSelCol = orgImg(:,:,1) > colThresUp & ...
orgImg(:,:,3) < colThresLow & ...
orgImg(:,:,2) < colThresLow;
% set all pixels above the threshold to zero i.e. black
rMat(idxSelCol) = 0;
gMat(idxSelCol) = 0;
bMat(idxSelCol) = 0;
% build new image
modImg = cat(3, rMat,gMat,bMat);
% visualize result
subplot(2,1,1)
imshow(orgImg)
subplot(2,1,2)
imshow(modImg)
我希望我的程序将 coloredChips.png
中用户输入的颜色更改为黑色。我该怎么做?我试过了,但是失败了。
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);
如您所见,我输入了红色,但图像仍然是红色,而不是黑色:
您的代码的第一个问题是,在您的 for 循环中,您更改了 originalImage 而不是您的红色、绿色、蓝色通道。其次,您不使用迭代参数,这意味着您实际上没有设置相应的像素,实际上在您的 for 循环中,您将黑色像素设置为黑色。
下面是我如何解决问题的示例代码。希望这能给你一些指示。
orgImg = imread('image.jpg');
% threshold
t = 0.5;
colThresUp = t * 255;
colThresLow = (1-t) * 255;
% init color mat
rMat = orgImg(:,:,1);
gMat = orgImg(:,:,2);
bMat = orgImg(:,:,3);
% build an index matrix of all pixels that are above the threshold
% resp. below for the other colors
% TODO depending on user selection - example for RED
idxSelCol = orgImg(:,:,1) > colThresUp & ...
orgImg(:,:,3) < colThresLow & ...
orgImg(:,:,2) < colThresLow;
% set all pixels above the threshold to zero i.e. black
rMat(idxSelCol) = 0;
gMat(idxSelCol) = 0;
bMat(idxSelCol) = 0;
% build new image
modImg = cat(3, rMat,gMat,bMat);
% visualize result
subplot(2,1,1)
imshow(orgImg)
subplot(2,1,2)
imshow(modImg)