Resolving OpenCV 4 Compatibility Issues in Visual SLAM Chapter 7

When migrating the source code from "Visual SLAM: Theory and Practice" to a development environment using OpenCV 4, several compilation errors arise due to deprecated constants and API changes. This guide addresses the necessary modifications to the build system and source code too ensure a successful build.

1. Updating the Build Configuration

The primary configuration file requires updates to target OpenCV 4, set the C++ standard appropriately, and explicitly link the fmt library, which is used for formatting in later sections of the code.

Modify the CMakeLists.txt to reflect the new dependencies and version requirements. The following configuration restructures the original build script for clarity and compatibility:

cmake_minimum_required(VERSION 3.0)
project(slam_chapter7)

# Set C++ standard and optimization flags
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_BUILD_TYPE Release)
set(CMAKE_CXX_FLAGS "-O2 -Wall")

# Locate required packages
find_package(OpenCV 4 REQUIRED)
find_package(G2O REQUIRED)
find_package(Sophus REQUIRED)
find_package(fmt REQUIRED)

# Include directories
include_directories(
    ${OpenCV_INCLUDE_DIRS}
    ${G2O_INCLUDE_DIRS}
    ${Sophus_INCLUDE_DIRS}
    "/usr/include/eigen3"
    ${FMT_INCLUDE_DIRS}
)

# Helper macro to simplify executable creation
macro(add_slam_executable target_name source_file)
    add_executable(${target_name} ${source_file})
    target_link_libraries(${target_name} ${OpenCV_LIBS})
    
    # Link fmt library if not the basic orb_cv
    if(NOT ${target_name} STREQUAL "orb_cv" AND NOT ${target_name} STREQUAL "orb_self")
        target_link_libraries(${target_name} fmt::fmt)
    endif()
    
    # Link G2O for pose estimation modules
    if(${target_name} MATCHES "pose_estimation")
        target_link_libraries(${target_name} g2o_core g2o_stuff)
    endif()
endmacro()

# Define build targets
add_slam_executable(orb_cv orb_cv.cpp)
add_slam_executable(orb_self orb_self.cpp)
add_slam_executable(pose_estimation_2d2d pose_estimation_2d2d.cpp)
add_slam_executable(triangulation triangulation.cpp)
add_slam_executable(pose_estimation_3d2d pose_estimation_3d2d.cpp)
add_slam_executable(pose_estimation_3d3d pose_estimation_3d3d.cpp)

2. Addressing Image Reading Deprecasions

In OpenCV 4, legacy flags for image loading have been moved into the cv::ImreadModes enum. The global constants prefixed with CV_ are no longer valid.

Error: 'CV_LOAD_IMAGE_COLOR' was not declared in this scope
Solution: Replace CV_LOAD_IMAGE_COLOR with cv::IMREAD_COLOR.

// Legacy OpenCV 3 code
// cv::Mat img_1 = cv::imread(argv[1], CV_LOAD_IMAGE_COLOR);

// Updated OpenCV 4 code
cv::Mat img_1 = cv::imread(argv[1], cv::IMREAD_COLOR);

Similarly, for depth map loading where the original data must be preserved, CV_LOAD_IMAGE_UNCHANGED must be replaced.

// Legacy code
// cv::Mat depth_map = cv::imread(argv[3], CV_LOAD_IMAGE_UNCHANGED);

// Updated code
cv::Mat depth_map = cv::imread(argv[3], cv::IMREAD_UNCHANGED);

3. Fixing Fundamental Matrix Calculation Flags

Constants used for specifying the algorithm method in findFundamentalMat have also been renamed to comply with stricter namespacing in OpenCV 4.

Error: 'CV_FM_8POINT' was not declared in this scope
Solution: Use cv::FM_8POINT instead of the legacy constant.

// Inside pose estimation function
// Legacy code
// fundamental_matrix = cv::findFundamentalMat(points1, points2, CV_FM_8POINT);

// Updated code
fundamental_matrix = cv::findFundamentalMat(points1, points2, cv::FM_8POINT);

4. Resolving Undefined References with fmt

After applying the code fixes, the build may still fail with multiple "undefined reference" errors related to fmt::v7 or similar symbols. This occurs because the source files utilize the fmt library for string formatting, but the linker is not instructed to link against it.

As demonstrated in the updated CMakeLists.txt in Section 1, ensure that every target utilizing formatting features explicitly links the fmt::fmt imported target:

target_link_libraries(your_target_name fmt::fmt)

Tags: OpenCV Visual SLAM CMake C++ Computer Vision

Posted on Sat, 29 Aug 2026 16:37:37 +0000 by Smudly