Ask Your Question

How to initialize a FeatureDetector with OpenCV 3?

asked 2015-09-28 13:08:19 -0500

SR gravatar image

updated 2015-09-28 16:29:42 -0500

With OpenCV 3 I get a SegFault when running the detect() function of either the ORB or AKAZE detector although the pointer is not null. I assume that something is not initialized. But my compiler is unable to find the previously necessary cv::initModule_features2d();. What do I need to do to initialize the feature detectors properly?

The code is roughly like the following:

   cv::FeatureDetector* detector = cv::ORB::create();
   CV_Assert( detector != NULL );
   vector<cv::KeyPoint> kpts1;
   detector->detect(img, kpts, cv::Mat());

I tried both 8uc3 and 8uc1 images, both ORB and AKAZE and it always segfaulted. Tested on OS X 10.10.

edit retag flag offensive close merge delete


hi, @SR, can you post your code along with the question ?

the features2d api has changed a lot in respect to 2.4 ->3.0, e.g. calling initModule_Something is no more nesssecary

berak gravatar imageberak ( 2015-09-28 13:32:51 -0500 )edit

2 answers

Sort by ยป oldest newest most voted

answered 2015-09-28 23:56:10 -0500

berak gravatar image

updated 2015-09-29 00:01:50 -0500

all feature2d classes return a Ptr<Something> from the create() method. if you don't use a smartpointer to catch it, you instance is already dead in line 1 (that is your segfault, it self-destroyed). so, it has to be:

cv::Ptr<cv::ORB> detector = cv::ORB::create();
CV_Assert( ! detector.empty() );
edit flag offensive delete link more



Yep, that was my stupid mistake. Thanks for the hint! But: Why does cv::Ptr<cv::ORB> allow implicit conversion to cv::ORB*? That behaviour is dangerous and misleading. The conversion should be made explicit via a get() function.

SR gravatar imageSR ( 2015-09-29 16:10:22 -0500 )edit

answered 2015-09-28 14:04:03 -0500

SR. I've only been playing with OpenCV in python and this particular part using the SIFT feature detector / description extractor in the xfeatures2d module.

When using the SIFT detector, you have to pass it the image as the first argument and a None variable (not exactly null, but a special python value) as the second argument to the detect() function. This second argument is an optional mask for the keypoint detection, but without it I think the function will throw an exception.

Since all of the python is just a wrapper for existing C++ code, I suspect that this is the same for your instance. You're probably not passing this second argument.

I also read somewhere that this only works on grayscale images. I'm not sure if that is true, as I am running it on color values without incident, but who knows, maybe I converted to grayscale before I started troubleshooting my latest issue and just forgot.

Good luck.

edit flag offensive delete link more
Login/Signup to Answer

Question Tools

1 follower


Asked: 2015-09-28 13:08:19 -0500

Seen: 1,365 times

Last updated: Sep 28 '15