Hướng dẫn cài opencv 3.3.0

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:

  1. Official tutorial from OpenCV.org: Installation in Linux
  2. 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.

Super simple, go to ubuntu.com and download an iso, and you know what to do next [in case you don’t, here is a great start]. And here is the link to the download page for desktop: Ubuntu Desktop

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:

  • Compilers:

$ sudo apt-get install build-essential

  • required:

$ 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 $ 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 $ 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

export WORKON_HOME=$HOME/.virtualenvs source /usr/local/bin/virtualenvwrapper.sh

  • Then save the file, go back to terminal. Run this line:

$ sudo apt-get install build-essential

0

This line is to activate/apply what we have done to the

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

3 file, which finalizes the installation of virtualenv and virtualenvwrapper.

  • Next, let’s create a virtual environment for OpenCV, called $ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev 4:

$ sudo apt-get install build-essential

1

So, you have create a virtual environment with the name

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

4. You probably end up in the virtual environment that you have just created, which looks similar to this:

Look at the beginning of the line, it said

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

6, which is what/where we want to be in

If you restart or accidentally close the terminal, type the command below:

$ sudo apt-get install build-essential

2

[quick tip: you want to get out of the virtual environment, type:

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

7 ]

  • the last package to install is numpy:

$ sudo apt-get install build-essential

3

Step 5: Configuring and compiling

  • Double check that you are in the virtual environment

$ sudo apt-get install build-essential

2

  • After that, we can configure our build using cmake:

$ sudo apt-get install build-essential

5

  • And compile OpenCV while you are in in the build folder:

$ sudo apt-get install build-essential

6

  • Last step: install openCV [remember that you still in the build folder]

$ sudo apt-get install build-essential

7

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:

$ sudo apt-get install build-essential

8

you will see a file begin with

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

8In my case, the exact filename is:

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

9

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:

  1. It may go to the $ 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 0 in your python folder, which may be: - dist-packages in python 3 folderor dist-packages in python 2 folder if you use python 2.7

Anyway, to continue the tutorial, you would want to move it back to

$ 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

1

we need to rename it to

$ 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

2 :

$ sudo apt-get install build-essential

9

  • 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:

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

0

and done!! you have successfully installed openCV in your virtual environment named

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

4 .

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:

$ sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev

1

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.

Chủ Đề