Installing OpenCV 3.2 with Python including CUDA SDK in Ubuntu 16.04

Posted on Posted in Computer Science, Computer Vision, Tutorial

Bismillah,

Assalamu’alaikum, peace be with you Readers
Do you know OpenCV Library?

OpenCV (Open Source Computer Vision Library) is released under a BSD license and hence it’s free for both academic and commercial use. It has C++, C, Python and Java interfaces and supports Windows, Linux, Mac OS, iOS and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Enabled with OpenCL, it can take advantage of the hardware acceleration of the underlying heterogeneous compute platform.

 

simplify, OpenCV library is Computer Vision library with most complete algorithm ever that could be written in many programming language and platforms. This library have been helping so much in research and industrial computer vision apps. I have read the tutorial from this site  by Adrian Rosebrock, it was great tutorial ever, so helpful but recently multicore usable for computing being popular particulary for deep learning or other machine learning algorithm, actually computer vision algorithm have been using this in long time, it proven with OpenCL and CUDA that included in OpenCV. OpenCL is support for any multicore device type CPU or GPU but CUDA isn’t, it only for Nvidia and by default OpenCL will be installed when we installing OpenCV, so in this section i wan’t to create tutorial installing OpenCV with CUDA.

Then why using Ubuntu?, Even OpenCV could be run in Windows, the experience i have, the library run slowly than linux particullary in ubuntu. Ok let’s begin

Specification Software and Hardware that i used

  • Ubuntu 16.04
  • OpenCV 3.2
  • OpenCV Contrib 3.2
  • Python 2.7
  • CUDA 8
  • Nvidia GeForce GTX 1080 Ti

#1 First, you must already Ubuntu 16.04 and installed nvidia geforce with cuda core support

 

#2 Download CUDA 8 and libcudnn in nvidia site, i recommend to download local deb file

i have downloaded with these filename

  • cuda-repo-ubuntu1604-8-0-local-ga2_8.0.61-1_amd64-deb
  • libcudnn5_5.1.10-1+cuda8.0_amd64.deb
  • libcudnn5-dev_5.1.10-1+cuda8.0_amd64.deb

Just open it without terminal, and click install

or you can install with terminal with this

then

then install libcudnn

#3 Add installation location to bashrc file

after the file open, add this following lines

then reload bashrc

#4 Install OpenCV dependencies for ubuntu

this steps are still using your ubuntu terminal

The pkgconfig  package is (very likely) already installed on your system, but be sure to include it in the above aptget  command just in case. The cmake  program is used to automatically configure our OpenCV build.

OpenCV is an image processing and computer vision library. Therefore, OpenCV needs to be able to load various image file formats from disk such as JPEG, PNG, TIFF, etc. In order to load these images from disk, OpenCV actually calls other image I/O libraries that actually facilitate the loading and decoding process.

We install the necessary ones below:

Use the following commands to install packages used to process video streams and access frames from camera

OpenCV ships out-of-the-box with a very limited set of GUI tools. These GUI tools allow us to display an image to our screen ( cv2.imshow ), wait for/record keypresses ( cv2.waitKey ), track mouse events ( cv2.setMouseCallback ), and create simple GUI elements such as sliders and trackbars. Again, you shouldn’t expect to be building full-fledged GUI applications with OpenCV — these are just simple tools that allow you to debug your code and build very simple applications.

Internally, the name of the module that handles OpenCV GUI operations is highgui . The highgui  module relies on the GTK library, which you should install using the following command:

Next, we install libraries that are used to optimize various functionalities inside OpenCV, such as matrix operations

Install python 2.7

Note, if the python not installed, when process of opencv installation the library for python support will be skipped then python won’t import opencv library

#5 Download OpenCV and OpenCV Contrib source

or you can download manually through opencv site or sourceforge

then download opencv contrib

Why we need opencv contrib? In opencv 2.4 SIFT and SURF were included in main packages but due to patent issue opencv separate it to contrib package. Then opencv main package with contrib should be in same version.

#6 Installing PIP for python

pip is python dependencies library downloader, there any library dependencies for opencv python that called numpy, it should be installed so you must install pip. Install with following lines

then install numpy

#7 Configuring, Compiling and Installing OpenCV

Note: If you are getting an error related to stdlib.h: No such file or directory during either the cmake  or make  phase of this tutorial you’ll also need to include the following option to CMake: D ENABLE_PRECOMPILED_HEADERS=OFF . In this case I would suggest deleting your build  directory, re-creating it, and then re-running CMake with the above option included. This will resolve the stdlib.h  error. Thank you to Carter Cherry and Marcin for pointing this out in the comments section!

you must check cuda detected version, opengl and python

then

the j number is depend on your processor core number

this process will take a damn long time, it doesn’t like you compile without cuda. i recommend to compile with multiple core support.

next, after compiling is finished, install the compiled file into system

#8 Testing OpenCV via python

the output will be

Finish.

You can exploring computer vision algorithm with cuda through python

Leave a Reply

Your email address will not be published. Required fields are marked *