Skip to content

Support OpenCV 5 #24

Description

@csparker247

Homebrew's unversioned opencv formula is now 5.0.0, and other distros will follow. RT currently requires major version 4 and cannot configure against 5.

It is not only the version pin

The obvious change is the two places that pin the major version:

File Line
cmake/FindDependencies.cmake:6 find_package(OpenCV 4 QUIET REQUIRED)
cmake/Config.cmake.in:17 find_dependency(OpenCV 4)

The second one matters as much as the first — it is the installed package config, so it constrains downstream consumers of rt::core, not just our own build.

But relaxing the version alone will not link. OpenCV 5 reorganised the modules RT uses, comparing modules/ on the upstream 4.x and 5.x branches:

4.x:  calib3d   core dnn features2d flann gapi         highgui imgcodecs imgproc ... stitching
5.x:  calib     core dnn features   flann geometry ptcloud stereo highgui imgcodecs imgproc ... stitching

calib3d is split across calib / geometry / stereo, and features2d is renamed features. So these link targets do not exist in OpenCV 5:

  • core/CMakeLists.txt:66 — opencv_calib3d
  • core/CMakeLists.txt:67 — opencv_features2d

modules/calib/CMakeLists.txt on 5.x declares ocv_define_module(calib ...), i.e. the target is opencv_calib, and a code search for opencv_calib3d across 5.x *.cmake returns nothing — there are no legacy target aliases.

The headers are fine

opencv2/calib3d.hpp and opencv2/features2d.hpp still ship on 5.x as compatibility shims. The whole of modules/calib/include/opencv2/calib3d.hpp is:

#ifndef OPENCV_CALIB3D_HPP
#define OPENCV_CALIB3D_HPP
#include "opencv2/geometry.hpp"
#include "opencv2/stereo.hpp"
#include "opencv2/calib.hpp"
#endif

So core/src/LandmarkDetector.cpp:7-8 can stay as-is. Only the CMake target names need to change.

Surface area is small

RT touches very little of the affected modules — one file:

  • core/src/LandmarkDetector.cpp:57 — cv::SIFT::create(), cv::DescriptorMatcher::create(FLANNBASED), cv::KeyPoint (features2d → features)
  • core/src/LandmarkDetector.cpp:185 — cv::estimateAffinePartial2D (on 5.x this is declared in modules/geometry/include/opencv2/geometry/3d.hpp, so the target is likely opencv_geometry rather than opencv_calib — worth confirming)

Everything else is core, imgproc, imgcodecs, which are unchanged.

Also worth cleaning up while here

core/CMakeLists.txt:70 links opencv_stitching, but nothing in the tree includes opencv2/stitching* or references cv::Stitcher / cv::detail:: stitching symbols. It appears to be a stale dependency and can probably be dropped. (The module does still exist on 5.x, so it is not a blocker either way.)

Suggested shape

Supporting 4 and 5 concurrently is probably worth it rather than a hard cutover, since distros will straddle the versions for a while:

find_package(OpenCV 4 QUIET)
if(NOT OpenCV_FOUND)
  find_package(OpenCV 5 QUIET REQUIRED)
endif()

if(OpenCV_VERSION_MAJOR GREATER_EQUAL 5)
  set(RT_OPENCV_GEOM opencv_calib opencv_geometry opencv_features)
else()
  set(RT_OPENCV_GEOM opencv_calib3d opencv_features2d)
endif()

Related

Once this lands, the macOS CI workaround added in #23 can be reverted: the Brewfile can go back to the unversioned opencv, and the -DOpenCV_DIR=$(brew --prefix opencv@4)/lib/cmake/opencv4 line in .github/workflows/ci.yml (needed because versioned Homebrew formulae are keg-only) can be dropped.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions