RGB图像转换到L*u*v*空间(matlab)

①RGB—XYZ

%% Convert RGB to XYZ
function XYZ = rgb2xyz(image)
    T = (1/0.17697)*[0.49, 0.31, 0.20; 0.17697, 0.81240, 0.01063; 0.00, 0.01, 0.99];
    R = image(:,:,1);
    G = image(:,:,2);
    B = image(:,:,3);
    XYZ(:,:,1) = T(1)*R + T(4)*G + T(7)*B; % X
    XYZ(:,:,2) = T(2)*R + T(5)*G + T(8)*B; % Y
    XYZ(:,:,3) = T(3)*R + T(6)*G + T(9)*B; % Z
end

②XYZ—Luv*

%% Convert XYZ to Luv
function LUV = xyz2luv(image)
    whiteP = [0.950456,1,1.088754]; %白点的XYZ值
    un = 4*whiteP(1) / (whiteP(1)+15*whiteP(2)+3*whiteP(3));
    vn = 9*whiteP(2) / (whiteP(1)+15*whiteP(2)+3*whiteP(3));
    Y = image(:,:,2);
    U = 4*image(:,:,1) ./ (image(:,:,1) + 15*image(:,:,2) + 3*image(:,:,3));
    V = 9*image(:,:,2) ./ (image(:,:,1) + 15*image(:,:,2) + 3*image(:,:,3));
    L = zeros(size(Y));
    for i = 1:size(Y,1)
        for j = 1:size(Y,2)
            if Y(i, j) <= 0.008856
                L(i, j) = 903.3*Y(i, j);
            else
                L(i, j) = 116*Y(i, j)^(1/3) - 16;
            end
        end
    end
    LUV(:,:,1) = L;
    LUV(:,:,2) = 13 * L .* (U - un);
    LUV(:,:,3) = 13 * L .* (V - vn);
end

麻瓜写于2019/01/14

你可能感兴趣的:(RGB图像转换到L*u*v*空间(matlab))