Ask Your Question
0

Compilation errors with g++-8 for OpenCV 4.3.0 with opencv_contrib (xfeatures2d namespace conflict)

asked 2020-06-16 02:05:27 -0500

gus gravatar image

updated 2020-06-16 02:31:36 -0500

berak gravatar image

I initially created an issue here, but it seems it is the good place here:

System information (version) - OpenCV => 4.3.0 - Opencv_contrib => 4.3.0 - Operating System / Platform => Ubuntu 18.04 - Compiler => g++-8

Detailed description

Compilation errors occured when building OpenCV 4.3.0 with opencv_contrib 4.3.0. The problem is a conflict for type SIFT which is defined into two different namespaces. The code was built with the following options: -DWITH_OPENCL=ON -DWITH_V4L=ON -DOPENCV_EXTRA_MODULES_PATH=../../../src/OpenCV4/opencv_contrib/modules -DBUILD_EXAMPLES=ON

Note: I built it using catkin (from ROS) in a catkin isolated workspace as plain C-Make, so the same erros should occur without catkin build (which simply call make command for plain-cmake projects)

Errors occured when compiling generated files:

  • modules/python_bindings_generator/pyopencv_generated_types_content.h

    In file included from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/python/src2/cv2.cpp:1904: /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h: In function ‘PyObject* pyopencv_cv_xfeatures2d_xfeatures2d_SIFT_create_static(PyObject, PyObject, PyObject)’: /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h:74083:13: error: template argument 1 is invalid Ptr<sift> retval; ^ /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h:74093:56: error: invalid user-defined conversion from ‘cv::Ptr<cv::xfeatures2d::sift>’ to ‘int’ [-fpermissive] ERRWRAP2(retval = cv::xfeatures2d::SIFT::create(nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/python/src2/cv2.cpp:159:5: note: in definition of macro ‘ERRWRAP2’ expr; \ ^~~~ In file included from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/cvstd.hpp:81, from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/utils/configuration.private.hpp:8, from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/python/src2/cv2.cpp:35: /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/cvstd_wrapper.hpp:129:12: note: candidate is: ‘cv::Ptr<t>::operator T() const [with T = cv::xfeatures2d::SIFT]’ <near match=""> inline operator T* () const { return std::shared_ptr<t>::get(); } ^~~~~~~~ /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/cvstd_wrapper.hpp:129:12: note: no known conversion from ‘cv::xfeatures2d::SIFT’ to ‘int’ In file included from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/python/src2/cv2.cpp:1904: /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h: In function ‘PyObject pyopencv_cv_xfeatures2d_xfeatures2d_SIFT_create_static(PyObject, PyObject, PyObject)’: /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h:74083:13: error: template argument 1 is invalid Ptr<sift> retval; ^ /home/gus/ressac/catkin_isolated_ws/build_isolated/OpenCV4/install/modules/python_bindings_generator/pyopencv_generated_types_content.h:74093:56: error: invalid user-defined conversion from ‘cv::Ptr<cv::xfeatures2d::sift>’ to ‘int’ [-fpermissive] ERRWRAP2(retval = cv::xfeatures2d::SIFT::create(nfeatures, nOctaveLayers, contrastThreshold, edgeThreshold, sigma)); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/python/src2/cv2.cpp:159:5: note: in definition of macro ‘ERRWRAP2’ expr; \ ^~~~ In file included from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/cvstd.hpp:81, from /home/gus/ressac/catkin_isolated_ws/src/OpenCV4/modules/core/include/opencv2/core/utils/configuration.private.hpp ...

(more)
edit retag flag offensive close merge delete

Comments

SIFT was moved from contrib/xfeatures2d to the main opencv repo with 4.3.0

if you still have the xfeatures2d version, you must have conflicting main/contrib versions, NOT 4.3.0 for both, please check again. also please clean your build folder and start all over with cmake

also, iirc ROS comes with its own (usually outdated) opencv version which will get in the way, so please try to avoid all things ROS for now

berak gravatar imageberak ( 2020-06-16 02:29:27 -0500 )edit

also, "patching" machine-generated code like pyopencv_generated_funcs.h is quite useless, it may be overwritten at any moment

berak gravatar imageberak ( 2020-06-16 02:33:07 -0500 )edit
1

Ok thanks.

  • Patches were just a "readable" way to let you know in the post the change I did to solve the problem

  • I know ROS uses an outdated version of OpenCV, that's why I wanted to build the 4.3.0 version in order to use of new features/functions in some of my ROS nodes.

gus gravatar imagegus ( 2020-06-18 08:36:46 -0500 )edit

1 answer

Sort by » oldest newest most voted
1

answered 2020-06-18 08:37:22 -0500

gus gravatar image

Ok, thank you it helps me to understand what happened:

There was effectively a conflict between my system opencv version (3.2) and the sources (4.3.0) I wanted to build. I just set the CMAKE_IGNORE_PATH variable correctly to avoid this conflict, so rebuild OpenCV in my catkin_isolated_ws with:

catkin_make_isolated -DWITH_OPENCL=ON -DWITH_V4L=ON -DCMAKE_IGNORE_PATH=/usr/lib/x86_64-linux-gnu;/usr/include -DOPENCV_EXTRA_MODULES_PATH=../../../src/OpenCV/opencv_contrib/modules -DBUILD_EXAMPLES=ON --pkg OpenCV --install

Note: If someone is interested to use recent OpenCV version with ROS, you can find bellow step-by-step procedure to achieve it (this is the procedure I used to use OpenCV 4.3.0 with ros melodic which normally depends on OpenCV 3.2. Note that ROS packages installed via apt-get still depend on OpenCV 3.2. OpenCV 4.3.0 is used only for custom package build in the catkin_ws):

  1. Create a catkin_isolated_ws:
  2. check no ros env is sourced

    env | grep PATH
    
  3. create isolated workspace and clone sources:

    mkdir -p  ~/catkin_isolated_ws/src
    cd ~/catkin_isolated_ws/src
    git clone https://github.com/opencv/opencv.git --branch 4.3.0 OpenCV
    cd OpenCV
    git clone https://github.com/opencv/opencv_contrib.git --branch 4.3.0 opencv_contrib
    
  4. create a package.xml with the following content:

    <package>
        <name>OpenCV</name>
        <version>4.3.0</version>
        <description>Open CV library for catkin</description>
        <maintainer email="your-email">your-name</maintainer>
        <license>your-licence</license>
        <buildtool_depend>cmake</buildtool_depend>
        <run_depend>catkin</run_depend>
        <export>
          <build_type>cmake</build_type>
        </export>
    </package>
    
  5. Source ROS and init workspace:

    source /opt/ros/melodic/setup.bash
    catkin_init_workspace
    
  • build and add the eventual opencv system's version to the cmake ignore path (CMAKE_IGNORE_PATH) (to avoid conflict with xfeatures2d):

    cd ~/catkin_isolated_ws/
    catkin_make_isolated -DWITH_OPENCL=ON -DWITH_V4L=ON -DCMAKE_IGNORE_PATH=/usr/lib/x86_64-linux-gnu;/usr/include -DOPENCV_EXTRA_MODULES_PATH=../../../src/OpenCV/opencv_contrib/modules -DBUILD_EXAMPLES=ON --pkg OpenCV --install
    
    1. Create a catkin_ws layer for packages we want to depend on OpenCV 4.3.0:
    mkdir -p  ~/catkin_ws/src
    cd ~/catkin_ws/src
    source ~/catkin_isolated_ws/install_isolated/setup.bash
    catkin_init_workspace
    cd ~/catkin_ws
    catkin_make
    source ~/catkin_ws/devel/setup.bash
    
    1. Now we can build ROS packages depending on OpenCV, the first found version will be the one of catkin_isolated_ws which hides the /opt/ros/melodic one which also hides the OpenCV system's version (3.2 for me)

Note: You may have conflicts with ROS package cv_bridge, as it is linked against ros's opencv version (OpenCV 3.2), so you can:

  • build in the catkin_ws your own cv_bridge from sources which will then be build against OpenCV 4.3.0 (to hide native ros cv_bridge package linked to 3.2)

  • or convert ROS sensor_msgs::Image by your own to openCV format

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2020-06-16 02:05:27 -0500

Seen: 150 times

Last updated: Jun 18