1.由于需要,之前系统中是opencv2.4.10版本,后来安装opencv3.10版本,并支持多版本切换。通过以下命令可以查看opencv版本
- pkg-config--modversionopencv
2.如果编译好并安装了opencv3.10版本,想切换到3.10版本,可以通过如下的方式,
通过修改bashrc文件来设置PKG_ CONFIG_PATH 和LD_LIBRARY_PATH的路径来选择对应的opencv版本
- sudogedit~/.bashrc
并在文件的默认追加新编译的3.10版本的路径,也就是cmake时:CMAKE_INSTALL_PREFIX=/usr/local/opencv/3.10
- exportPKG_CONFIG_PATH=/usr/local/opencv/3.10/lib/pkgconfig
- exportLD_LIBRARY_PATH=/usr/local/opencv/3.10/lib
然后执行命令,后面用pkg-config --modversion opencv 查看opencv版本的时候就是3.10版本
- source~/.bashrc
- #cmakeneedsthisline
- cmake_minimum_required(VERSION2.8)
- set(OpenCV_DIR"/usr/local/opencv-3.1.0/build")
- #Defineprojectname
- project(myTest)
- #FindOpenCV,youmayneedtosetOpenCV_DIRvariable
- #totheabsolutepathtothedirectorycontainingOpenCVConfig.cmakefile
- #viathecommandlineorGUI
- find_package(OpenCVrequired)
- #Ifthepackagehasbeenfound,severalvariableswill
- #beset,youcanfindthefulllistwithdescriptions
- #intheOpenCVConfig.cmakefile.
- #Printsomemessageshowingsomeofthem
- message(STATUS"OpenCVlibrarystatus:")
- message(STATUS"version:${OpenCV_VERSION}")
- message(STATUS"libraries:${OpenCV_LIBS}")
- message(STATUS"includepath:${OpenCV_INCLUDE_DIRS}")
- if(CMAKE_VERSIONVERSION_LESS"2.8.11")
- #AddOpenCVheaderslocationtoyourincludepaths
- include_directories(${OpenCV_INCLUDE_DIRS})
- endif()
- #Declaretheexecutabletargetbuiltfromyoursources
- add_executable(demodemo.cpp)
- #LinkyourapplicationwithOpenCVlibraries
- target_link_libraries(demo${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的位置去变化。
cmake_minimum_required(VERSION 2.8)project( DisplayImage )#链接opencv2set(OpenCV_DIR /usr/local/share/OpenCV)find_package(OpenCV required)INCLUDE_DIRECTORIES(${OpenCV_INCLUDE_DIRS})# 添加头文件include_directories( ${OpenCV_INCLUDE_DIRS} )message(STATUS "OpenCV library status:")message(STATUS " version: ${OpenCV_VERSION}")message(STATUS " libraries: ${OpenCV_LIBS}")message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")add_executable( DisplayImage DisplayImage.cpp )target_link_libraries( DisplayImage ${OpenCV_LIBS} )