Linking ROS Noetic to opencv build from source

Viewed 226

I am writing a ROS node which uses OpenCV and SIFT (ROS Noetic, Ubuntu 20.04). As of OpenCV version 4, the SIFT algorithm is part of the opencv_contrib package. Within my ROS node, I want to use the ROS package cv_bridge, to convert between the OpenCV image format and ROS image sensor messages.

My understanding is that

  1. Code in the opencv_contrib package is only usable if such package is built together with OpenCV from source.
  2. cv_bridge depends on the Ubuntu package version of OpenCV, libopencv-dev.

I currently have both installed (the Ubuntu package and the version compiled from source) and I am trying to have my node depend on the source-compiled one, in order to use the non-free algorithms. My procedure in order to do this is (after compiling OpenCV with the additional modules and OPENCV_ENABLE_NONFREE=ON) adding to the CMakeLists.txt of my package the following lines:

find_package(OpenCV PATHS .../opencv-4.5.4/cmake)
include_directories(include ${catkin_INCLUDE_DIRS} .../opencv-4.5.4/include)
link_directories(.../opencv-4.5.4/lib)
add_executable(...)
target_link_libraries(nodeName ${catkin_LIBRARIES} .../opencv-4.5.4/lib)

in order to link my code to the compiled version of OpenCV. However, when I try to build it with catkin build I obtain:

In file included from [...]:
[...]/markerDetection.h:8:10: fatal error: opencv2/xfeatures2d.hpp: No such file or directory
    8 | #include <opencv2/xfeatures2d.hpp>

I suspect that ROS is trying to link to the version I installed through the package manager, which in fact does not have the opencv2/xfeatures2d.hpp library. Am I setting the wrong options in CMakeLists.txt? Is what I am trying to do even possible?

0 Answers
Related