Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

opencv function implementation

I wonder how does opencv do operations on Matrices. For example, when I write code for

cv::add (Mat mat1, Mat mat2, Mat &result)

using two for loops, it takes around 120-130 ms for 1000x750 image. But using opencv add function it takes 6-7 ms. Does anyone know what is their trick? I want to learn it to be able to write functions that opencv doesn't have.

I have searched inside opencv and find this two .cpp files(first, second) but I dont know if I'm looking at correct place.

I just want to know how to use this power. Could somebody help me?

Thanks,

like image 373
smttsp Avatar asked Sep 19 '26 03:09

smttsp


1 Answers

The two cpp files you provided are for GPU operations (CUDA and OpenCL). From your question, I think you are looking for non-GPU operations and this is the correct file..

OpenCV is famous for its speed and it comes from a lot of optimizations they do in their codes. I will just give some hints to some of them.

1. SIMD Optimization

This is one of the major source of optimization in OpenCV. Almost all arithmetic operations are SIMD optimized. In your case also, SIMD optimization is the better option (which OpenCV has already done). It improves the performance by several times depending on the level of your implementation. All the modern day processors comes with in-built SIMD support (SSE, AVX etc).

It is a little bit complicated compared to our normal C++. Instead of adding only two pixels from both matrices at a time, you add some 16 pixels (It depends on the datatype) simultaneosly. Theoretically it provides 16x speedup. Here is a simple example which I wrote while I was learning SIMD assembly (you can use Intrinsics which are much more simpler). It is not much optimized (written just to learn it), still provides a speedup of 20x.

Similarly, for use in ARM platform, the codes are being NEON optimized (contributed mainly by Nvidia Team for their Tegra processors). Example

2. Multi-threading via TBB

Another important one is use of TBB, Some one has already mentioned it in his answer and you have to compile OpenCV source with TBB to achieve it. As he mentioned, it may not be an easy task to do. Many functions like face detection etc are TBB optimized in OpenCV.

OpenCV does some other techniques also like loop unrolling. (Example) It provides a slight improvement. Modern day compilers are already very good at this.

You can read Agner Fog's optimization techniques manuals for more details on optimizing C++ codes. All those details are relevant.

like image 109
Abid Rahman K Avatar answered Sep 21 '26 16:09

Abid Rahman K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!