Ask Your Question
1

Pixel-wise matrix multiplication

asked Dec 3 '0

fadeto404 gravatar image

updated Dec 3 '0

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?

Preview: (hide)

1 answer

Sort by » oldest newest most voted
2

answered Dec 3 '0

berak gravatar image

updated Dec 3 '0

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.

Preview: (hide)

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 (Dec 3 '0)edit
2

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

Der Luftmensch gravatar imageDer Luftmensch (Dec 3 '0)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 (Dec 3 '0)edit

it applies the matrix multiplication on the pixel coordinates

no, to the pixel values

berak gravatar imageberak (Dec 3 '0)edit

Question Tools

1 follower

Stats

Asked: Dec 3 '0

Seen: 1,697 times

Last updated: Dec 03 '20