【高翔视觉SLAM14讲】由于opencv4版本问题所遇到的那些事的解决办法
此教程使用与《高翔视觉SLAM14讲》配套源码
主要是解决《高翔视觉SLAM14讲》配套源码中使用Opencv3与Opencv4的一些差异
@
ch7章问题
CmakeLists.txt中
find_package(OpenCV 3 REQUIRED)
==>修改为
find_package(OpenCV 4 REQUIRED)
随后创建build,然后cd build/ 后使用cmake . . 然后make这时又会有报错。
出现
/home/usr/slam/slambook2/ch7/orb_cv.cpp: In function ‘int main(int, char**)’:
/home/usr/slam/slambook2/ch7/orb_cv.cpp:16:31: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
16 | Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
| ^~~~~~~~~~~~~~~~~~~
make[2]: *** [CMakeFiles/orb_cv.dir/build.make:76:CMakeFiles/orb_cv.dir/orb_cv.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:93:CMakeFiles/orb_cv.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
这是因为由于OpenCV版本更新所致。
在旧版本的OpenCV中,可以使用CV_LOAD_IMAGE_COLOR来指定图像读取为彩色模式。
但是在新版本的OpenCV中,这个常量已经被更新为cv::IMREAD_COLOR。
不嫌弃麻烦的话直接一一将 CV_LOAD_IMAGE_COLOR 替换为cv::IMREAD_COLOR
也可以在orb_cv.cpp中加入
#define CV_LOAD_IMAGE_COLOR cv::IMREAD_COLOR
就像这样
随后继续make又会发现发现新的问题。
/home/usr/slam/slambook2/ch7/pose_estimation_2d2d.cpp: In function ‘int main(int, char**)’:
/home/usr/slam/slambook2/ch7/pose_estimation_2d2d.cpp:36:31: error: ‘CV_LOAD_IMAGE_COLOR’ was not declared in this scope
36 | Mat img_1 = imread(argv[1], CV_LOAD_IMAGE_COLOR);
| ^~~~~~~~~~~~~~~~~~~
/home/ansel/slam/slambook2/ch7/pose_estimation_2d2d.cpp: In function ‘void pose_estimation_2d2d(std::vector<cv::KeyPoint>, std::vector<cv::KeyPoint>, std::vector<cv::DMatch>, cv::Mat&, cv::Mat&)’:
/home/ansel/slam/slambook2/ch7/pose_estimation_2d2d.cpp:143:61: error: ‘CV_FM_8POINT’ was not declared in this scope
143 | fundamental_matrix = findFundamentalMat(points1, points2, CV_FM_8POINT);
| ^~~~~~~~~~~~
make[2]: *** [CMakeFiles/pose_estimation_2d2d.dir/build.make:76:CMakeFiles/pose_estimation_2d2d.dir/pose_estimation_2d2d.cpp.o] 错误 1
make[1]: *** [CMakeFiles/Makefile2:145:CMakeFiles/pose_estimation_2d2d.dir/all] 错误 2
make: *** [Makefile:91:all] 错误 2
其中36行的错误在上面以及提及,而这143行的错误其实也是一样的道理
这个问题是由于OpenCV版本更新所致。在较新的OpenCV版本中,CV_FM_8POINT已经被更改为cv::FM_8POINT。
要解决这个问题,您需要对代码进行以下修改:
将CV_FM_8POINT替换为cv::FM_8POINT,即可修复此错误。
继续
/home/ansel/slam/slambook2/ch7/pose_estimation_3d2d.cpp:64:28: error: ‘CV_LOAD_IMAGE_UNCHANGED’ was not declared in this scope
64 | Mat d1 = imread(argv[3], CV_LOAD_IMAGE_UNCHANGED); // 深度图为16位无符号数,单通道图像
|
直接将CV_LOAD_IMAGE_UNCHANGED替换为cv::IMREAD_UNCHANGED
以此类推便可解决剩下的问题
当解决了Opencv4版本问题后编译发现又不得了了,出现一大堆
不要紧张。
这表明缺少与fmt库的链接。
首先你得确保安装了fmt
直接修改CmakeLists.txt的内容
#为
cmake_minimum_required(VERSION 2.8)
project(vo1)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++11 -O2 ${SSE_FLAGS} -msse4")
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenCV 4 REQUIRED)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
include_directories(${FMT_INCLUDE_DIRS})
include_directories("/usr/local/include/fmt")
find_package(FMT REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
)
add_executable(orb_cv orb_cv.cpp)
target_link_libraries(orb_cv ${OpenCV_LIBS})
add_executable(orb_self orb_self.cpp)
target_link_libraries(orb_self ${OpenCV_LIBS})
# add_executable( pose_estimation_2d2d pose_estimation_2d2d.cpp extra.cpp ) # use this if in OpenCV2
add_executable(pose_estimation_2d2d pose_estimation_2d2d.cpp)
target_link_libraries(pose_estimation_2d2d ${OpenCV_LIBS})
target_link_libraries(pose_estimation_2d2d fmt::fmt)
# # add_executable( triangulation triangulation.cpp extra.cpp) # use this if in opencv2
add_executable(triangulation triangulation.cpp)
target_link_libraries(triangulation ${OpenCV_LIBS})
target_link_libraries(triangulation fmt::fmt)
add_executable(pose_estimation_3d2d pose_estimation_3d2d.cpp)
target_link_libraries(pose_estimation_3d2d
g2o_core g2o_stuff
${OpenCV_LIBS})
target_link_libraries(pose_estimation_3d2d fmt::fmt)
add_executable(pose_estimation_3d3d pose_estimation_3d3d.cpp)
target_link_libraries(pose_estimation_3d3d
g2o_core g2o_stuff
${OpenCV_LIBS})
target_link_libraries(pose_estimation_3d3d fmt::fmt)
成功!
总结也是省流
在ch7课程里首先将CmakeLists.txt文件里的更改为以下
cmake_minimum_required(VERSION 2.8)
project(vo1)
set(CMAKE_BUILD_TYPE "Release")
add_definitions("-DENABLE_SSE")
set(CMAKE_CXX_FLAGS "-std=c++11 -O2 ${SSE_FLAGS} -msse4")
set(CMAKE_CXX_STANDARD 14)
list(APPEND CMAKE_MODULE_PATH ${PROJECT_SOURCE_DIR}/cmake)
find_package(OpenCV 4 REQUIRED)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
include_directories(${FMT_INCLUDE_DIRS})
include_directories("/usr/local/include/fmt")
find_package(FMT REQUIRED)
include_directories(
${OpenCV_INCLUDE_DIRS}
${G2O_INCLUDE_DIRS}
${Sophus_INCLUDE_DIRS}
"/usr/include/eigen3/"
)
add_executable(orb_cv orb_cv.cpp)
target_link_libraries(orb_cv ${OpenCV_LIBS})
add_executable(orb_self orb_self.cpp)
target_link_libraries(orb_self ${OpenCV_LIBS})
# add_executable( pose_estimation_2d2d pose_estimation_2d2d.cpp extra.cpp ) # use this if in OpenCV2
add_executable(pose_estimation_2d2d pose_estimation_2d2d.cpp)
target_link_libraries(pose_estimation_2d2d ${OpenCV_LIBS})
target_link_libraries(pose_estimation_2d2d fmt::fmt)
# # add_executable( triangulation triangulation.cpp extra.cpp) # use this if in opencv2
add_executable(triangulation triangulation.cpp)
target_link_libraries(triangulation ${OpenCV_LIBS})
target_link_libraries(triangulation fmt::fmt)
add_executable(pose_estimation_3d2d pose_estimation_3d2d.cpp)
target_link_libraries(pose_estimation_3d2d
g2o_core g2o_stuff
${OpenCV_LIBS})
target_link_libraries(pose_estimation_3d2d fmt::fmt)
add_executable(pose_estimation_3d3d pose_estimation_3d3d.cpp)
target_link_libraries(pose_estimation_3d3d
g2o_core g2o_stuff
${OpenCV_LIBS})
target_link_libraries(pose_estimation_3d3d fmt::fmt)
ch7内所有cpp文件CV_LOAD_IMAGE_COLOR更改为cv::IMREAD_COLOR
pose_estimation_2d2d.cpp里将CV_FM_8POINT更改为cv::FM_8POINT
pose_estimation_3d2d.cpp和pose_estimation_3d3d.cpp里将CV_LOAD_IMAGE_UNCHANGED替换为cv::IMREAD_UNCHANGED
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 终于写完轮子一部分:tcp代理 了,记录一下
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理