Home » OpenCV

Tag: OpenCV

How to install OpenCV from source on Ubuntu PC

Remark: This tutorial describes how to install OpenCV 2.4.8 on a Ubuntu 14.04 LTS PC, but it can be easily adapted for other versions of OpenCV and/or Linux distributions.

Step 1: Create a new script

All required steps to install OpenCV will be written in a script called OpenCVSetup.sh (assuming you do not have a file with that name in the current folder already).

To create the file write on the command line the following (excluding the “$” symbol):
$ touch OpenCVSetup.sh

Step 2: Install dependencies and OpenCV

Open the script using your favourite text editor. Assuming this is vi(m) write on the command line the following (excluding the “$” symbol):
$ vim OpenCVSetup.sh

Next write the contents of the script (i.e. copy and paste the text below into the script):

/scripts/blob/master/scripts/linux/OpenCVSetup.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#!/bin/bash
 
###########################################################
#
#
# OpenCV 2.4.8 setup
#
#
###########################################################
 
# Starting setup of OpenCV 2.4.8
echo "Setting up OpenCV 2.4.8..."
 
 
#----------------------------------------------------------
# Installing dependent packages
#----------------------------------------------------------
 
# Inform the user about the next action
echo "Installing the dependent packages libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip..."
 
# Execute the action
sudo apt-get -y install libopencv-dev build-essential cmake git libgtk2.0-dev pkg-config python-dev python-numpy libdc1394-22 libdc1394-22-dev libjpeg-dev libpng12-dev libtiff4-dev libjasper-dev libavcodec-dev libavformat-dev libswscale-dev libxine-dev libgstreamer0.10-dev libgstreamer-plugins-base0.10-dev libv4l-dev libtbb-dev libqt4-dev libfaac-dev libmp3lame-dev libopencore-amrnb-dev libopencore-amrwb-dev libtheora-dev libvorbis-dev libxvidcore-dev x264 v4l-utils unzip
 
 
#----------------------------------------------------------
# Installing OpenCV
#----------------------------------------------------------
 
# Inform the user about the next action
echo "Downloading and installing the OpenCV 2.4.8 library..."
 
# Constant values definitions
FOLDER_NAME="OpenCV2.4.8"
 
# Create a new folder for storing the source code
mkdir ${FOLDER_NAME}
 
# Change directory
cd ${FOLDER_NAME}
 
# Download source code
wget http://sourceforge.net/projects/opencvlibrary/files/opencv-unix/2.4.8/opencv-2.4.8.zip
 
# Extract archive
unzip opencv-2.4.8.zip
 
# Change directory
cd opencv-2.4.8
 
# Create a build directory
mkdir build
 
# Change directory
cd build
 
# Build the project using CMake
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 WITH_QT=ON -D WITH_OPENGL=ON ..
 
# Compile the project
make -j `nproc`
 
# Install the libraries in the appropriate folder
sudo make install
 
# Add the OpenCV libraries path to the default Ubuntu library search path
sudo /bin/bash -c 'echo "/usr/local/lib" > /etc/ld.so.conf.d/opencv.conf'
 
# Update the default Ubuntu library search paths
sudo ldconfig
 
# Return to the parent directory
cd ../../../
 
# Inform user that OpenCV 2.4.8 was successfully installed
echo "OpenCV 2.4.8 was successfully installed."

The comments in the script should provide enough details to understand what each instruction does.

Step 3: Execute script

Add execution permission to the script OpenCVSetup.sh using the following (excluding the “$” symbol):
$ chmod +x OpenCVSetup.sh

Finally execute the script as below (excluding the “$” symbol):
$ ./OpenCVSetup.sh