【高翔视觉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




	
	
  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

沐月Ansel

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值