1

我正在使用FreeMat,我有一张RGB图片,它是一个 3D 矩阵,包含图片的列和行以及每个像素的 RGB 值。

由于没有将 RGB 图片转换为YIQ的内在函数,我已经实现了一个。我想出了这段代码:

假设我有一个 3D 数组image_rgb

matrix = [0.299 0.587 0.114;
0.596 -0.274 -0.322;
0.211 -0.523 0.312];
row = 1:length(image_rgb(:,1,1));
col = 1:length(image_rgb(1,:,1));
p = image_rgb(row,col,:);

%Here I have the problem
mage_yiq(row,col,:) = matrix*image_rgb(row,col,:);

max_y = max (max(image_yiq(:,:,1)));
max_i = max (max(image_yiq(:,:,2)));
max_q = max (max(image_yiq(:,:,3)));

%Renormalize the image again after the multipication
% to [0,1].
image_yiq(:,:,1) = image_yiq(:,:,1)/max_y;
image_yiq(:,:,2) = image_yiq(:,:,2)/max_i;
image_yiq(:,:,3) = image_yiq(:,:,3)/max_q;

我不明白为什么矩阵乘法会失败。我希望代码很好,而不仅仅是手动乘以矩阵......

4

2 回答 2

2

您尝试将 3D 数组与matrix您创建的数组相乘,这不是正确的矩阵乘法。您应该将图像数据展开为 3×m*n 矩阵,并将其与自定义矩阵相乘。

这是将自定义色彩空间转换应用于 RGB 图像的解决方案。我使用了您提供的矩阵并将其与内置的 YIQ 变换进行了比较。

%# Define the conversion matrix
matrix = [0.299  0.587  0.114;
          0.596 -0.274 -0.322;
          0.211 -0.523  0.312];

%# Read your image here
rgb = im2double(imread('peppers.png'));
subplot(1,3,1), imshow(rgb)
title('RGB')


%# Convert using unfolding and folding
[m n k] = size(rgb);

%# Unfold the 3D array to 3-by-m*n matrix
A = permute(rgb, [3 1 2]);
A = reshape(A, [k m*n]);

%# Apply the transform
yiq = matrix * A;

%# Ensure the bounds
yiq(yiq > 1) = 1;
yiq(yiq < 0) = 0;

%# Fold the matrix to a 3D array
yiq = reshape(yiq, [k m n]);
yiq = permute(yiq, [2 3 1]);

subplot(1,3,2), imshow(yiq)
title('YIQ (with custom matrix)')


%# Convert using the rgb2ntsc method
yiq2 = rgb2ntsc(rgb);
subplot(1,3,3), imshow(yiq2)
title('YIQ (built-in)')

YIQ结果

请注意,k对于 RGB 图像,它将是 3。在每个语句之后查看矩阵的大小。并且不要忘记将您的图像转换为double.

于 2011-11-14T15:03:48.943 回答
1

可以使用 Imagemagick 使用相同的矩阵和 -color-matrix 函数来做到这一点:

输入:

在此处输入图像描述

convert peppers_tiny.png -color-matrix \
" \
0.299 0.587 0.114 \
0.596 -0.274 -0.322 \
0.211 -0.523 0.312 \
" \
peppers_tiny_yiq.png

在此处输入图像描述

但这不是真正的 sRGB 到 YIQ 的转换。

这是 sRGB 到 YIQ 的转换,将 YIQ 显示为 RGB:

convert peppers_tiny.png -colorspace YIQ -separate \
-set colorspace sRGB -combine peppers_tiny_yiq2.png

在此处输入图像描述

这是相同的,但交换了前两个通道:

convert peppers_tiny.png -colorspace YIQ -separate \
-swap 0,1 -set colorspace sRGB -combine peppers_tiny_yiq3.png

在此处输入图像描述

于 2018-03-21T23:12:32.630 回答