Ask Your Question

Segmentation fault with ORB feature extractor

asked 2018-10-09 09:30:08 -0500

pooshak gravatar image

Hi, I've written a bit of code which takes an image, detects keypoints with using FAST (as shown below) and the resulting keypoints are passed over to an ORB feature detector. However, despite checking the image to be not empty and having a lot of keypoints, I get a weird segmentation fault whenever I try and run it.

class Code{
  //ROS node init removed for brevity
  Ptr<cv::FastFeatureDetector> detector = cv::FastFeatureDetector::create(detector_set_input);
  DescriptorExtractor* extractor = cv::ORB::create();
  std::vector<KeyPoint> keypoints_last;
  Mat descriptors_last;

  void callback(const sensor_msgs::ImageConstPtr& msg)
    cv_bridge::CvImagePtr cv_ptr;
    cv_ptr = cv_bridge::toCvCopy(msg, sensor_msgs::image_encodings::BGR8);   //try/excepts cut down for brevity
    Mat img = cv_ptr->image;
    detector->detect(image, keypoints_last);   //This line never throws bugs
    extractor->compute(gray_lastimage, keypoints_last, descriptors_last);    //Segmentation fault here

Details: callback is a ROS node callback function, and the image is converted from a sensor_msg to a cv::Mat using cv_bridge, but I don't think that's where the error is coming from or it probably wouldn't have detected any keypoints.

descriptors_last and keypoints_last are empty here- the issue comes from when the node is trying to initialize.

I'm using openCV3.3 on Ubuntu 16.04 with ROS Kinetic.

Many thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2018-10-09 09:39:44 -0500

berak gravatar image

updated 2018-10-09 09:46:36 -0500

problem is here:

DescriptorExtractor* extractor = cv::ORB::create();

you need:

Ptr<Feature2D> extractor = cv::ORB::create();

it returns a cv::Ptr, and if you don't use a smartpointer on the left side, it will simply self-destroy !

NEVER use raw pointers with opencv !

(DescriptorExtractor is just a typedef for the more general Feature2D)

edit flag offensive delete link more


Or use auto and there is never a problem.

Der Luftmensch gravatar imageDer Luftmensch ( 2018-10-09 12:07:40 -0500 )edit

^^ that only works for a single level of problems. stack "auto" 5 deep, and noone ever will know, what actually happened.

berak gravatar imageberak ( 2018-10-09 12:10:37 -0500 )edit

Thank you so much! You saved me haha However, can I ask why we should never use raw pointers with openCV?

pooshak gravatar imagepooshak ( 2018-10-09 15:15:20 -0500 )edit

because they are never used. it's most likeley, you missed a smartpointer, like in your question.

berak gravatar imageberak ( 2018-10-09 16:45:16 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2018-10-09 09:28:49 -0500

Seen: 729 times

Last updated: Oct 09 '18