1、安装opencv所需的库(编译器、必须库、可选库)
详细参考了 http://www.cnblogs.com/llxrl/p/4471831.html,并根据3.1做了修改。
- GCC 4.4.x or later
- CMake 2.6 or higher
- Git
- GTK+2.x or higher, including headers (libgtk2.0-dev)
- pkg-config
- Python 2.6 or later and Numpy 1.5 or later with developer packages (python-dev, python-numpy)
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
- [optional] libtbb2 libtbb-dev
- [optional] libdc1394 2.x
- [optional] libjpeg-dev, libpng-dev, libtiff-dev, libjasper-dev, libdc1394-22-dev
[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
2、从官网下载最新opencv源码(这里是3.1)
或者github
3、编译opencv
将opencv放至任意目录,解压
unzip opencv-3.1.zip
创建编译目录,编译
cd ~/opencv-3.1mkdir release cd release cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. makesudo make install
然后记得将其加入环境变量,这样有些框架(caffe)在调用opencv库的时候,可以找到它们。
(1)在 /etc/ld.so.cong.d/文件夹下建立opencv.conf,里面加入以下语句:
/usr/local/lib
(2)将/etc/bash.bashrc中最后加入下面代码:
1 PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
2 export PKG_CONFIG_PATH
View Code
如下:
(3)将Opencv中的imgcodecs__c.h和一些动态库加入到相应位置:
然后链接它们。
1 $ sudo cp imgcodecs_c.h /usr/local/include 2 $ sudo cp libopencv_imgcodecs.so /usr/local/lib 3 $ sudo cp libopencv_imgcodecs.so.3.0/usr/local/lib 4 $ sudo cp libopencv_imgcodecs.so.3.0.0/usr/local/lib 5 $ sudo ln -sf /usr/local/lib/libopencv_imgcodecs.so.3.0.0/usr/local/lib/libopencv_imgcodecs.so.3.0 6 $ sudo ln -sf /usr/local/lib/libopencv_imgcodecs.so.3.0/usr/local/lib/libopencv_imgcodecs.so 7 $ sudo ldconfig -v
4、测试opencv
(1) 创建工作目录
mkdir ~/opencv-lena cd ~/opencv-lena gedit DisplayImage.cpp
(2)编辑如下代码
1 #include <stdio.h>#include <opencv2/opencv.hpp>using namespace cv; 2 int main(int argc, char** argv ) 3 { 4 if ( argc != 2 ) 5 { 6 printf("usage: lena.out <Image_Path>\n"); 7 return -1; 8 } 9 Mat image; 10 image = imread( argv[1], 1 ); 11 if ( !image.data ) 12 { 13 printf("No image \n"); 14 return -1; 15 } 16 namedWindow("lena", WINDOW_AUTOSIZE ); 17 imshow("lena", image); 18 waitKey(0); 19 return 0; 20 }
(3)创建CMake编译文件
gedit CMakeLists.txt
写入如下内容:
cmake_minimum_required(VERSION 2.8) project( lena) find_package( OpenCV REQUIRED ) add_executable( lena lena.cpp ) target_link_libraries( lena ${OpenCV_LIBS} )
(4)编译
cd ~/opencv-lena cmake . make
(5) 执行
此时opencv-lena文件夹中已经产生了可执行文件lena,下载lena.jpg放在opencv-lena下,运行
./lena lena.jpg
(6) 结果