将RGB转换为波长的方法

时间:2016-04-14 09:25:51

标签: javascript scilab

我想将RGB图像矩阵转换为相应的Wavelenth矩阵。如果有任何方法论或算法,请在此处提及。

1 个答案:

答案 0 :(得分:2)

查看其他答案,您可以执行两个步骤:

从RGB转换为HSV

function [h,s,v] = rgb2hsv(r, g, b)

    MAX_PIXEL_VALUE = 255.0;

    assert_checktrue ( r <= MAX_PIXEL_VALUE )
    assert_checktrue ( g <= MAX_PIXEL_VALUE )
    assert_checktrue ( b <= MAX_PIXEL_VALUE )

    r = r / MAX_PIXEL_VALUE;
    g = g / MAX_PIXEL_VALUE;
    b = b / MAX_PIXEL_VALUE;

    max_val = max(r, g, b);
    min_val = min(r, g, b);

    v = max_val;

    if max_val == 0.0 then
        s = 0;
        h = 0;
    elseif (max_val - min_val) == 0.0 then
        s = 0;
        h = 0;
    else
        s = (max_val - min_val) / max_val;

        if max_val == r then
            h = 60 * ((g - b) / (max_val - min_val)) + 0;
        elseif max_val == g then
            h = 60 * ((b - r) / (max_val - min_val)) + 120;
        else
            h = 60 * ((r - g) / (max_val - min_val)) + 240;
        end
    end

    if h < 0 then
        h = h + 360.0;
    end

    h = h / 2;
    s = s * MAX_PIXEL_VALUE;
    v = v * MAX_PIXEL_VALUE;

    return [h, s, v]
endfunction

从Hue转换为波长

function wavelength = hue2wavelength(hue)

    // There is nothing corresponding to magenta in the light spectrum, 
    // So let's assume that we only use hue values between 0 and 270.
    assert_checktrue(hue >= 0);
    assert_checktrue(hue <= 270);

    // Estimating that the usable part of the visible spectrum is 450-620nm, 
    // with wavelength (in nm) and hue value (in degrees), you can improvise this:
    wavelength = 620 - 170 / 270 * hue;

endfunction

将它们放在一起

function wavelength = rgb2wavelength(r,g,b)
    [h,s,v] = rgb2hsv(r,g,b);
    wavelength = hue2wavelength(h);
endfunction