문제

Why doesn't color filter below find green peppers?

The code:

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

Display code:

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

result on scale

enter image description here

(works)

result on peppers:

enter image description here

(does not)

Why?

도움이 되었습니까?

해결책

If you looked closer at the H values, those green peppers are kind of yellowish, so you might want to widen the rule a bit.enter image description here

I would suggest something in between 0.15 and 0.5. You can also combine with saturation channel, say only consider portions of images that are vibrant, i.e., we want to get rid of the onions. Try the following codes to get a preview.

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

You should get

enter image description here

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top