1 安裝OpenCV 3.3.1
1.1 安裝依賴包
# [compiler]
sudo apt-get install build-essential
# [required]
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
# [optional]
sudo apt-get install python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
1.2 編譯
cmake
cd ~/opencv
mkdir build
cd build
# configuring
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
# without cuda 用CUDA可能會出現錯誤,並且編譯時間較長。如果不是特別需要,關掉CUDA。
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local -D WITH_CUDA=OFF ..
# specify install directory
cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/home/zhangxin/tools/opencv-3.3.1/release -D WITH_CUDA=OFF ..
很多時候我們用不多個不同版本的OpenCV,所以最好指定安裝目錄,與其他版本OpenCV避免混淆。
build
make -j8
install
make install
2 測試代碼
2.1 C++代碼
#include <iostream>
#include "opencv2/core/version.hpp"
int main(int argc, char ** argv)
{
std::cout << "OpenCV version: "
<< CV_MAJOR_VERSION << "."
<< CV_MINOR_VERSION << "."
<< CV_SUBMINOR_VERSION
<< std::endl;
return 0;
}
2.2 Makefile
CPP = g++
# OpenCV trunk
CPPFLAGS = -L/home/zhangxin/tools/opencv-3.3.1/release/libs \
-I/home/zhangxin/tools/opencv-3.3.1/release/include \
-Wl,-rpath=/home/zhangxin/tools/opencv-3.3.1/release/libs
# Opencv 2.4.8
#CPPFLAGS = -L/home/krystof/libs/opencv-2.4.8/release/installed/libs \
# -I/home/krystof/libs/opencv-2.4.8/release/installed/include \
# -Wl,-rpath=/home/krystof/libs/opencv-2.4.8/release/installed/libs
all: test
# 注意Makefile裏要用製表符
test: main.cpp
$(CPP) $(CPPFLAGS) $^ -o $@
2.3 運行
make test
./test