Ask Your Question

Is OpenCV in C++ or C significantly faster than python?

asked 2012-10-23 13:13:58 -0600

chriszuma gravatar image

Hi all,

I've been developing a real-time augmented-reality type application in Python OpenCV, and had a rather nasty surprise when I switched from my Core i7 development machine to the Atom unit that we wanted to actually use. It turned out to be nowhere near fast enough, giving me 5-10 fps where it would easily keep up with 30 fps videos on the i7.

So my question is whether porting my application to C would help at all, or if it's just that OpenCV can't handle this kind of application. When I put my app through the Python profiler almost all of the time was being spent in cv calls, and waitKey was also taking forever. Should I just give up and look at something like Halcon?

- Chris

edit retag flag offensive close merge delete

2 answers

Sort by ยป oldest newest most voted

answered 2012-10-23 15:09:45 -0600

sammy gravatar image

updated 2012-10-24 00:17:47 -0600

  • Halcon is nowere as fast as OpenCV
  • Coding in C gives zero advantage over C++, but it may give a boost over Python. The profiler will tell you exactly how much you can gain. You will have a gain if some of the processing is done in python, bu custom code, but the OpenCV calls, as Alexander S. said, are the same.

  • The difference between i7 and Atom is the expected difference between a top-end desktop processor and a modest, low-energy chip.

Finally, the problem is not OpenCV, but the way you are using it. I've wrote complex processing that takes 3ms on a mobile processors, and saw others do the same - the price is a hard work in optimization. Take this post as an introduction to a more serious approach to optimization. On Intel chips you have SSE instructions instead of the NEON technology found on ARM devices.

edit flag offensive delete link more


Halcon is really slower? That's surprising considering they charge $700 + $200/PC for their software. I guess I'm glad I asked before I wasted a bunch of time trying to get it to work.

chriszuma gravatar imagechriszuma ( 2012-10-30 08:49:22 -0600 )edit

So is NEON or SSE better? All I'm really doing is essentially some resizing, thresholding and blob detecting based on an AOI from an external sensor. It doesn't seem like I should need an i7.

chriszuma gravatar imagechriszuma ( 2012-10-30 08:53:55 -0600 )edit

It's always worth take a look at optimizations. Maybe in your case SSE or NEON is an overkill, but you may improve the algorithm by simple means - like reducing the image resolution before any other processing takes place.

sammy gravatar imagesammy ( 2012-10-31 01:03:01 -0600 )edit

How does one actually add SSE or NEON optimization? Do I have to recompile OpenCV from source, or do something to my code?

chriszuma gravatar imagechriszuma ( 2012-10-31 07:55:53 -0600 )edit

answered 2012-10-23 21:41:52 -0600

AlexanderShishkov gravatar image

All modern code in OpenCV library is written on C++, but there are light-weight wrappers for C and Python. So these languages are very similar from optimization point of view. So you should special optimization techniques for getting fast code. Try to use SSE instructions, add parallel code, ...

edit flag offensive delete link more



In other words, there is no sense in porting Python code to the C++. OpenCV doesn't work at Python level, it simply calls C++ function, and all the pixels processing happens on native level. And the amount of work on the native level is much larger than on the Python level, because we usually process millions of pixels. BTW, that's why you can program with OpenCV in Java on Android - because everything is calculated on the native level. Thus, if somebody wants to optimize the code, just find which C++ function consumes the most time, and optimize it. And finally you can call your optimized version from Python, because the call is cheap in comparison to processing time :)

Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-10-24 02:32:53 -0600 )edit

Question Tools



Asked: 2012-10-23 13:13:58 -0600

Seen: 14,725 times

Last updated: Oct 24 '12