FROM:http://www.cnblogs.com/MnsterLu/p/5858557.html
----------------------------------------------------------------------------------
1. 先下载OpenCV的源码
2. 解压到任意目录
-
unzip opencv-2.4.13.zip
3. 进入源码目录,创建release目录
-
cd opencv-2.4.13
-
mkdirrelease
4. 可以看到在OpenCV目录下,有个CMakeLists.txt文件,需要事先安装一些软件
-
sudoapt-getinstallbuild-essentialcmakelibgtk2.0-devpkg-configpython-devpython-numpylibavcodec-devlibavformat-devlibswscale-dev
5. 进入release目录,安装OpenCV是所有的文件都会被放到这个release目录下
-
cdrelease
6. cmake编译OpenCV源码,安装所有的lib文件都会被安装到/usr/local目录下
-
cmake-DCMAKE_BUILD_TYPE=RELEASE-DCMAKE_INSTALL_PREFIX=/usr/local..
7. 安装
-
sudomakeinstall
8. 测试,在某个目录下建立一个test.cpp文件
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main(int argc,char* argv[])
{
Mat image;
image = imread(argv[1],1);
if (argc != 2 || !image.data)
{
printf("No image data\n");
return -1;
}
namedWindow("Display Image",CV_WINDOW_AUTOSIZE);
imshow("Display Image",image);
waitKey(0);
return 0;
}
9. 写一个cmake的makefile,也叫CMakeLists.txt
project(test)
find_package(OpenCV required)
add_executable(test test)
target_link_libraries(test ${OpenCV_LIBS})
cmake_minimum_required(VERSION 2.8)
10. 编译+运行
-
cmake.
-
make
-
得到可执行文件test
11. 随便弄个jpg图片做个测试,注意要和上面那个可执行文件放在同一目录下面,我这里名字取的是test.jpg。
12. ./test test.jpg 如果能看到照片,那就表示成功了。
Part 2 -----------------------------------------------------------------------------------------------------------------------------------------------
CMakeLists.txt
# cmake needs this line cmake_minimum_required(VERSION 2.8) # Define project name project(opencvTest) # Find OpenCV,you may need to set OpenCV_DIR variable # to the absolute path to the directory containing OpenCVConfig.cmake file # via the command line or GUI find_package(OpenCV required) # If the package has been found,several variables will # be set,you can find the full list with descriptions # in the OpenCVConfig.cmake file. # Print some message showing some of them message(STATUS "OpenCV library status:") message(STATUS " version: ${OpenCV_VERSION}") message(STATUS " libraries: ${OpenCV_LIBS}") message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}") # Add OpenCV headers location to your include paths include_directories(${OpenCV_INCLUDE_DIRS}) # Declare the executable target built from your sources add_executable(main main.cpp) # Link your application with OpenCV libraries target_link_libraries(main ${OpenCV_LIBS})
1、find_package(OpenCV required)中的opencv一定要遵循该大小写。
因为该句话是根据OpenCV作为前缀自动去/usr/local/share/OpenCV(如果你的opencv安装时,默认前缀设置为:/usr/local时)文件夹中去找OpenCVConfig.cmake,OpenCV-config.cmake 两个文件,进而确定你要引入的opencv头文件和库函数在哪里。
2、自定义引入相应opencv版本
如果你有很多的opencv版本,尤其是一个是opencv2.x.x一个是opencv3.x.x,因为2和3在数据结构上有相对较大的变化,所以如果引入错误的版本可能导致程序的无法运行。因为find_package找默认路径下的OpenCV,但是很多时候安装多个版本的库的时候都会make install在opt目录下。、
安装在opt的文件中会有share文件夹,这里面就有咱们需要的OpenCV文件夹,所以如果要自己有选择的控制版本,则在find_package这句话前面去设置opencv的OpenCV文件夹在哪里,添加set(OpenCV_DIR /opt/opencv-2.4.11/share/OpenCV)这句话。因为我的opencv2.4.11版本install在/opt/opencv-2.4.11文件夹下。这个可以根据你install的位置去变化。