Recently, because a project needed computer vision recognition, I started tinkering with OpenCV. Although the installation process was long and winding, the experience of using it together with Python is actually quite good. Here I record the OpenCV installation and compilation process.
System environment: Ubuntu16.04
Software installed: OpenCV3.3.0+Python2.7.12
Updating the system packages:
$ sudo apt update
$ sudo apt upgrade
Installing some developer tools:
$ sudo apt-get install build-essential cmake pkg-config
The pkg-config package is very likely already installed by default, but just in case, add it in as well. The cmake program is used later to compile OpenCV.
$ sudo apt-get install libjpeg8-dev libtiff5-dev
$ sudo apt-get install libjasper-dev libpng12-dev
The packages above are used by OpenCV to process JPEG, PNG, TIFF and other images. For video, the packages installed below can be used to process it frame by frame.
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install libv4l-dev libxvidcore-dev libx264-dev
The highgui module that handles OpenCV GUI operations depends on the GTK library, so the GTK library also has to be installed
$ sudo apt-get install libgtk-3-dev
Install the optimization module
$ sudo apt-get install libatlas-base-dev gfortran
Install the Python development related files and libraries
$ sudo apt-get install python2.7-dev python3.5-dev
Downloading the OpenCV source code:
$ cd ~
$ wget -O opencv.zip https://github.com/Itseez/openCV/archive/3.3.0.zip
$ unzip opencv.zip
You can check the new version released by OpenCV officially on Github and replace the version number before .zip in the wget command
Link: OpenCV Github
The opencv_contrib library also needs to be downloaded
$ wget -O opencv_contrib.zip https://github.com/Itseez/opencv_contrib/archive/3.3.0.zip
$ unzip opencv_contrib.zip
The version number can likewise be replaced with a new one
Link: opencv_contrib
Note that when replacing with a new version, keep the version numbers of your opencv and opencv_contrib consistent
Setting up the Python environment:
First make sure you have installed the pip tool
To keep the system environment clean, I decided to install virtualenv and virtuanenvwrapper. These tools allow you to work in a virtual environment independent of the system.
Install the related tools:
$ sudo pip install virtualenv virtualenvwrappper
$ sudo rm -rf ~/.cache/pip
Then we need to update the .bashrc in the user’s home directory: open that file and add the following content at the end
# virtualenv and virtualenvwrapper
export $WORKON_HOME=$HOME/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh
Finally, when .bashrc is executed again, an error may be reported:
/usr/bin/python: No module named virtualenvwrapper
virtualenvwrapper.sh: There was a problem running the initialization hooks.
If Python could not import the module virtualenvwrapper.hook_loader,
check that virtualenvwrapper has been installed for
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python and that PATH is
set properly.
Because ubuntu installs two versions of Python, and virtualenvwrapper was installed with pip3 while the system runs Python2 by default, some modules are missing in Python2. Part of the virtualenvwrapper.sh file is as follows:
# Locate the global Python where virtualenvwrapper is installed.
if [ "$VIRTUALENVWRAPPER_PYTHON" = "" ] then
VIRTUALENVWRAPPER_PYTHON="$(command \which python)"
fi
When the VIRTUALENVWRAPPER_PYTHON environment variable does not exist, which python is used by default
so the environment variable also needs to be added:
VIRTUALENVWRAPPER_PYTHON=/usr/bin/python3
Choose the Python version to depend on (Python2 or Python3) to create the virtual environment
$ mkvirtualenv opencvwork -p python2
Here I choose Python2 and create a virtual environment named opencvwork; the related directory is under ~/.virtualenv
Then enter the virtual environment
$ workon opencvwork
Afterwards the terminal prompt will start with a related marker
Install Numpy (inside the virtual environment)
$ pip install numpy
Configuring and compiling OpenCV:
Inside the virtual environment
$ cd ~/opencv-3.3.0/
$ mkdir build
$ cd build
$ cmake -D CMAKE_BUILD_TYPE=RELEASE \
-D CMAKE_INSTALL_PREFIX=/usr/local \
-D INSTALL_PYTHON_EXAMPLES=ON \
-D INSTALL_C_EXAMPLES=OFF \
-D OPENCV_EXTRA_MODULES_PATH=~/opencv_contrib-3.3.0/modules \
-D PYTHON_EXECUTABLE=~/.virtualenvs/cv/bin/python \
-D BUILD_EXAMPLES=ON ..
If the compilation reports an error: stdlib.h: No such file or directory
then you need to add -D ENABLE_PRECOMILED_HEADERS=OFF to the Cmake options
In the interface returned after installation, the Python section should list the Interpreter, Libraries, numpy and packages path items (corresponding to the Python version you chose)
Once you have confirmed that CMake ran without errors, you can compile OpenCV:
$ make -j4
The number 4 can be replaced with the number of CPU cores on your computer
$ make clean
$ make
After it runs successfully, OpenCV can be installed on Ubuntu:
$ sudo make install
$ sudo ldconfig
Finishing the OpenCV installation:
After all the commands above have executed successfully, the Python+OpenCV related files will be saved in /usr/local/lib/python-2.7/site-packages/
(corresponding to the Python version you chose earlier)
Then create a symbolic link:
$ cd ~/.virtualenv/opencvwork/lib/python2.7/site-packages/
$ ln -s /usr/local/lib/python2.7/site-packages/cv2.so cv2.so
Sometimes cv2.so may be under /usr/local/lib/python2.7/dist-packages
A simple check of the OpenCV installation:
$ cd ~
$ workon opencvwork
$ python
Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.3.0'
>>>
Installation successful!
At the end of the post, the official OpenCV3.3.0 tutorial is attached: OpenCV3.3.0