How to distinguish opencv3 from opencv4 in your project without conflict

Next to the previous blog, continue to analyze:
Opencv3 and opencv4 are installed on the computer at the same time, so the problem is how to write the CmakeLists.txt file when you are working on a project, when you are using opencv3, and when you are using opencv4, how to write the CmakeLists.txt file, and see the next explanation!!!!!!
Explain with the example of running the project described in the eighth lecture of the second edition of Gaobo:
When using opencv3, CmakeLists.txt should be written as follows: (The specific syntax details are more familiar to you than others)

cmake_minimum_required(VERSION 2.8)
project(ch8)

set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
#It is best to use the-march=native Remove to prevent memory errors
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})

Of course, if the above code is not modified to open cv4's cmakeList, it will certainly be compiled. Here's just how to write two opencv versions of cmakeLists files separately.
Here's how to write 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})

Unlike opencv3, there are several lines of code:

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)

Where set (${OpenCV_INCLUDE_DIRS}'/home/s/opencv_4_install/include/opencv4/opencv2/') set (${OpenCV_LIBS}'/home/s/opencv_4_install/lib/')
These two sentences are meant to allow the compiler to find the header and library files for opencv4.1.1, because we installed opencv4 under the path of / home/s/opencv_4_install (referring to the previous blog post, the installation path was set to its own path) instead of the default path (the default path is / usr/local/), so we need to reset two environment variables to prevent system silenceFind the header and library files for opencv3.I will not repeat the sentence find_package(OpenCV 4.1.1 REQUIRED).
This allows you to switch opencv3 and opencv4 back and forth freely.
Last but not least, compile if error:'CV_GRAY2BGR'was not declared in this scope
Just add #include <opencv2/imgproc/types_c.h> to the cpp file to compile perfectly

Six original articles were published. Approved 0. Visits 87
Private letter follow

Keywords: OpenCV

Added by jawapro on Mon, 03 Feb 2020 04:23:13 +0200