2

为什么下面的滤色镜找不到青椒?

编码:

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);

规模化结果

在此处输入图像描述

(作品)

辣椒的结果:

在此处输入图像描述

(才不是)

为什么?

4

1 回答 1

4

如果你仔细观察 H 值,那些青椒有点淡黄色,所以你可能想稍微扩大一下规则。在此处输入图像描述

我建议介于 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)

你应该得到

在此处输入图像描述

于 2013-07-29T20:26:05.510 回答