# Pixel-wise matrix multiplication

I want to multiply every pixel in an image with a 3x3 matrix, treating every pixel as a 3D-vector of colors. In mathematical terms the operation would be something like:

u = M v

where u is the resulting pixel value, M the (3x3) matrix, and v the original pixel value.

I have searched the documentation but have not been able to find a way to do this in OpenCV. Does anybody have any suggestions?

edit retag close merge delete

Sort by ยป oldest newest most voted

opencv has operators, that allow you to write it down exactly like you did above:

Vec3f v(1,2,3);
Matx33f M(1,1,1,
2,2,2,
3,3,3);

Vec3f u = M * v;

cout << u << endl;
[6, 12, 18]


(you can also use a "normal" cv::Mat, Matx33f was just "easier to fill" in the example)

on the other hand, if it's for a whole image, you SHOULD NOT do this per-pixel, but use transform() instead.

more

The function transform() seems to operate on every pixel position, that is, it applies the matrix multiplication on the pixel coordinates. I am looking for a solution that applies the operation to the pixel values.

( 2020-12-03 06:42:16 -0600 )edit
2

berak is right. Use cv::transform, you misunderstand the documentation.

( 2020-12-03 07:08:30 -0600 )edit
1

I read the documentation again, and after trying it out I realise that you are right. I was confused as it states in the documentation that the matrix parameter m has to be either 2x2 or 2x3.

( 2020-12-03 08:07:52 -0600 )edit

it applies the matrix multiplication on the pixel coordinates

no, to the pixel values

( 2020-12-03 09:24:03 -0600 )edit

Official site

GitHub

Wiki

Documentation