ycbcr2rgb.m

来自「有关matlab的电子书籍有一定的帮助希望有用」· M 代码 · 共 67 行

M
67
字号
function rgb = ycbcr2rgb(in)
%YCBCR2RGB Convert YCBCR values to RGB color space.
%   RGBMAP = YCBCR2RGB(YCBCRMAP) converts the YCBCR values in the
%   colormap YCBCRMAP to the RGB color space. If YCBCRMAP is M-by-3 and
%   contains the YCBCR luminance (Y) and chrominance (Cb and Cr) color
%   values as columns, then RGBMAP is an M-by-3 matrix that contains
%   the red, green, and blue values equivalent to those colors.
%
%   RGB = YCBCR2RGB(YCBCR) converts the YCBCR image to the equivalent
%   truecolor image RGB.
%
%   Class Support
%   -------------
%   If the input is a YCBCR image, it can be of class uint8 or double;
%   the output image is of the same class as the input image.  If the
%   input is a colormap, the input and output colormaps are both of
%   class double.
%
%   See also NTSC2RGB, RGB2NTSC, RGB2YCBCR.

%   Chris Griffin 7-97
%   Copyright 1993-1998 The MathWorks, Inc.  All Rights Reserved.
%   $Revision: 1.4 $  $Date: 1997/11/24 15:36:48 $

%   Reference:
%     Charles A. Poynton, "A Technical Introduction to Digital Video", John Wiley
%     & Sons, Inc., 1996

if ndims(in)~=3 | size(in,3)~=3
   if ndims(in)==2 & size(in,2)==3 % a colormap
      iscolormap=1;
      colors = size(in,1);
      in = reshape(in, [colors 1 3]);
   else
      error('Invalid YCbCr input image');
   end
else
   iscolormap=0;
end

if isa(in, 'uint8')
   in = double(in);
   u8in = 1;
elseif isa(in, 'double')
   in = in*255;
   u8in = 0;
else
   error('Unsupported input class');
end

% These equations transform YCBCR in [0, 255] to RGB in [0,1].
in(:,:,1) = in(:,:,1)-16;
in(:,:,2) = in(:,:,2)-128;
in(:,:,3) = in(:,:,3)-128;

rgb(:,:,1) = .00456621 * in(:,:,1) + .00625893 * in(:,:,3);
rgb(:,:,2) = .00456621 * in(:,:,1) - .00153632 * in(:,:,2) - .00318811 * in(:,:,3);
rgb(:,:,3) = .00456621 * in(:,:,1) + .00791071 * in(:,:,2);

if u8in
   rgb = im2uint8(rgb);
end

if iscolormap
   rgb = reshape(rgb, [colors 3 1]);
end

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?