I’m planning to use Python for my upcoming project as a way to understand OpenCV. C++ seems like a uphill battle with a cherry on top. Though, I’d like to learn OpenCV with C++ in the future. Does anyone want to share their personal experiences working with both languages and any issues they faced? From what I know online, C++ for speed and Python for coding quickness. However, there must some small things that each language offers to make OpenCV experience better and vice versa.
C++ for speed and Python for coding quickness
Yes, that’s correct. Especially learning C++ is much more complicated than Python.
I use both C++ and Python with OpenCV. For quick tests, prototyping, doing some simple operations Python is much faster. But when passing to a more serious development stage, stage, I prefer C++.
Python is quite fast as long as you use standard OpenCV or numpy functions. The problem is that it becomes extremely slow when you need to write your own functions, if they contain
for loops like:
for y in range (image.shape|0]): for x in range (image.shape|1]): result[y,x] = doSomethingWithPixel(image[y,x])
So try to avoid the situation above in Python.
As I said, it’s OK to use native OpenCV functions like:
result = cv2.doSomethingWithImage(image)
I agree with @kbarni. Would sum up that you should think about the deployment environment to make a better decision. A few examples: if you have a server application and python performance is good enough, you would be safe with python. On the other hand, IOT, mobile or even desktop apps would benefit the better performance of a fine tunned C++ compiled project. Also, I prefer to deliver C++ binaries to the end user, usually it will be faster, safer and more stable.
in terms of security, i.e. exploitability, python code is safer because you simply can’t make a large class of errors that you can in C++.
it can also not be claimed that python is somehow more prone to bugs or crashes than C++. in fact, the Python language is less convoluted and easier to grasp in its entirety than C++.
hiding your source code from the customer can be a valid concern. you can do that to Python code.
you shouldn’t write your own pixel manipulation loops. if you do, write an OpenCL kernel. OpenCL runs on CPUs and GPUs. if you write C++, it’ll only ever run on CPUs.
both for obfuscation and for optimization, you should look at Cython. it is a language and a compiler/translator. it is a derivative of Python that can be compiled to run as fast as C, if you add appropriate static types the compiler can optimize for. if you have to write your own pixel manipulation loops, use Cython.
I can’t recommend C++ to anyone for any purpose, and the only excuses are masochism and peer pressure. in terms of how dirty the programmer can get, it’s worse than C, perl, php, bash, … you name it, C++ is worse.
example: in C you can’t have reference counting. in Java and Python, you have garbage collection everywhere, and refcounting is implicit in that. in C++ however, if you want refcounting, you’ll have to dirty all your code with
that is visual noise. and it’s just one example of C++. this puts needless cognitive load on you and anyone who has to read that source code.
C++ is leaking abstractions all over the place. it is high level assembly language. it is what other languages are written in. you should only write C++ if your time as a human being is worth very little or someone pays you for the pain and suffering.
you should only write C++ if your time as a human being is worth very little or someone pays you for the pain and suffering
Lol, I like that. Not that I know much about C++, or python, for that matter. But it reminds me of my days doing industrial controls. Once you learned to program Siemens PLCs in statement list (a very simple almost machine code like PLC language) you never wanted to see ladder logic again.
But the customers (for good reason) wanted ladder logic. So you just stuff the good programming or obfuscate stuff into functions and they never know.
A quick reminder to all the C++ bashers in here, that when you do an ‘import cv2’ you actually reference to the C++ library (cv2.dll/.so) and all your cv2 function calls are going to the C++ API. But it’s true, for python scripting, you only need to know scripting. For C++ programming, you actually have to understand, what you are doing and the architecture you are working on.
My personal opinion (you may disagree with) is, that python is great for fast prototyping, for use case analyses, one-time data analysis or proofs-of concept. Stuff where you have an idea and want to get it to work fast without caring for architecture and type safety. But there is a very good reason, why all important and big programs, even OpenCV, are actually written in C++. I also think, that this forum is the worst place for a general ‘C++ vs. Python’ question, even if its getting narrowed down to OpenCV in the question body.
I’m fully aware of what OpenCV is written in.
the reason(s) you leave unmentioned is that it’s a compiled language that allows some level of abstraction/organization, and it’s widely known.
that makes it a pragmatic choice. that doesn’t mean it is immune from criticism or that it’s a pinnacle of human achievement.
Another point I think has to be made: image processing and computer vision are fields where visualization is critical to every stage of the research and develppment. Using python in
Jupyter-Notebooks will make your life much easier. When working with C++, there is a great Visual-Studio plugin called Image-Watch.