rgb to ycbcr conversion in matlab

前端 未结 2 2153
有刺的猬
有刺的猬 2020-12-17 07:15

I am trying to write a function in Matlab that takes an RGB image of class unit8 and double and converts it to a YCBCR image. The transformation formula is below.

2条回答
  •  渐次进展
    2020-12-17 08:03

    "rgb2ycbcr" function (image processing toolbox):

    Path :

    MATLAB\R2013a\toolbox\images\colorspaces\rgb2ycbcr.m

    Code :

    function ycbcr = rgb2ycbcr(varargin)
    rgb = parse_inputs(varargin{:});
    isColormap = false;
    
    if (ndims(rgb) == 2)
     isColormap=true;
     colors = size(rgb,1);
     rgb = reshape(rgb, [colors 1 3]);
    end
    
    origT = [65.481 128.553 24.966;-37.797 -74.203 112; 112 -93.786 -18.214];
    origOffset = [16;128;128];
    
    
    scaleFactor.double.T = 1/255;      
    scaleFactor.double.offset = 1/255; 
    scaleFactor.uint8.T = 1/255;       
    scaleFactor.uint8.offset = 1;      
    scaleFactor.uint16.T = 257/65535;  
    scaleFactor.uint16.offset = 257;   
    
    classIn = class(rgb);
    T = scaleFactor.(classIn).T * origT;
    offset = scaleFactor.(classIn).offset * origOffset;
    
    
    ycbcr = zeros(size(rgb),classIn);
    
    for p = 1:3
     ycbcr(:,:,p) = imlincomb(T(p,1),rgb(:,:,1),T(p,2),rgb(:,:,2), T(p,3),rgb(:,:,3),offset(p));
    end  
    
    if isColormap
     ycbcr = reshape(ycbcr, [colors 3 1]);
    end
    

提交回复
热议问题