There is an easy, quick, painless way to do it:
- Make sure that you install python, virtualenv … OR, just make sure python is working.
- Run this:
pip install opencv-contrib-python
That’s it… I don’t know why I go all the way to hell (as you see below to do it). So, the whole content below is just for research purpose only.
Thank you for reading
— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —
First, this document is an update and a combination of 2 documents:
- Official tutorial from OpenCV.org: Installation in Linux
- Another tutorial by Adrian Rosebrock: Ubuntu 16.04: How to install OpenCV. So, that is a super detailed tutorial, but I feel the need of combining with the official because when I install all packages recommended from the official tutorial, the installation runs smoother and have no error (though the tutorial by Adrian also does the job :) ).
I’ll try as concise as possible and get you to the final result asap.
1 final note, all the instructions are done in terminal, you should already know that, don’t you?
Alright, let’s dive right to it:
Step 1: Get ubuntu 16.04 running.
Remember to update ubuntu before doing any thing else, which is
$ sudo apt-get update && sudo apt-get dist-upgrade && sudo apt-get autoremove
Step 2: Installing all the recommended packages, which are:
$ sudo apt-get install build-essential
$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
- And recommended optional packages: (if you are an expert, you know what to install. if you are new to this like me, I just copy all of these and get it done)
$ sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev$ sudo apt-get install libxvidcore-dev libx264-dev$ sudo apt-get install libgtk-3-dev$ sudo apt-get install libatlas-base-dev gfortran$ sudo apt-get install python2.7-dev python3.5-dev
Ok. If you need to know what those packages are, please go to the articles by Adrian above. He explains everything in there. This article is to help you get the most features of openCV up and running asap.
Step 3: Download OpenCV source:
- OpenCV latest release: here. As of writing this, the latest release is 3.3.0. For the newer version, just replace the link for the source code zip file
$ cd ~$ wget -O opencv.zip https://github.com/opencv/opencv/archive/3.3.0.zip$ unzip opencv.zip
Note that you need to be at the home folder before downloading the OpenCV source, so you don’t have to change the code in the next steps of this tutorial.
- And we also install the contribution packages of OpenCV. Adrian also explains the reason very detail in his article. Here I just provide you with the update link to the contrib latest release repository and code:
$ cd ~$ wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/3.3.0.zip$ unzip opencv_contrib.zip
Step 4: Setup your environment
At this part we will install pip and virtual environment manager packages in python. I choose to work with python 3 (and you should do it, too) since python 2 is pretty old. If you want to know what is virtualenv and virtualenvwrapper, just google it, or you can read here and here.
This line below is to install pip and upgrade it to newest version
$ sudo apt-get install python-pip && pip install --upgrade pip
- We install virtualenv and virtualenvwrapper:
$ sudo pip install virtualenv virtualenvwrapper
$ sudo rm -rf ~/.cache/pip
- Then we need to add some line to .bashrc file:
$ cd ~
$ nano .bashrc
add the following lines to the bottom of the content of the file
# virtualenv and virtualenvwrapper
- Then save the file, go back to terminal. Run this line:
$ source ~/.bashrc
This line is to activate/apply what we have done to the
.bashrc file, which finalizes the installation of virtualenv and virtualenvwrapper.
- Next, let’s create a virtual environment for OpenCV, called
$ mkvirtualenv cv -p python3
So, you have create a virtual environment with the name
cv. You probably end up in the virtual environment that you have just created, which looks similar to this:
If you restart or accidentally close the terminal, type the command below:
$ workon cv
(quick tip: you want to get out of the virtual environment, type:
- the last package to install is numpy:
$ pip install numpy
Step 5: Configuring and compiling
- Double check that you are in the virtual environment
$ workon cv
- After that, we can configure our build using cmake:
$ 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 ..
- And compile OpenCV while you are in in the build folder:
$ make -j4
- Last step: install openCV (remember that you still in the build folder)
$ sudo make install
$ sudo ldconfig
Step 6: Finish your OpenCV install (oh yeah, it is not over yet)
You have installed it as I said above, right? but there still are some last work to do in order to get it done.
- Let’s look at the output of our work so far:
$ cd /usr/local/lib/python3.5/site-packages/
$ ls -l
you will see a file begin with
cv2.cpython-… .In my case, the exact filename is:
I don’t know why but the output file may go to different places, just check those places if you don’t find it in the folder above. Below is some of the place that I have experienced:
- It may go to the
dist-packagesin your python folder, which may be:
Anyway, to continue the tutorial, you would want to move it back to
we need to rename it to
$ sudo mv cv2.cpython-35m-x86_64-linux-gnu.so cv2.so
- To have OpenCV working in our virtual environment, we create a sym-link of the file to our site-packages folder in the virtual environment:
$ cd ~/.virtualenvs/cv/lib/python3.5/site-packages/
$ ln -s /usr/local/lib/python3.5/site-packages/cv2.so cv2.so
and done!! you have successfully installed openCV in your virtual environment named
Step 7: Test what you have done:
This is what you do to confirm that you have installed openCV correctly:
Then you can remove the installation files and folders:
$ cd ~
$ rm -rf opencv-3.3.0 opencv_contrib-3.3.0 opencv.zip opencv_contrib.zip
If you follow my code exactly, you will find it working just fine. If you have saved the files into different places and/or give them different names, make sure that you also make changes to the codes too.
That it! We are done :) I make this article for me to remember how I did it and hopefully it may help other people like me to have openCV up and running.
If you think this article sucks, you know what to do.
If you like this article, hit the like button.
Either ways, let me know in the comment section below.