Calling matrix multiplication from python

I’m wondering if there is a way to access the matrix multiply operators from python.

Specifically, the C++ documentation mentions the ability to use matrix multplication that is different than mul.

https://docs.opencv.org/4.x/d3/d63/classcv_1_1Mat.html#a385c09827713dc3e6d713bfad8460706

Unfortunately, I cannot find any documentation on the function being exported to the cv2 namespace in python.

The main advantage I find is it would help bring users to C++ performance to python.

I believe that for my application, I found the cv2::transform function,
https://docs.opencv.org/4.x/d2/de8/group__core__array.html#ga393164aa54bb9169ce0a8cc44e08ff22

Maybe all that is needed a little more cross referencing, but I’m hoping that matmul might be a good thing to expose in python:

  • Convert to opencv cv2::Mat
  • Apply the * operator
  • Return result

Is there a version of the * operator that supports an output parameter? Is that gemm?

there is no export because numpy is a good library for matrix operations
If you want in C++:

    Mat A = (Mat_<float>(2, 3) << 0, 2, 3, 1, 5, 7);
    Mat B = (Mat_<float>(2, 3) << 1, 2, 3, 3, 2, 1);
    Mat C = A.mul(2 / B);

python code is

import numpy as np
A = np.array([[0, 2, 3],[1, 5, 7]], dtype=np.float32)
B = np.array([[1, 2, 3],[3, 2, 1]], dtype=np.float32)
C = A*2/B

About GEMM and performance, see:

Also, with NumPy do not write iteration loops but rather use the different NumPy functions instead and install a BLAS library.

(Actually it is similar in C++. If you are doing large matrix multiplication, you better have to install a BLAS library like Intel MKL or OpenBLAS.)

You can also call the BLAS routines directly from OpenCV but as previously stated there isn’t any point because to get the same performance as numpy you need to build OpenCv against a BLAS library and as images are stored as numpy arrays in python you can just use numpy. See below for a comparison of GEMM from python

Sorry for the late reply on my behalf. this is a new forum for me. I really do appreciate all your replies.

Ultimately, I feel like numpy is well versed for floating point operations, but falls short for integer operations when overflow and memory consumption is a concern (a very common concern for image processing).

Honestly, I didn’t know that numpy was able to use gemm with syntax like:

npMat3 = npMat4 = npMat5 = npTmp + npTmp*1j
%timeit npMat3.T @ npMat4 + npMat5

I’ll have to get back to you all with real benchmarks it seems.

if you are concerned about numpy producing a lot of temporary data, just write your kernels in plain python and then use @numba.njit

or write OpenCL kernels and run them with pyopencl

or use OpenCV functions, with cv.UMat, but then you’re back to intermediate results for a bunch of stuff, same as with numpy.

I hear there exist GPU-accelerated flavors of numpy too