0%

compile and install opencv on ubuntu 16.04

Series

Guide

requirements:

  • ubuntu: 16.04
  • opencv: 3.3.0

install dependencies

1
2
3
4
5
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
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 cmake-gui

compile

1
2
3
4
5
6
7
8
9
10
11
12
git clone https://github.com/opencv/opencv.git
wget https://github.com/opencv/opencv/archive/3.1.0.zip

cd opencv-3.1.0
mkdir build
cd build && cmake-gui ..

# may take several minutes
sudo make -j8

# install to /usr/local/bin
sudo make install

check version

1
2
opencv_version
3.3.0

python cv2

1
2
3
python
>>> import cv2
>>> cv2.__version__

pip install opencv

1
2
3
4
5
6
7
workon py3
pip install opencv-contrib-python

python
>import cv2
>cv2.__version__
'3.3.0'

for virtualenv, see python virtualenv tutorial

opencv samples

1
2
3
cd samples
cmake .
make

Example

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
using namespace cv;

int main()
{
Mat image = imread("../image/cat.jpg",0);
imshow("image",image);
waitKey(0);
return 0;
}

CMakeLists.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
cmake_minimum_required(VERSION 2.8.8)

project(demo)

# Find includes in corresponding build directories
set(CMAKE_INCLUDE_CURRENT_DIR ON)

find_package(OpenCV REQUIRED COMPONENTS core highgui imgproc features2d calib3d)
include_directories(${OpenCV_INCLUDE_DIRS})

message( [opencv] ${OpenCV_INCLUDE_DIRS} )
message( [opencv] ${${OpenCV_LIBS}} )
message( [opencv] ${${OpenCV_LIBRARIES}} )


add_executable(${PROJECT_NAME}
demo.cpp
)
target_link_libraries(${PROJECT_NAME} ${OpenCV_LIBRARIES})

Reference

History

  • 20180919: created.