2013-07-29 287 views
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); 

結果

enter image description here

(作品)

重新SULT對辣椒:

enter image description here

(不)

爲什麼?

+1

我不是圖像處理專家,但我的攝影者眼球的辣椒有很多黃色的,幾乎沒有純綠色。將peppers.png圖像放入Photoshop並在「色相/飽和度」面板中調整圖像即可確認。 – horchler

回答

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