Multiply Image Pixels by a Matrix

Hello, Let's say I have a 3 channel image M -> [numRowx * numColumns * numChannels]. I also have a 3 by 3 matrix A (Color Conbersion).
I would like to multiply each pixel 3 channel by the matrix to have a new 3 channels pixel.
How would you do that the most efficient way without writing the actual formula by hand (Multiplying each channel of the image by a row)?
My best so far is:
rgbToYPbPrMat = [0.299, 0.587, 0.144; 0.5, -0.419, -0.081; -0.169, -0.331, 0.5];
yPbPrToRgbMat = inv(rgbToYPbPrMat);
inputImage = randi([0, 255], [256, 256, 3], 'uint8');
inputImageFP = double(inputImage) / 255; %<! Floating Point
figure;
image(inputImage);
axis image;
inputImageYPbPrFP = shiftdim(reshape((rgbToYPbPrMat * reshape(shiftdim(inputImageFP, 2), [3, (256 * 256)])), [3, 256, 256]), 1);
Thanks.

3 件のコメント

Sean de Wolski
Sean de Wolski 2012 年 8 月 22 日
I smell bsxfun. Please provide a small example with a sample image we have and a a 3x3 matrix. What would you expect as the result?
Royi Avital
Royi Avital 2012 年 8 月 22 日
Hi @Sean, Look at my code I added.
I couldn't make it work with 'bsxfun'.
Royi Avital
Royi Avital 2012 年 8 月 23 日
Anyone? Please...

サインインしてコメントする。

 採用された回答

Jan
Jan 2012 年 8 月 23 日

2 投票

I cannot check it currently, but please try:
M = rand(numRows, numColumns, 3);
T = rand(3, 3);
P = reshape(M, numRows * numColumns, 3) * T';
Result = reshape(P, numRows, numColumns, 3);

1 件のコメント

Royi Avital
Royi Avital 2012 年 8 月 23 日
Hi, Checked it, it does work and it the fastest so far (Since the Transpose works on the smaller matrix).
Do you think there's even a faster way?
Thanks.

サインインしてコメントする。

その他の回答 (0 件)

タグ

質問済み:

2012 年 8 月 22 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by