Colin747
Colin747

Reputation: 5013

OpenCV segmentation fault after comparing descriptor.type and descriptor.cols

In my program whenever I cover the camera with something that causes no ORB features to be detected the program crashes with the error:

OpenCV Error: Assertion failed (type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U)) in batchDistance, file /bin/opencv-2.4.7/modules/core/src/stat.cpp, line 2473
terminate called after throwing an instance of 'cv::Exception'
  what():  /bin/opencv-2.4.7/modules/core/src/stat.cpp:2473: error: (-215) type == src2.type() && src1.cols == src2.cols && (type == CV_32F || type == CV_8U) in function batchDistance

Aborted

In order to try and fix this error I followed the advice in this post here which involved adding the following if around the line matcher->match(descriptorsObject, descriptors, matches);

 if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
      matcher->match(descriptorsObject, descriptors, matches);
  }

However when the camera cannot detect any feature points the program still crashes but I just get the error:

Segmentation fault

I am not sure why this is now happening, do I need to have an else condition to get the next frame if the condition of the if is not met?

EDIT: When the code is run in debug mode I get the following error:

Program received signal SIGSEGV, Segmentation fault.
0x0000000000402d9e in main (argc=2, argv=0x7fffffffe428) at ORB.cpp:57
57        double dist = matches[i].distance;

When enabling all warnings when compiling I get the following:

ORB.cpp:87:42: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Extra code:

    if(descriptorsObject.type() == descriptors.type() && descriptorsObject.cols == descriptors.cols) {
      matcher->match(descriptorsObject, descriptors, matches);
    }

    double max_dist = 0;
    double min_dist = 100;
    for( int i = 0; i < descriptorsObject.rows; i++ ) { 
      double dist = matches[i].distance;  //LINE 57
      if( dist < min_dist ) 
        min_dist = dist;
      if( dist > max_dist ) 
       max_dist = dist;
    }
    std::vector<DMatch> goodMatches;

    for( int i = 0; i < descriptorsObject.rows; i++ ) { 
      if( matches[i].distance < 3*min_dist ) { 
       goodMatches.push_back( matches[i]); 
      }
    }
if(goodMatches.size() >= 4) {
    Mat outputImage;
    Scalar keypointColor = Scalar(255, 0, 0);
    drawMatches( img_object, keypointsObject, gray, keypoints, goodMatches, imgMatches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
    std::vector<Point2f> obj;
    std::vector<Point2f> scene;

    for( int i = 0; i < goodMatches.size(); i++ ) {  //LINE 87
      //-- Get the keypoints from the good matches
      obj.push_back( keypointsObject[ goodMatches[i].queryIdx ].pt );
      scene.push_back( keypoints[ goodMatches[i].trainIdx ].pt );
    }
    std::vector<uchar> mask;
    Mat H = findHomography( obj, scene, CV_RANSAC, 3, mask );
    std::vector<Point2f> obj_corners(4);
    obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( img_object.cols, 0 );
    obj_corners[2] = cvPoint( img_object.cols, img_object.rows ); obj_corners[3] = cvPoint( 0, img_object.rows );
    std::vector<Point2f> scene_corners(4);
    perspectiveTransform( obj_corners, scene_corners, H);
    line( imgMatches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
    line( imgMatches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
    line( imgMatches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
    line( imgMatches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
    if (mask.size() < 100) {
      std::cout << fileNamePostCut << std::endl;
    }
}
    namedWindow("Output");
    imshow("Output", imgMatches);

Upvotes: 1

Views: 1187

Answers (2)

Colin747
Colin747

Reputation: 5013

I extended the scope of the if statement so instead of closing after the line matcher->match(descriptorsObject, descriptors, matches); it now closes at the end above the line namedWindow("Output");

Upvotes: 0

Anoop K. Prabhu
Anoop K. Prabhu

Reputation: 5635

Did you try debugging?

As modifying the code with the if statement led to a different error, the most possible reason could be that you might be trying to pick some value from matches or descriptors after the if condition. And as there are no matches or features present in the image, this can lead to segmentation fault.

Try debugging the code and give us more details including code snippet so that the possibilities could be narrowed down further.

Upvotes: 1

Related Questions