matlab中rgb到ycbcr的转换

发布于 2024-11-15 07:19:19 字数 174 浏览 10 评论 0原文

我正在尝试在Matlab中编写一个函数,该函数获取unit8类的RGB图像并进行双精度转换,并将其转换为YCBCR图像。变换公式如下。

YCbCr 和 RGB 之间的转换

我将非常感谢任何形式的帮助。

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.

The transformation between YCbCr and RGB

I would be really thankful for any help of any kind.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自控 2024-11-22 07:19:19

如果您有权访问的话,有一个图像处理工具箱功能:RGB2YCBCR

如果您无权访问它,请按以下方法操作您可以自己进行转换:

rgbImage = imread('peppers.png');  %# A sample RGB image
A = [65.481 -37.797 112; ...       %# A 3-by-3 matrix of scale factors
     128.553 -74.203 -93.786; ...
     24.966 112 -18.214];

%# First convert the RGB image to double precision, scale its values to the
%#   range 0 to 1, reshape it to an N-by-3 matrix, and multiply by A:
ycbcrImage = reshape(double(rgbImage)./255,[],3)*A;

%# Shift each color plane (stored in each column of the N-by-3 matrix):
ycbcrImage(:,1) = ycbcrImage(:,1)+16;
ycbcrImage(:,2) = ycbcrImage(:,2)+128;
ycbcrImage(:,3) = ycbcrImage(:,3)+128;

%# Convert back to type uint8 and reshape to its original size:
ycbcrImage = reshape(uint8(ycbcrImage),size(rgbImage));

这是显示 ycbcrImage 时获得的图像:

There's an Image Processing Toolbox function for that, if you have access to it: RGB2YCBCR

If you don't have access to it, here's how you can do the conversion yourself:

rgbImage = imread('peppers.png');  %# A sample RGB image
A = [65.481 -37.797 112; ...       %# A 3-by-3 matrix of scale factors
     128.553 -74.203 -93.786; ...
     24.966 112 -18.214];

%# First convert the RGB image to double precision, scale its values to the
%#   range 0 to 1, reshape it to an N-by-3 matrix, and multiply by A:
ycbcrImage = reshape(double(rgbImage)./255,[],3)*A;

%# Shift each color plane (stored in each column of the N-by-3 matrix):
ycbcrImage(:,1) = ycbcrImage(:,1)+16;
ycbcrImage(:,2) = ycbcrImage(:,2)+128;
ycbcrImage(:,3) = ycbcrImage(:,3)+128;

%# Convert back to type uint8 and reshape to its original size:
ycbcrImage = reshape(uint8(ycbcrImage),size(rgbImage));

And here's the image you get when you display ycbcrImage:

enter image description here

夏见 2024-11-22 07:19:19

“rgb2ycbcr”函数(图像处理工具箱):

路径

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

代码

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

"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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文