【发布时间】:2014-06-03 01:10:49
【问题描述】:
我正在尝试在 Matlab 中编写一个函数,该函数采用 unit8 类的 RGB 图像并加倍并将其转换为 YCBCR 图像。转换公式如下。
我会非常感谢任何形式的帮助。
【问题讨论】:
-
看看这个page,在这里你会找到关于yuv、rgb以及它们之间的转换所需的所有信息;包括示例代码。
标签: matlab image-processing image-conversion
我正在尝试在 Matlab 中编写一个函数,该函数采用 unit8 类的 RGB 图像并加倍并将其转换为 YCBCR 图像。转换公式如下。
我会非常感谢任何形式的帮助。
【问题讨论】:
标签: matlab image-processing image-conversion
有一个 Image Processing Toolbox 函数,如果你可以访问它: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时得到的图像:
【讨论】:
rgbImage(:,1,1),然后是rgbImage(:,2,1),...,rgbImage(:,M-1,3),rgbImage(:,M,3))。使用[] 作为 RESHAPE 的参数告诉它找出该值应该是什么。第二次重塑只是颠倒了这个过程。
路径:
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
【讨论】: