2

First, an example:

In[1]: import numpy as np
  ...: import cv2
  ...: arr = np.random.rand(2000, 2000, 3).astype(np.float32)
In[2]: %timeit cv2.cvtColor(arr, cv2.COLOR_RGB2HSV)
13.1 ms ± 758 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In[3]: %timeit arr*2
13.8 ms ± 172 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)

For some reason, this trivial operation in numpy takes longer than performing a full conversion of color model. I'm looking to do realtime image analysis using OpenCV to do most of the heavy lifting and using Numpy to perform some glue operations, but given this it appears like somehow Numpy will be consuming most of the processing time. Is there a reason for this? Is there a way to speed up Numpy to make it on par with OpenCV? Is there ways to use whatever speedups OpenCV provides for general array operations?

Sebastian Mendez
  • 2,859
  • 14
  • 25
  • If you want heavy power for mathematical computations you should definitely resort to a GPU-oriented library – rafaelc Sep 11 '19 at 17:37
  • Take a look [here](http://numba.pydata.org/numba-doc/0.12/tutorial_numpy_and_numba.html#the-vectorize-decorator) for a quick example – rafaelc Sep 11 '19 at 17:38
  • 1
    It's worth noting that numpy operations are far more generalizable, and therefore there is likely more overhead in determining data type and shape, while cv2 has the benefit of being able to accept only certain input within certain dtype and shape requirements, giving it the edge in imaging specific use-cases – G. Anderson Sep 11 '19 at 17:44
  • 5
    OpenCV was written to showcase Intel’s SIMD instructions and derives most of its performance from using them - SSE2/3, AVX etc. – Mark Setchell Sep 11 '19 at 18:32
  • See line 380 onwards for a taster here... https://github.com/opencv/opencv/blob/master/modules/core/src/mathfuncs.cpp – Mark Setchell Sep 11 '19 at 18:54
  • Numpy also uses SIMD: https://github.com/numpy/numpy/blob/master/numpy/core/src/umath/simd.inc.src, though it's possible it just doesn't utilize it as well as opencv does. – Sebastian Mendez Sep 11 '19 at 21:08
  • 1
    What do you expect? Both operations are not really complicated. More than half of the time is spent with memory allocation for the output, compare `out= np.empty_like(arr); %timeit np.multiply(arr,2.,out=out)`, the remaining part are quite simple mathematical operations. This operations are both limited by the costly memory allocation and memory throughput. – max9111 Sep 12 '19 at 16:14
  • Side note and self-plug: watch out because sometimes OpenCV cuts corners with precision https://stackoverflow.com/questions/42880897/opencv-remap-interpolation-error – Andras Deak -- Слава Україні Sep 17 '19 at 21:18
  • As I stated, I'm using OpenCV/Numpy for image processing, and it appears that the optimizations that OpenCV makes only become realized when you're using it for math and not for processing an image. However, this does explain some of the speedups. – Sebastian Mendez Sep 18 '19 at 05:06
  • Yeah, I'm pretty sure using it for actual image processing is fine. – Andras Deak -- Слава Україні Sep 18 '19 at 12:48

0 Answers0