user2372976
user2372976

Reputation: 725

Convert RGB to HSV

i want to convert RGB values to HSV values . But if I devide 9 by 28, octave calculate 0. Can anyone explain me the reason??

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

Upvotes: 4

Views: 1109

Answers (2)

carandraug
carandraug

Reputation: 13091

You can also use Octave's builtin rgb2hsv function instead of writing your own.

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

If this is an exercise, then I'd suggest you look at its source, enter type rgb2hsv at the Octave prompt, and see how its implemented.

Upvotes: 2

Royi
Royi

Reputation: 4963

You must cast the image into Double by doing:

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

This will solve your issues which happens since the image is type UINT8.

Upvotes: 4

Related Questions