Ask Your Question
1

Pixel-wise matrix multiplication

asked 2020-12-03 05:45:30 -0600

fadeto404 gravatar image

updated 2020-12-03 07:02:10 -0600

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 flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
2

answered 2020-12-03 06:11:26 -0600

berak gravatar image

updated 2020-12-03 06:19:55 -0600

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.

edit flag offensive delete link more

Comments

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.

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

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

Der Luftmensch gravatar imageDer Luftmensch ( 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.

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

it applies the matrix multiplication on the pixel coordinates

no, to the pixel values

berak gravatar imageberak ( 2020-12-03 09:24:03 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2020-12-03 05:45:30 -0600

Seen: 1,685 times

Last updated: Dec 03 '20