Convert RGB to HSV

I want to convert RGB values ​​to HSV values. But if I divide 9 by 28, octave calculate 0. Can someone explain the reason to me ??

function [hsv] = RGBtoHSV()
    im = imread('picture.png'); 
    R = im(:,:,1); 
    G = im(:,:,2); 
    B = im(:,:,3);

    len = length(R); % R, G, B should have the same length
    for i = 1:len
        MAX = max([R(i),G(i),B(i)]);
        MIN = min([R(i),G(i),B(i)]);
        S = 0;
        if MAX == MIN
            H = 0;
        elseif MAX == R(i)
            disp(G(i) - B(i)); % 9 
            disp(MAX - MIN);  % 28
            H = 0.6 * ( 0 + ( (G(i) - B(i)) / MAX - MIN) ); % 0
            disp(H) % why i get 0 if try to calculate ( 0 + ( (G(i) - B(i)) / MAX - MIN)?
        ....
            end
        return;
    end
endfunction

RGBtoHSV()

Chris: D

+4
source share
2 answers

You must make the image in Doubleby doing:

im = double(imread('picture.png'));

This will solve your problems that occur as the image is of type UINT8.

+4
source

You can also use the Octab function, the built-in rgb2hsv function, instead of writing your own.

im_rgb = imread ("picture.png"); 
im_hsv = rgb2hsv (im_rgb);

If this is an exercise, I would suggest you look at its source, type type rgb2hsvOctave at the prompt, and see how it is implemented.

+1

Source: https://habr.com/ru/post/1542973/


All Articles