一种可以大大改善生成的图像质量的选项是转换为其他颜色空间,以便更轻松地选择颜色。特别是, HSV颜色空间根据像素的色相(颜色),饱和度(颜色量)和值(颜色的亮度)来定义像素颜色。
例如,您可以使用rgb2hsv
函数将RGB图像转换为HSV空间,找到具有跨越您要定义为“非红色”颜色(例如20度到340度)的色相的像素,设置饱和度对于那些像素为0(因此它们是灰度),然后使用函数hsv2rgb
将图像转换回RGB空间:
cdata = imread('EcyOd.jpg'); % Load image
hsvImage = rgb2hsv(cdata); % Convert the image to HSV space
hPlane = 360.*hsvImage(:, :, 1); % Get the hue plane scaled from 0 to 360
sPlane = hsvImage(:, :, 2); % Get the saturation plane
nonRedIndex = (hPlane > 20) & ... % Select "non-red" pixels
(hPlane < 340);
sPlane(nonRedIndex) = 0; % Set the selected pixel saturations to 0
hsvImage(:, :, 2) = sPlane; % Update the saturation plane
rgbImage = hsv2rgb(hsvImage); % Convert the image back to RGB space
这是结果图像:

请注意,与zellus的解决方案相比,您可以轻松地维持花朵上的浅粉色调。还要注意,茎和地面上的褐色调也消失了。
有关根据对象的颜色属性从图像中选择对象的出色示例,请查看Steve Eddins博客文章《两个朋友》 ,其中描述了MathWorks的Brett Shoelson提出的从图像中提取一个“ amigo”的解决方案。
有关选择颜色范围的说明...
您可以做的另一件事是帮助您选择颜色范围,这是查看HSV图像像素中出现的色调(即,上方的hPlane
)的直方图。这是一个使用histc
函数(或推荐的histcounts
,如果可用)和bar
的示例:
binEdges = 0:360; % Edges of histogram bins
hFigure = figure(); % New figure
% Bin pixel hues and plot histogram:
if verLessThan('matlab', '8.4')
N = histc(hPlane(:), binEdges); % Use histc in older versions
hBar = bar(binEdges(1:end-1), N(1:end-1), 'histc');
else
N = histcounts(hPlane(:), binEdges);
hBar = bar(binEdges(1:end-1), N, 'histc');
end
set(hBar, 'CData', 1:360, ... % Change the color of the bars using
'CDataMapping', 'direct', ... % indexed color mapping (360 colors)
'EdgeColor', 'none'); % and remove edge coloring
colormap(hsv(360)); % Change to an HSV color map with 360 points
axis([0 360 0 max(N)]); % Change the axes limits
set(gca, 'Color', 'k'); % Change the axes background color
set(hFigure, 'Pos', [50 400 560 200]); % Change the figure size
xlabel('HSV hue (in degrees)'); % Add an x label
ylabel('Bin counts'); % Add a y label
这是生成的像素颜色直方图:

请注意,原始图像如何主要包含红色,绿色和黄色的像素(以及一些橙色的像素)。几乎没有青色,蓝色,靛蓝或洋红色的像素。还要注意,我在上面选择的范围(20到340度)可以很好地排除不属于两端两个大红色群集的大部分内容。
0
我正在尝试创建一种类似于《 罪恶之城》或其他电影的效果,其中它们会删除图像中除一种颜色以外的所有颜色。
我有一个RGB图像,我想将其转换为灰度,但我想保留一种颜色。
这是我的照片:
我想保持红色。其余应为灰度。
到目前为止,这是我的代码输出的内容(您可以看到这些区域是正确的,但是我不知道为什么它们是白色而不是红色):
到目前为止,这是我的代码: