下载了最新的opencv源码和opencv extra modules 源码,编译
mkdir build; cd build; cmake ..
make
sudo make install
install后是将需要的头文件和库文件拷贝到/usr目录下,有时是/usr/local目录下。具体如何区分还待进一步分析
然后编译opencv extra modules中的样例代码。
cmake_minimum_required(VERSION 2.8)
SET(CMAKE_MODULE_PATH
${CMAKE_INSTALL_PREFIX}/lib/cmake/ )
SET(CMAKE_CXX_FLAGS "-std=c++0x")
find_package(aruco REQUIRED )
# 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
#MESSAGE(STATUS "Head files Directory: ${PROJECT_SOURCE_DIR}/include")
#declaration the include files directories
include_directories(${OpenCV_INCLUDE_DIRS} ${PROJECT_SOURCE_DIR}/include)
#MESSAGE(STATUS "Lib Directory: ${PROJECT_SOURCE_DIR}/lib")
# declaration the lib directories
link_directories(${PROJET_SOURCE_DIR}/lib)
include_directories(${PROJET_SOURCE_DIR}/include)
add_executable(aruco_simple aruco_simple.cpp)
target_link_libraries(aruco_simple ${aruco_LIBS} ${OpenCV_LIBS})
小技巧:
1. find_package 就是在系统环境变量指定的目录中寻找******Config.cmake和*****Config-version.cmake文件,通常在ubuntu下使用apt-get install 命令会自动在相应目录下生成这个文件。如果是手动编译源代码的话,在执行make install后也会在相应目录下生成该两个文件。如果有些package找不到,那就手动搜索这两个文件,如果找到了,那就是目录没有添加到系统变量中。
2.
message(STATUS "libraries: ${OpenCV_LIBS} message(STATUS " include path: ${OpenCV_INCLUDE_DIRS}")
上面输出的信息非常有用,可以查看编译中真正用到的库和头文件是哪些。有可能系统中有opencv2.14和opencv3共存的情况,名字相同的头文件在不同的目录下。这时需要你仔细分辨。
3. 如果Config.cmake指的不是用到的版本,例如安装了ros-indigo-opencv ,就生成/opt/ros/indigo/share/OpenCV-3.1.0-dev/OpenCVConfig.cmake文件,这个opencv的版本非常低,不适合,但ros其它节点用到了这个库,在编译以前的代码时需要该文件。这样下载了opencv的最新的代码,编译,make install,再编译sample时就不应该使用find_package,而是在CMakefile中直接将所需的头文件路径和库加进去,例如
include_directories(/usr/local/include)
target_link_libraries(aruco_simple /usr/local/lib/libopencv_aruco.so /usr/local/lib/libopencv_core.so /usr/local/lib/libopencv_video.so /usr/local/lib/libopencv_videoio.so /usr/local/lib/libopencv_highgui.so)
这样系统中就有了opencv的两个版本
4. 还有就是
SET(CMAKE_CXX_FLAGS "-std=c++0x")
SET(CMAKE_CXX_FLAGS "-std=c++11")
增加编译时的标志
或者某些环境变量缺失时,可以直接用SET增加,例如经常莫名奇妙就走丢的Eigen3
#Add Eigen3 headers location to your include paths
SET( EIGEN3_INCLUDE_DIR "/usr/local/include/eigen" )