接着上一篇博客继续分析:
在电脑上同时安装了opencv3和opencv4,那么问题来了,在做项目的时候,使用opencv3的时候CmakeLists.txt文件如何书写,使用opencv4的时候CmakeLists.txt文件又该如何书写呢,且看接下来的讲解!!!!!!
以运行高博第二版十四讲中的第八讲的工程的为例进行讲解:
使用opencv3时,CmakeLists.txt应该这样书写:(具体的语法细节相比大家已经非常熟悉了)
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
#使用时最好将-march=native去掉,防止报内存错误
set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
find_package(OpenCV 3.0.0 REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS})
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
当然了,上述代码如果不修改成opencv4的cmakeList肯定是编译不过了,在这里只是告诉大家如何分别书写两个opencv版本的cmakeLists文件。
下面是opencv4的书写方法:
cmake_minimum_required(VERSION 2.8)
project(ch8)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++11 ${SSE_FLAGS} -g -O3 -march=native")
set(${OpenCV_INCLUDE_DIRS} "/home/s/opencv_4_install/include/opencv4/opencv2/")
set(${OpenCV_LIBS} "/home/s/opencv_4_install/lib/")
find_package(OpenCV 4.1.1 REQUIRED)
find_package(Sophus REQUIRED)
find_package(Pangolin REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
${Pangolin_INCLUDE_DIRS}
)
add_executable(optical_flow optical_flow.cpp)
target_link_libraries(optical_flow ${OpenCV_LIBS})
add_executable(direct_method direct_method.cpp)
target_link_libraries(direct_method ${OpenCV_LIBS} ${Pangolin_LIBRARIES})
与opencv3不同的就是以下几句代码:
set(${OpenCV_INCLUDE_DIRS} "/home/s/opencv_4_install/include/opencv4/opencv2/")
set(${OpenCV_LIBS} "/home/s/opencv_4_install/lib/")
find_package(OpenCV 4.1.1 REQUIRED)
其中set(${OpenCV_INCLUDE_DIRS} "/home/s/opencv_4_install/include/opencv4/opencv2/") set(${OpenCV_LIBS} "/home/s/opencv_4_install/lib/")
这两句话是为了让编译器可以找到opencv4.1.1的头文件和库文件,因为我们是把opencv4安装在/home/s/opencv_4_install(参考的上一篇博文,安装路径设置成自己的路径)这个路径下,而不是默认的路径(默认路径为/usr/local/)下所以需要重新设置两句环境变量,防止系统默认去找opencv3的头文件和库文件。find_package(OpenCV 4.1.1 REQUIRED)
这句话我就不再赘述了。
至此就可以实现opencv3和opencv4的来回自如的切换了。
最后罗嗦一下,编译如果报error: ‘CV_GRAY2BGR’ was not declared in this scope
的错误
只需要在cpp文件中加入#include <opencv2/imgproc/types_c.h>
这句话即可完美编译