'fatal error: opencv2/xfeatures2d.hpp: No such file or directory

I am trying to build this code: https://docs.opencv.org/3.2.0/d5/d6f/tutorial_feature_flann_matcher.html

I am using Ubuntu 16.04 with CLion 2017.3 and have OpenCV 3.4 installed.

xfeatures2d.hpp can't be found on the system.

I have looked at many different problems on the internet, but couldn't find a solution.

Any help?



Solution 1:[1]

You have to include cmake comiplation flag to opencv OPENCV_EXTRA_MODULES_PATH and set it to the opencv_contrib/modules.

cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib/modules

Solution 2:[2]

Here's what I had to do to compile OpenCV with xfeatures2d:

  • Download opencv_contrib (I took a release from the releases page). This step is what gives us xfeatures2d.
  • Unpack the tarball somewhere, e.g., ~/src/opencv_contrib-4.5.5
  • When configuring OpenCV (the main OpenCV source like opencv-4.5.2) with CMake, add the following option to cmake command line (adjusting the value to be your actual path to the modules subdirectory of the opencv_contrib tarball):
-DOPENCV_EXTRA_MODULES_PATH=$HOME/src/opencv_contrib-4.5.5/modules/
  • Now just build as you normally would and install.

The above actions gave me the expected file (among others): /usr/include/opencv4/opencv2/xfeatures2d.hpp.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 craken
Solution 2 Ruslan