1.编译环境安装:
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ sudo apt-get install build-essential
2.必须包安装:
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ sudo apt-get install cmake Git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
3.可选包安装:
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ sudo apt-get install Python-dev python-numpy libtbb2 libtbb-dev libjpeg-dev libpng-dev libtiff-dev libjasper-dev libdc1394-22-dev
4.获取OpenCV源代码
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ git clone git://github.com/Itseez/opencv.git
获取的源代码存放在/home/zhuquan下的opencv文件夹里,即源代码放在用户主目录下。
5.使用CMake从源代码安装OPENCV
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ cd ~/opencv
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv$ mkdir build
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ cd build
6.生成CMake编译配置
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ cmake -D CMAKE_BUILD_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local ..
7.从安装目录运行make
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ make -j4 #runs 4 jobs in parallel
8.安装OPENCV
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ sudo make install
9.运行测试
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ git clone https://github.com/Itseez/opencv_extra.git
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$OPENCV_TEST_DATA_PATH=~/opencv_extra
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$~/opencv/build/bin/opencv_test_core
10.检查是否已安装
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~$ pkg-config--modversion opencv
11.一个例子
首先,在在build文件下创建一个目录,并进入目录
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ mkdir test
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build$ cd test
其次,创建DisplayImage.cpp,
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build/test$touch DisplayImage.cpp
并在DisplayImage.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;
}
接着,创建cmake配置文件CMakeLists.txt,
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build/test$ touch CMakeLists.txt
并写入如下内容:
cmake_minimum_required(VERSION 2.8)
project( DisplayImage )
find_package( OpenCV REQUIRED )
include_directories( ${OpenCV_INCLUDE_DIRS} )
add_executable( DisplayImage DisplayImage.cpp )
target_link_libraries( DisplayImage ${OpenCV_LIBS} )
然后,使用CMake进行编译
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build/test$ cmake .
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build/test$ make
最后,显示结果
zhuquan@zhuquan-HP-Z230-Tower-Workstation:~/opencv/build/test$ ./DisplayImage 123.jpg #随便一张图片都行
参考:
http://docs.opencv.org/3.1.0/d7/d9f/tutorial_linux_install.html
http://docs.opencv.org/3.1.0/db/df5/tutorial_linux_gcc_cmake.html