2011-11-14 14 views
1

私はを使用しています。RGBの画像は、画像の列と行と各ピクセルのRGB値を含む3Dマトリックスです。基本FreeMat/MATLAB構文 - 寸法誤差

RGBピクチャをYIQに変換するための組み込み関数がないため、実装しています。私はこのコードを思い付いた:行列の乗算が失敗した理由を私は理解できない

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; 

は、私は、3D配列、image_rgbを持っていると仮定します。私はコードを素敵にするだけでなく、行列を手で掛けたいと思っています...

+1

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

+1

原則として行列の乗算がどのように機能するのか理解していますか?あなたはどのようにエラーメッセージを解釈しましたか?あなたが問題にしなければならない行では、実際に行列と3D配列を乗算しようとしています。 btw:サイズ(mat、n)を使用して、長さ(mat(:、1,1))または長さ(mat(1、:、1))ではなく次元nに沿ってマットのサイズを取得できます。そして、マット(1:サイズ(マット、1)、マット(1:サイズ(マット、2)、:)はマットと同じです:(マットと同じです。 as image_rgb。 –

答えて

2

matrixあなたが作成した3D配列は、適切な行列乗算ではない倍数で表示しようとします。画像データを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は、RGB画像の3になることに注意してください。各ステートメントの後の行列のサイズを参照してください。画像をdoubleに変換することを忘れないでください。

関連する問題