Matrix Multiplication in OpenCV

  1. Matrix Multiplication Using Asterisk Operator in OpenCV
  2. Use the multiply() Function to Multiply Two Matrices in OpenCV
Matrix Multiplication in OpenCV

This tutorial will discuss multiplying two matrices using the asterisk operator and the multiply() function of OpenCV.

Matrix Multiplication Using Asterisk Operator in OpenCV

In OpenCV, we can multiply two images using the asterisk operator. Images are stored in a matrix in OpenCV, so we can use the asterisk operator to multiply two matrices.

In this case, the multiplication will be direct, the matrix X of size (i x j) will be multiplied with a matrix Y of size (k x l), and it will produce a third matrix Z of size (i x l). For example, let’s use NumPy to create two images, one with black color and one with red color, and then multiply them using the asterisk operator and show them using the imshow() function of OpenCV.

See the code below.

import cv2
import numpy as np

img_1 = np.zeros((512,512,3), dtype=np.float32)
img_2 = np.zeros((512,512,3), dtype=np.float32)

img_2[:,:] = (0,0,255)

img_3 = img_1 * img_2

cv2.imshow("Img1", img_1)
cv2.imshow('img2', img_2)
cv2.imshow('img3', img_3)
cv2.waitKey(0)

Output:

Use The Asterisk Operator To Multiply Two Matrices in OpenCV

The multiplication result is a black image because the first image contains only zero values.

When multiplied with the second image, the result will also contain zero values. The BGR triplet value (0, 0, 0) is the color code to black color.

For successful multiplication, the number of rows of one matrix should equal the number of columns of the second matrix.

Use the multiply() Function to Multiply Two Matrices in OpenCV

Let’s use NumPy to create two images, one with black and one with red.

Then multiply using the multiply and show them using the imshow() function OpenCV.

import cv2
import numpy as np

img_1 = np.zeros((212,212,3), dtype=np.float32)
img_2 = np.zeros((212,212,3), dtype=np.float32)

img_2[:,:] = (0,0,255)

img_3 = cv2.multiply(img_1, img_2)

cv2.imshow("Img1", img_1)
cv2.imshow('img2', img_2)
cv2.imshow('img3', img_3)
cv2.waitKey(0)

The multiplication result is a black image because the first image contains only zero values.

When multiplied with the second image, the result will also contain zero values. The BGR triplet value (0, 0, 0) is the color code to black color.

Output:

Use The multiply() Function To Multiply Two Matrices in OpenCV

The result of the above two examples is the same, but it might change in certain conditions. We can also change the value of two more optional arguments of the multiply() function.

The first optional argument is used to set the scale of the output matrix. Its value is 1 by default, but we can set it to any number.

The second optional argument is used to set the data type of the output matrix. By default, the data type is set to -1, which means the output matrix will have the same data type as the input matrices.

Author: Ammar Ali
Ammar Ali avatar Ammar Ali avatar

Hello! I am Ammar Ali, a programmer here to learn from experience, people, and docs, and create interesting and useful programming content. I mostly create content about Python, Matlab, and Microcontrollers like Arduino and PIC.

LinkedIn Facebook

Related Article - OpenCV Image

  • OpenCV sobel() Function
  • OpenCV Face Recognition
  • OpenCV Background Subtraction
  • OpenCV Object Detection
  • OpenCV Edge Detection