nimcap
nimcap

Reputation: 10493

How to multiply each channel separately with same matrix?

I have a 1 and 3 channeled Mats of the same size, call them a and img. I want to multiply each channel of img with a. And I will perform this many times, performance is an issue.

Is there a way of using the multiply() operations or multiply operator overloads to benefit from the optimizations in OpenCV? I am trying to avoid writing my own loop for performance reasons, using operators leads to much clean code too.

I don't want to repeat a three times and merge() into a single 3-channeled Mat because of performance issues.

Upvotes: 0

Views: 788

Answers (1)

Mikhail
Mikhail

Reputation: 8028

Is there a way of using the multiply() operations or multiply operator overloads to benefit from the optimizations in OpenCV?

OpenCV3 pushes the use of the cv::UMat class in place of cv::Mat. This should give you a little GPU acceleration where possible.

I am trying to avoid writing my own loop for performance reasons, using operators leads to much clean code too.

I would disagree, performance reasons is probably wrong because you will depend on whatever compilation was used to build the libs. If the lib doesn't have AVX2, you will loose performance. Further, you will be limited to OpenCV's primitives which drastically increase memory access. Specifically, each time you do something like cv::add(A,B,C) followed by cv::sqrt(C,C) you hit the memory an extra time resulting in a notable performance decrease.

It also definitely not cleaner code, more like writing scripts for an old Polish Notation calculator.

In summary, if you have performance concerns grab the .data() pointer, check if it vectorizes, and do your work in C++/CUDA/OCL.

Upvotes: 1

Related Questions