Installing OpenCV has never been easy and always required a lot of careful usage of the command line to build from source. This was especially painful when working with a Raspberry Pi as building and installing OpenCV took a lot of time on the RPi, especially on the older models. Luckily this has changed very recently as it is now possible to install OpenCV with pip!
Below I guide you through the basic steps that I think are necessary to get opencv to work nicely on Mac, Ubuntu and Raspberry Pi. If you want more background information, see the excellent article by Adrian Rosebrock from PyImageSearch.com.
Pip is the main package manager for python that we will also use to install opencv. Pip should already be installed on your system (see here), but if it’s not, we can install it with wget. Open a Terminal window and enter:
Now to install pip for Python 3 enter:
sudo python3 get-pip.py
Or if you are still working with Python 2.7, simply enter:
sudo python get-pip.py
Next, and for the Raspberry Pi only, we need to install some additional packages. Make sure apt-get is fully up to date by entering the following in Terminal:
sudo apt-get update
Now install the prerequisites:
sudo apt-get install libhdf5-dev libhdf5-serial-dev
sudo apt-get install libqtwebkit4 libqt4-test
sudo apt-get install libatlas-base-dev libjasper-dev libqtgui4 python3-pyqt5
Finally, we can enter a wonderfully simple command to install opencv:
sudo pip install opencv-contrib-python
For those of you also used to installing OpenCV manually I am sure you will be as happy as I am! Now let’s just make sure that OpenCV is working. Open a terminal window and enter
python3 to start Python. Now to make sure you have installed OpenCV correctly enter:
Your terminal window should look like:
Python 3.5.3 (default, Sep 27 2018, 17:25:39)
[GCC 6.3.0 20170516] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
You are done! If you have an problem with installing or if anything is unclear, please leave a comment below!
Update 1: If you are using Python 2.7 and get an error message such as Could not find a version that satisfies the requirement opencv-contrib-python (from versions: ) No matching distribution found for opencv-contrib-python, a quick solution is to not use pip but apt-get instead:
sudo apt-get install python-opencv