% Convert RGB into HVC

function im2 = RGB2HVC(im)
global clusterId label im2 eps minpts

im = double(im);
[i, j, k] = size(im);

tm = [0.62 0.178 0.204; 0.299 0.587 0.114; 0 0.056 0.942];

for r=1:i
    for s=1:j
        temp = tm*[im(r, s, 1);im(r, s, 2);im(r, s, 3)];
        h1 = f(temp(1))-f(temp(2));
        h2 = f(temp(3))-f(temp(2));
        h3 = f(temp(2));
        m1 = h1;
        m2 = 0.4*h2;
        if (m1 == 0)
            q = (pi/2);
        else
            q = atan(m2/m1);
        end
        s1 = (8.880+0.966*cos(q))*m1;
        s2 = (8.025+2.558*cos(q))*m2;
        
        % H V C corresponds to 1 2 3
        if (s1 == 0)
            im2(r, s, 1) = (pi/2);
        else
            im2(r, s, 1) = atan(s2/s1);
        end
        im2(r, s, 2) = h3;
        im2(r, s, 3) = sqrt(s1^2 + s2^2);   
    end
end

return
       
