2011-11-14 91 views
1

我使用的是FreeMat,我有一个RGB图片,它是一个3D矩阵,包含图片的列和行以及每个像素的RGB值。基本FreeMat/MATLAB语法 - 尺寸错误

由于没有固有的功能将RGB图片转换为YIQ,我已经实现了一个。我想出了这个代码:

假设我有一个三维阵列,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; 

我不明白为什么矩阵乘法失败。我想要的代码是好的,不只是,用手乘以矩阵...

+1

http://www.mathworks.com/help/toolbox/images/ref/rgb2ntsc.html – 0x90

+1

你知道矩阵乘法原理如何工作吗?你是如何解读你得到的错误信息的?在需要解决的问题中,您实际上是在尝试将矩阵和3D数组相乘。 btw:您可以使用size(mat,n)来获取沿尺寸n而不是长度(mat(:1,1))或长度(mat(1,:,1))的mat的大小。和mat(1:size(mat,1),mat(1:size(mat,2),:)是一样的mat(:,:),它与mat相同,即你的p是相同的作为image_rgb。 –

回答

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 results

注意k将3 RGB图像。在每个陈述后查看矩阵的大小。并且不要忘记将您的图片转换为double