首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基本FreeMat/MATLAB语法-尺寸错误

基本FreeMat/MATLAB语法-尺寸错误
EN

Stack Overflow用户
提问于 2011-11-14 14:43:18
回答 2查看 2.9K关注 0票数 1

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

因为没有一个内部函数来将RGB图片转换为YIQ,所以我实现了一个。我想出了这个代码:

假设我有一个3D数组,image_rgb

代码语言:javascript
复制
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;

我不明白为什么矩阵乘法会失败。我想要的代码是好的,而不仅仅是,手动乘以矩阵…

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-11-14 23:03:49

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

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

代码语言:javascript
复制
%# 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)')

请注意,对于RGB图像,k将为3。查看每条语句后面的矩阵大小。别忘了把你的图片转换成double格式。

票数 2
EN

Stack Overflow用户

发布于 2018-03-22 07:12:33

使用相同的矩阵和-color矩阵函数,可以使用Imagemagick完成此操作:

输入:

代码语言:javascript
复制
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:

代码语言:javascript
复制
convert peppers_tiny.png -colorspace YIQ -separate \
-set colorspace sRGB -combine peppers_tiny_yiq2.png

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

代码语言:javascript
复制
convert peppers_tiny.png -colorspace YIQ -separate \
-swap 0,1 -set colorspace sRGB -combine peppers_tiny_yiq3.png

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8118153

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档