clear
close all

rgb_original = imread('test3.png');

imshow(rgb_original);

points = ginput(2);

a = rgb_original(points(1,2):points(2,2),points(1,1):points(2,1),:);
HSV = rgb2hsv(a);

% Calculate avarage from H value space
H = HSV(:,:,1);
Hl = H(1:length(H));
Hmean = mean(Hl)
% Calculate avarage from S value space
S = HSV(:,:,2);
Sl = S(1:length(S));
Smean = mean(Sl)

B = rgb2hsv(rgb_original);

sh = size(B);
% H = zeros(sh(1),sh(2), sh(3));
for x = 1 : sh(1)    
    for y = 1 : sh(2)
        if (B(x,y,2) > Smean-0.025 & B(x,y,2) < Smean+0.025)
            B(x,y,2) = 1;
            B(x,y,3) = 0; % Set intensity to 0
        else
            B(x,y,2) = 0;
            B(x,y,3) = 1; % Set intensity to 1
        end
        if (B(x,y,1) > Hmean-0.025 & B(x,y,1) < Hmean+0.025)
            B(x,y,1) = 1;
            B(x,y,3) = 0;
        else
            B(x,y,1) = 0;
            B(x,y,3) = 1; % Set intensity to 1
        end
    end
end

imshow(hsv2rgb(B))