Tuesday 17 July 2018

Install OpenCV on Ubuntu

Regarding which Python version you should use…I’m not getting into that argument. I’ll simply say that you should use whichever version of Python you are comfortable with and use on a daily basis. Keep in mind that Python 3 is the future — but also keep in mind that porting Python 2.7 code to Python 3 isn’t terribly challenging either once you understand the differences between the Python versions. And as far as OpenCV goes, OpenCV 2 doesn’t care which version of Python you’re using: the bindings will work just the same.

Install OpenCV dependencies on Ubuntu

Most (in fact, all) steps in this tutorial will be accomplished by using your terminal. To start, open up your command line and update the aptget  package manager to refresh and upgrade and pre-installed packages/libraries:
$ sudo apt-get update
$ sudo apt-get upgrade
Next, let’s install some developer tools:
$ sudo apt-get install -y build-essential
$ sudo apt-get install -y cmake
$ sudo apt-get install -y libgtk2.0-dev
$ sudo apt-get install -y pkg-config
$ sudo apt-get install -y python-numpy python-dev
$ sudo apt-get install -y libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install -y libjpeg-dev libpng-dev libtiff-dev libjasper-dev
$ sudo apt-get -qq install libopencv-dev build-essential checkinstall cmake pkg-config yasm libjpeg-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libdc1394-22-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev python-dev python-numpy libtbb-dev libqt4-dev libgtk2.0-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils

Download the OpenCV source

At the time of this article’s publication, the most recent version of OpenCV is 2.4.11 , which we download a .zip  of and unarchive using the following commands:
$ wget http://downloads.sourceforge.net/project/opencvlibrary/opencv-unix/2.4.11/opencv-2.4.11.zip
$ unzip opencv-2.4.11.zip
Note: You might need to expand the commands above using the “<=>” button during your copy and paste. The .zip  in the 3.1.0.zip  may be cutoff in smaller browser windows. For convenience, I have included the full URL of both the opencv  archive as well as the opencv_contrib  archive below:
I also want to mention that both your opencv  and opencv_contrib  versions should be the same (in this case, 3.1.0 ). If the versions numbers do not matchup, you could very easily run into compile time errors (or worse, runtime errors that are near impossible to debug).
After unzipping open that directory and create new directory name as “release”
$ cd opencv-2.4.11
$ mkdir release
$ cd release
After this compile and install the openCV using bellow command.
$ cmake -G "Unix Makefiles" -D CMAKE_CXX_COMPILER=/usr/bin/g++ CMAKE_C_COMPILER=/usr/bin/gcc -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_TBB=ON -D BUILD_NEW_PYTHON_SUPPORT=ON -D WITH_V4L=ON -D INSTALL_C_EXAMPLES=ON -D INSTALL_PYTHON_EXAMPLES=ON -D BUILD_EXAMPLES=ON -D WITH_QT=ON -D WITH_OPENGL=ON -D BUILD_FAT_JAVA_LIB=ON -D INSTALL_TO_MANGLED_PATHS=ON -D INSTALL_CREATE_DISTRIB=ON -D INSTALL_TESTS=ON -D ENABLE_FAST_MATH=ON -D WITH_IMAGEIO=ON -D BUILD_SHARED_LIBS=OFF -D WITH_GSTREAMER=ON ..
$ make all -j4 # 4 cores
$ sudo make install

Creating your Python virtual environment

If you decide to use Python 2.7, use the following command to create a Python 2.7 virtual environment:
$ mkvirtualenv cv -p python2
Otherwise, use this command to create a Python 3 virtual environment:
$ mkvirtualenv cv -p python3

Verifying that you are in the “cv” virtual environment

If you ever reboot your Ubuntu system; log out and log back in; or open up a new terminal, you’ll need to use the workon  command to re-access your cv  virtual environment. An example of the workon  command follows:
$ workon cv

Testing your OpenCV install

To verify that your installation is working:
  1. Open up a new terminal.
  2. Execute the workon  command to access the cv  Python virtual environment.
  3. Attempt to import the Python + OpenCV bindings.
I have demonstrated how to perform these steps below:
raghavareddy@raghavareddy:~$ python
Python 2.7.3 (default, Oct 26 2016, 21:01:49) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import cv2
>>> cv2.__version__
'3.1.0'
>>>

No comments:

Post a Comment