简体   繁体   English

将RGB转换为HSV

[英]Convert RGB to HSV

i want to convert RGB values to HSV values . 我想将RGB值转换为HSV值。 But if I devide 9 by 28, octave calculate 0. Can anyone explain me the reason?? 但如果我将9乘以28,八度计算0.可以有人解释我的原因吗?

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 克里斯:D

You must cast the image into Double by doing: 您必须通过执行以下操作将图像转换为Double

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

This will solve your issues which happens since the image is type UINT8 . 这将解决因图像类型为UINT8而发生的问题。

You can also use Octave's builtin rgb2hsv function instead of writing your own. 您也可以使用Octave的内置rgb2hsv函数,而不是自己编写。

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. 如果这是一个练习,那么我建议你查看它的来源,在Octave提示符下输入type rgb2hsv ,看看它是如何实现的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM