2

I want to convert the RGB image matrix to corresponding Wavelenth matrix. If there any methodology or alogorithm just mention here.

proversion
  • 573
  • 3
  • 12
  • 24
  • Did my answer helped you? – spoorcc Apr 18 '16 at 07:43
  • Wavelength as in the size of the lightwave in nanometers? You can't do this, only a very small set of values correspond to a wavelength. You don't have a wavelength for brown or pink for example. – D.J. Klomp Aug 11 '19 at 16:26

1 Answers1

3

Looking at other answers you could do two-step process:

Converting from RGB to 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

Converting from Hue to wavelength

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

Putting it together

function wavelength = rgb2wavelength(r,g,b)
    [h,s,v] = rgb2hsv(r,g,b);
    wavelength = hue2wavelength(h);
endfunction
Community
  • 1
  • 1
spoorcc
  • 2,907
  • 2
  • 21
  • 29