Hue的彩色滤光片

时间:2013-07-29 19:04:48

标签: matlab colors color-space hsv

为什么下面没有彩色滤镜可以找到青椒?

代码:

function [ outhsv ] = ColorFilter( hsv, h, s )
%COLORFILTER Summary of this function goes here
%   Detailed explanation goes here

    if nargin < 2
        h = [];
    end
    if nargin < 3
        s = [];
    end

    if size(h,2)==1
        h = padarray(h, [0 1], 1/100, 'post');
    end

    if size(s,2)==1
        s = padarray(s, [0 1], 1/100, 'post');
    end

    if isempty(h)
        v_of_h = ones(size(hsv,1), size(hsv,2));
    else
        v_of_h = WeightFunction( hsv(:,:,1), h(:,1), h(:,2));
    end

    if isempty(s)
        v_of_s = ones(size(hsv,1), size(hsv,2));
    else
        v_of_s = WeightFunctionOnce( hsv(:,:,2), s(:,1), s(:,2));
    end

    outhsv = hsv;
    outhsv(:,:,3) = hsv(:,:,3) .* v_of_h .* v_of_s;



function y = WeightFunction( x, mu, sigma ) 

    %y = WeightFunctionOnce(x,mu,sigma) + WeightFunctionOnce(x-1,mu,sigma);
    y = 1 - (1-WeightFunctionOnce(x,mu,sigma)) .* (1-WeightFunctionOnce(x-1,mu,sigma));

function y = WeightFunctionOnce( x, mu, sigma ) 

    if nargin<2
        mu=0;
    elseif nargin<3
        sigma=1./100.;
    end


    if any(size(mu) ~= size(sigma))
        error('mu and sigma should be of the same size');
    end

    y = zeros([size(x) numel(mu)]);

    for i=1:numel(mu)
        y(:,:,i) = exp(-((x - mu(i)) .^ 2 ./ (2 .* sigma(i) .^ 2)));
    end

    %y = sum(y,3)/size(y,3);
    y = 1-prod(1-y,3);

显示代码:

hue = 120;
h = [hue/360 0.05];
s = [];

rgb1 = imread('huescale.png');
%rgb1 = imread('peppers.png');
hsv1 = rgb2hsv(rgb1);
hsv2 = ColorFilter(hsv1, h, s);
rgb2 = hsv2rgb(hsv2);
bitmask = hsv1(:,:,1)>(h(1)-h(2)) & hsv1(:,:,1)<(h(1)+h(2));


figure; 
subplot(3,1,1); imshow(rgb1);
subplot(3,1,2); imshow(rgb2);
subplot(3,1,3); imshow(bitmask);

按比例计算结果

enter image description here

(作品)

结果辣椒:

enter image description here

(不)

为什么?

1 个答案:

答案 0 :(得分:4)

如果仔细观察H值,那些青椒会有些偏黄,所以你可能想稍微扩大规则。enter image description here

我建议介于0.15到0.5之间。你也可以结合饱和度通道,比如只考虑充满活力的图像部分,即我们想摆脱洋葱。请尝试以下代码进行预览。

hsv_dat = rgb2hsv(imread('peppers.png'));
imagesc(hsv_dat(:,:,1) > 0.15 & hsv_dat(:,:,1) < 0.5 & hsv_dat(:,:,2) > 0.3)
colormap(gray)

你应该

enter image description here