User Tools

Site Tools


opencv_install_ubuntu

Installing OpenCV 3.2.0 With Ubuntu 16.04


Author: Alex Cater
Email: cater@unlv.nevada.edu

The following tutorial will go over installing the latest version of opencv (3.2.0) on the latest version of Ubuntu (16.04). Below will be a video tutorial going step-by-step on how to do this exactly. The example file and the script file used in the video are shown below. Copy and past each of these fragments into individual files as they will be used later. It may be best to watch the video first before doing this just so you know where exactly each file needs to be.

Part 1: Get the necessary Files

  • OpenCV zip files can be downloaded at their website at: opencv.org
  • The script file to build opencv files can be copied here.
script.sh
version="$(wget -q -O - http://sourceforge.net/projects/opencvlibrary/files/opencv-unix | egrep -m1 -o '\"[0-9](\.[0-9]+)+' | cut -c2-)"
echo "Installing OpenCV" $version
mkdir OpenCV
cd OpenCV
echo "Removing any pre-installed ffmpeg and x264"
echo "sudo apt-get remove x264 libx264-dev"
echo "***********************************"
echo "Installing Dependenices"
sudo apt-get install libopencv-dev
echo "************Build Tools***********************"
echo "<----------------------------------------------------Build Tools------------------------------------------------->"
sudo apt-get install build-essential checkinstall cmake pkg-config
echo "*_*_*_*_*_*_*_*_*_*_*_*_*_*"
echo "<------------------------------------------------------Image I/O----------------------------------------------------->"
sudo apt-get install libtiff5-dev libjpeg-dev libjasper-dev libpng-dev zliblg-dev libwebp-dev libopenexr-dev libgdal-dev
echo "***********************************"
echo "<--------------------------------------------------------Video I/O--------------------------------------------------->"
sudo apt-get install libavcodec-dev libavformat-dev libmp3lame-dev
sudo apt-get install libswscale-dev libdc1394-22-dev libxine-dev
sudo apt-get install libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev
sudo apt-get install libv4l-dev v4l-utils libfaac-dev libopencore-amrnb-dev
sudo apt-get instal libopencore-amrwb-dev libtheora-dev libvorbis-dev
sudo apt-get insta libxvidcore-dev libx264-dev x264 yasm
echo "***********************************"
echo "Parallelism and linear algebra libraries"
sudo apt-get install libtbb-dev libeigen3-dev
echo "***********************************"
echo "<------------------------------------------------for GUI------------------------------------------------->"
sudo apt-get install libqt4-dev libgtk2.0-dev qt5-default
echo " sudo apt-get install libvtk6-dev"
echo "*************************************************************************************************************"
echo "<--------------For JAVA-------------------->"
echo sudo apt-get install ant default-jdk
echo "<-------********------For Python------********-------->"
echo sudo apt-get install python-dev python-tk python-numpy python3-dev python3-tk python3-numpy python-matplotlib
sudo apt-get install python-opencv
echo "%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%_%"
echo "Downloading OpenCV" $version
wget -O OpenCV-$version.zip http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/$version/opencv-"$version".zip/download
echo "Installing OpenCV" $version
unzip OpenCV-$version.zip
cd opencv-$version
mkdir build
cd build
echo "*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_*_"
cmake -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 BUILD_opencv_java=ON \
	-D INSTALL_C_EXAMPLES=ON \
	-D INSTALL_PYTHON_EXAMPLES=ON \
	-D BUILD_DOCS=ON \
	-D BUILD_EXAMPLES=ON \
	-D WITH_QT=ON \
	-D WITH_OPENGL=ON \
	-D WITH_EIGEN=ON ..
make -j4
echo "***********************************"
sudo make install
echo "***********************************"
sudo sh -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
echo "***********************************"
sudo ldconfig
echo "OpenCV" $version "ready to be used"



  • The example file that will be run can be copied from here:
test.cpp
#include <stdio.h>
#include <opencv2/opencv.hpp>
using namespace cv;
int main(int argc, char** argv )
{
    if ( argc != 2 )
    {
        printf("usage: DisplayImage.out <Image_Path>\n");
        return -1;
    }
    Mat image;
    image = imread( argv[1], 1 );
    if ( !image.data )
    {
        printf("No image data \n");
        return -1;
    }
    namedWindow("Display Image", WINDOW_AUTOSIZE );
    imshow("Display Image", image);
    waitKey(0);
    return 0;
}



Part 2: The process


Above is a video tutorial on how to install, build and execute opencv and an example program. Below is the link to youtube if needed.
OpenCV installation on Ubuntu 16.04

First, you will need to download the zipped opencv files from opencv.org and extract them into a file location of your choice. In that very same directory create a file named “opencv.sh” and copy and paste the first set of lines into the file. Save it and this will be what we use to build opencv.
Open up a terminal and direct yourself to the directory you just saved the script file to as well as extracted the opencv files. Enter the command: <Code:c++ linenums:3> chmod +x opencv.sh ./opencv.sh </Code>
Your opencv files should begin to be built. This will take a few minutes to build. After completion enter these lines into your terminal: <Code:c++ linenums:4> pkg-config –cflags opencv pkg-config –libs opencv </Code>
Do the above to check if you have:
-I/usr/local/include/opencv -I/usr/local/include
-L/usr/local/lib -lopencv_shape -lopencv_stitching -lopencv_objdetect -lopencv_superres -lopencv_videostab -lopencv_calib3d -lopencv_features2d -lopencv_highgui -lopencv_videoio -lopencv_imgcodecs -lopencv_video -lopencv_photo -lopencv_ml -lopencv_imgproc -lopencv_flann -lopencv_viz -lopencv_core

If you do not have these then something went wrong. Try the installation all over again and make sure you do exactly what was done in the video.
Next, go to the directory ~/opencv-3.2.0/samples$
List the contents of this folder and you'll see folders for C++, java and python. These contain sample programs for opencv. Inside the sample folder enter the commands: <Code:c++ linenums:5> cmake . make </Code>
Your samples programs will be built. Go to the file labeled “cpp” in the samples folder and you'll see executable files for example programs. We will go over a brief sample program that is not included. Copy and past the code above in part 1 into a .cpp file inside the samples/cpp directory. Also, find any photo you wish and copy it into the same directory. For example, i used a photo of a woman and saved it under woman.jpg in my samples/cpp directory. Compile the .cpp file with the command: <Code:c++ linenums:6> g++ `pkg-config –cflags opencv` example.cpp `pkg-config –libs opencv` </Code>
Note, the “`” symbol is not a “'” sybol (single parenthesis). If you use single parenthesis the command will not work. The ` symbol is located above the tab key next to the number 1. Also, it may be easier to create a script that will automatically run the command above. This command will need to be used with every program you compile.
After the program compiles, run the code. Be sure to include the path to your photo (if it is in the same directory as the .cpp file then the file itself if only needed and you don't need to include the whole path). <Code:c++ linenums:7> ./a.out woman.jpg </Code>
The program will simply display the photo onto your desktop screen. This is everything you need to get started. Future examples will be explained in future tutorials.

opencv_install_ubuntu.txt · Last modified: 2017/05/16 21:07 by dwallace