Ubuntu install OpenCV

本版討論物聯網 IoT Platform 技術與應用
文章: 38
註冊時間: 2017-12-08, 23:00

Ubuntu install OpenCV

文章 inho » 2018-02-02, 15:45

How to Install OpenCV in Ubuntu for C/C++
http://www.codebind.com/cpp-tutorial/in ... buntu-cpp/
https://www.pyimagesearch.com/2016/04/1 ... -opencv-3/

Step 1 – Updating Ubuntu
$ sudo apt-get update
$ sudo apt-get upgrade

upgrade 結果如果出現以下問題
E: Sub-process /usr/bin/dpkg returned an error code (1),解決辦法:
$ sudo mv /var/lib/dpkg/info /var/lib/dpkg/info.bak //將現有info 資料夾更名
$ sudo mkdir /var/lib/dpkg/info //並再新建立一個info 資料夾,再重新執行 update 及 upgrade
$ sudo apt-get update
$ sudo apt-get upgrade
Step 2 – Install dependencies
$ sudo apt-get install build-essential cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
$ sudo apt-get install python3.5-dev python3-numpy libtbb2 libtbb-dev
$ sudo apt-get install libjpeg-dev libpng-dev libtiff5-dev libjasper-dev libdc1394-22-dev libeigen3-dev libtheora-dev libvorbis-dev libxvidcore-dev libx264-dev sphinx-common libtbb-dev yasm libfaac-dev libopencore-amrnb-dev libopencore-amrwb-dev libopenexr-dev libgstreamer-plugins-base1.0-dev libavutil-dev libavfilter-dev libavresample-dev
install some developer tools, including CMake, which helps us configure the OpenCV build process:
$ sudo apt-get install build-essential cmake pkg-config

install some image I/O packages that allow us to load various image file formats from disk. Examples of such file formats include JPEG, PNG, TIFF, etc.:
$ sudo apt-get install libjpeg-dev libtiff5-dev libjasper-dev libpng12-dev

we also need video I/O packages. These libraries allow us to read various video file formats from disk as well as work directly with video streams:
$ sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
$ sudo apt-get install libxvidcore-dev libx264-dev

The OpenCV library comes with a sub-module named highgui which is used to display images to our screen and build basic GUIs. In order to compile the highgui module, we need to install the GTK development library:
$ sudo apt-get install libgtk2.0-dev

Many operations inside of OpenCV (namely matrix operations) can be optimized further by installing a few extra dependencies:
$ sudo apt-get install libatlas-base-dev gfortran

Lastly, let’s install both the Python 2.7 and Python 3 header files so we can compile OpenCV with Python bindings:
$ sudo apt-get install python2.7-dev python3-dev
// 從 source 安裝 openCV 需要使用 cmake,raspberry PI 系統預設並沒有 cmake,因此,要先安裝cmake。
參考網站:http://osdevlab.blogspot.tw/2015/12/how ... e-for.html
$ mkdir Download
$ cd Download
$ wget https://cmake.org/files/v3.10/cmake-3.10.2.tar.gz
$ tar -xvzf cmake-3.10.2.tar.gz
Compile and install cmake
pi@raspberrypi ~/Download $ cd cmake-3.10.2/
pi@raspberrypi ~/Download/cmake-3.10.2 $ sudo ./bootstrap
pi@raspberrypi ~/Download/cmake-3.10.2 $ sudo make
pi@raspberrypi ~/Download/cmake-3.10.2 $ sudo make install

Step 3 – Get OpenCV
$ sudo -s
$ cd /opt
/opt$ sudo git clone https://github.com/Itseez/opencv.git
/opt$ sudo git clone https://github.com/Itseez/opencv_contrib.git

Step 4 – build and install OpenCV
/opt$ cd opencv
/opt/opencv$ sudo mkdir release
/opt/opencv$ cd release
/opt/opencv$ sudo make -j4
/opt/opencv$ sudo make install
/opt/opencv$ ldconfig
/opt/opencv$ exit
/opt/opencv$ cd ~

Now to check if OpenCV is installed on a machine, run the following commands
$ pkg-config --modversion opencv

We will get the opencv version installed
in this case it’s 3.4.0

Create a C++ program Follow the commands
$ cd ~
$ mkdir cpp_test
$ cd cpp_test
$ vi main.cpp

The above command will create a folder called cpp_test and create a main.cpp file inside it
Now place any .jpeg image inside the cpp_test folder.
So Now your cpp_test folder will contain two files as follows
├── sample.jpeg
└── main.cpp

Now open the main.cpp and add the following code

#include <opencv2/highgui.hpp>
#include <iostream>
using namespace std ;
// ==========================================
int main( int argc, char** argv )
cv::Mat image ;
image = cv::imread("sample.jpeg" , CV_LOAD_IMAGE_COLOR) ;

if (! image.data )
cout << "Could not open or find the image" << endl ;
return -1 ;

cv::namedWindow( "Display window", cv::WINDOW_AUTOSIZE ) ;
cv::imshow( "Display window", image ) ;

cv::waitKey(0) ;
return 0;
Now compile your code with the following command
$ g++ main.cpp -o output `pkg-config --cflags --libs opencv`

Now run the C++ program with the following command

$ ./output