OpenCV is the go-to choice when it comes to choosing a Computer Vision/Machine Learning library. The library is written in C++ and has a native C++ interface. However, there are official bindings for Python, Java, MATLAB/OCTAVE, JavaScript, as well as numerous unofficial ones. This article is going to show you a few ways to quickly install OpenCV with pip
- the Python package manager. Alternatively, you can use conda
instead of pip
.
OpenCV Bindings
Normally, you need to understand that the original OpenCV source code comes as several parts packaged into one.
First, the build process takes the source code and output a binary that works with your system.
Then, the bindings are installed, let's call it opencv
. Official Python binding for OpenCV is named cv2
.
Whenever you call any cv2
method or function (need import cv2
first, your request will be forwarded to opencv
by cv2
, where the actual computations happen.
Install OpenCV with pip
OpenCV-Python is a pre-built OpenCV packages for Python. It is made as an effort to simplify the installation of OpenCV and is now an official OpenCV project.
You don't need to build it from source, instead, just run the following command in a terminal emulator.
pip install opencv-python
The package already contains OpenCV binaries as well as its Python bindings. You do not need to install OpenCV separately (e.g. from opencv.org).
opencv-python
has its own set of limitations, though I personally think it doesn't really matter if you're a beginner.
First, it doesn't support GPU acceleration out of the box.
The opencv-python
package also doesn't include non-free/proprietary algorithms because of open source licensing restrictions.
Install OpenCV with conda
conda is another package manager, built not just for Python but also R. It's originally part Anaconda Python distribution, meaning it's aimed to solve complex set ups and package management challenges for data scientist.
pip
installed all Python package dependencies required, whether or not those conflict with other packages previously installed. For example, a working installation of pandas
(depends heavily on NumPy to work) would suddenly stop working if pip
installs a new package that depends a different version of the NumPy library - everything would still appear to work, but different results would be returned and may confuse even experienced users. In other words, pip
will do whatever you told (install the package) even if that breaks other packages.
conda
, on the other hand, would verify the current setup to see whether dependencies that has been installed or not, and figures out how to install compatible dependencies based on that information. Otherwise, it will tell the user that what he or she wants can't be done. pip
, by contrast, will just install the package the user specified and any dependencies, even if that breaks other packages.
OpenCV is being listed as one of the officially supported packages. Suppose you've got Anaconda installed on your Linux-based system, installing it should be as easy as running the following command in any terminal emulator:
conda install opencv
Another safer approach is creating a virtual environment before actually install OpenCV. This way, you'll avoid dependencies problem. The following command will create a new myenv
virtual environment and install OpenCV into it.
conda create -n myenv opencv
We hope that the article helped you install OpenCV using either pip
or conda
successfully. You may want to check out our guide on how to fix “pip: command not found” error, which is a common error among beginners.
If you have any suggestions or spot an error in the article, feel free to leave a comment below to let us know.