Search code examples
matlabconvertersrgbhsv

Weird phenomenon when converting RGB to HSV manually in Matlab


I have written a small Matlab funcion which takes an image in RGB and converts it to HSV according to the conversion formulas found here.

The problem is that when I apply this to a color spectrum there is a cut in the spectrum and some values are wrong, see images (to make the comparison easier I have used the internal hsv2rgb() function to convert back to RGB. This does not happen with Matlabs own function rgb2hsv() but I can not find what I have done wrong.

This is my function

function [ I_HSV ] = RGB2HSV( I_RGB )
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here

[MAX, ind] = max(I_RGB,[],3);
if max(max(MAX)) > 1
    I_r = I_RGB(:,:,1)/255;
    I_g = I_RGB(:,:,2)/255;
    I_b = I_RGB(:,:,3)/255;
    MAX = max(cat(3,I_r, I_g, I_b),[],3);
else
    I_r = I_RGB(:,:,1);
    I_g = I_RGB(:,:,2);
    I_b = I_RGB(:,:,3);
end
MIN = min(cat(3,I_r, I_g, I_b),[],3);
d = MAX - MIN;

I_V = MAX;
I_S = (MAX - MIN) ./ MAX;
I_H = zeros(size(I_V));

a = 1/6*mod(((I_g - I_b) ./ d),1);
b = 1/6*(I_b - I_r) ./ d + 1/3;
c = 1/6*(I_r - I_g) ./ d + 2/3;
H = cat(3, a, b, c);

for m=1:size(H,1);
    for n=1:size(H,2);
        if d(m,n) == 0
            I_H(m,n) = 0;
        else
            I_H(m,n) = H(m,n,ind(m,n));
        end
    end
end

I_HSV = cat(3,I_H,I_S,I_V);


end

Original spectrum Original spectrum Reconverted Converted spectrum Converted spectrum


Solution

  • The error was in my simplification of the calculations of a, b, and c. Changing it to the following solved the problem.

    function [ I_HSV ] = RGB2HSV( I_RGB )
    %UNTITLED3 Summary of this function goes here
    %   Detailed explanation goes here
    
    [MAX, ind] = max(I_RGB,[],3);
    if max(max(MAX)) > 1
        I_r = I_RGB(:,:,1)/255;
        I_g = I_RGB(:,:,2)/255;
        I_b = I_RGB(:,:,3)/255;
        MAX = max(cat(3,I_r, I_g, I_b),[],3);
    else
        I_r = I_RGB(:,:,1);
        I_g = I_RGB(:,:,2);
        I_b = I_RGB(:,:,3);
    end
    MIN = min(cat(3,I_r, I_g, I_b),[],3);
    D = MAX - MIN;
    
    I_V = MAX;
    I_S = D ./ MAX;
    I_H = zeros(size(I_V));
    
    a = 1/6*mod(((I_g - I_b) ./ D),6);
    b = 1/6*((I_b - I_r) ./ D + 2);
    c = 1/6*((I_r - I_g) ./ D + 4);
    H = cat(3, a, b, c);
    
    for m=1:size(H,1);
        for n=1:size(H,2);
            if D(m,n) == 0
                I_H(m,n) = 0;
            else
                I_H(m,n) = H(m,n,ind(m,n));
            end
    
            if MAX(m,n) == 0
                S(m,n) = 0;
            end
        end
    end
    
    I_HSV = cat(3,I_H,I_S,I_V);
    
    end