37

我正在尝试创建类似于罪恶之城或其他电影的效果,它们会从图像中删除除一种颜色之外的所有颜色。

我有一个 RGB 图像,我想将其转换为灰度,但我想保留一种颜色。

这是我的照片:

替代文字

我想保持红色。其余的应该是灰度的。

这是到目前为止我的代码输出的内容(您可以看到这些区域是正确的,但我不知道为什么它们是白色而不是红色):

替代文字

到目前为止,这是我的代码:

filename = 'roses.jpg';

[cdata,map] = imread( filename );
% convert to RGB if it is indexed image
if ~isempty( map ) 
   cdata = idx2rgb( cdata, map ); 
end

%imtool('roses.jpg');

imWidth = 685;
imHeight = 428;

% RGB ranges of a color we want to keep
redRange = [140 255];
greenRange = [0 40];
blueRange = [0 40];

% RGB values we don't want to convert to grayscale
redToKeep = zeros(imHeight, imWidth);
greenToKeep = zeros(imHeight, imWidth);
blueToKeep = zeros(imHeight, imWidth);

for x=1:imWidth

    for y=1:imHeight

        red = cdata( y, x, 1 );
        green = cdata( y, x, 2 );
        blue = cdata( y, x, 3 );

        if (red >= redRange(1) && red <= redRange(2) && green >= greenRange(1) && green <= greenRange(2) && blue >= blueRange(1) && blue <= blueRange(2))
            redToKeep( y, x ) = red;
            greenToKeep( y, x ) = green;
            blueToKeep( y, x ) = blue;
        else
            redToKeep( y, x ) = 999;
            greenToKeep( y, x ) = 999;
            blueToKeep( y, x ) = 999;
        end

    end 

end 

im = rgb2gray(cdata);
[X, map] = gray2ind(im);
im = ind2rgb(X, map);

for x=1:imWidth

    for y=1:imHeight

        if (redToKeep( y, x ) < 999)
            im( y, x, 1 ) = 240;
        end
        if (greenToKeep( y, x ) < 999)
            im( y, x, 2 ) = greenToKeep( y, x );
        end
        if (blueToKeep( y, x ) < 999)
            im( y, x, 3 ) = blueToKeep( y, x );
        end

    end 

end 

imshow(im);
4

3 回答 3

88

一种大大提高生成图像质量的选项是转换为不同的颜色空间,以便更轻松地选择颜色。特别是,HSV 颜色空间根据其色调(颜色)、饱和度(颜色量)和值(颜色的亮度)来定义像素颜色。

例如,您可以使用函数将 RGB 图像转换为 HSV 空间rgb2hsv,找到具有跨越您想要定义为“非红色”颜色(例如 20 度到 340 度)的色调的像素,设置饱和度为这些像素为 0(因此它们是灰度),然后使用以下函数将图像转换回 RGB 空间hsv2rgb

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 的博客文章The Two Amigos,其中描述了来自 MathWorks 的 Brett Shoelson 的解决方案,用于从图像中提取一个“朋友”。


关于选择颜色范围的注意事项...

可以帮助您选择颜色范围的另一件事是hPlane查看 HSV 图像像素中存在的色调(即从上方)的直方图。这是一个使用函数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 度)很好地排除了不属于两端两个大红色集群的大部分内容。

于 2010-10-31T17:33:45.540 回答
19
figure
pic = imread('EcyOd.jpg');

for mm = 1:size(pic,1)
    for nn = 1:size(pic,2)
        if pic(mm,nn,1) < 80 || pic(mm,nn,2) > 80 || pic(mm,nn,3) > 100
            gsc = 0.3*pic(mm,nn,1) + 0.59*pic(mm,nn,2) + 0.11*pic(mm,nn,3);
            pic(mm,nn,:) = [gsc gsc gsc];
        end
    end
end
imshow(pic)

替代文字

于 2010-10-31T17:41:52.400 回答
2

我真的不知道 matlab 是如何工作的,所以我不能真正评论代码,但也许这将有助于解释 RGB 颜色是如何工作的。

当使用 RGB 颜色时,可以通过确保 R、G 和 B 的值都相同来制作灰度。所以基本上你想要做的是检测一个像素是否是红色的,而不只是让 R、G 和 B 相同(你可以使用 3 的平均值作为基本结果)。

更难的部分是如何检测一个像素是否真的是红色,你不能只检查一个像素的 R 值是否高,因为它仍然可以是另一种颜色,而低 R 值可能意味着更深的红色。

所以你可以做这样的事情:(我没有matlab,所以假设语法):

红色 = cdata(y, x, 1);
绿色 = cdata(y, x, 2);
蓝色 = cdata(y, x, 3);

if (red < (blue * 1.4) || red < (green * 1.4) )
{
    平均=(红+绿+蓝)/3;
    cdata(y, x, 1) = 平均;
    cdata(y, x, 2) = 平均;
    cdata(y, x, 3) = 平均;
}

可能有更好的方法来检测红色并获得平均灰色,但这是一个开始;)

于 2010-10-31T17:19:48.980 回答