比较 descriptor.type 和 descriptor.cols 后的 OpenCV 分割错误



在我的程序中,每当我用导致未检测到ORB功能的东西覆盖相机时,程序就会崩溃并显示错误:

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

为了尝试修复此错误,我遵循了本文中的建议,其中包括在行matcher->match(descriptorsObject, descriptors, matches);周围添加以下if

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

但是,当相机无法检测到任何功能点时,程序仍然崩溃,但我只是收到错误:

Segmentation fault

我不确定为什么现在会发生这种情况,如果不满足if的条件,我是否需要else条件才能获得下一帧?

编辑:当代码在调试模式下运行时,我收到以下错误:

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

在编译时启用所有警告时,我得到以下结果:

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

额外代码:

    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);

你试过调试吗?

由于使用 if 语句修改代码会导致不同的错误,因此最可能的原因可能是您可能尝试从 if 条件之后的matchesdescriptors中选取一些值。由于图像中没有匹配项或特征,这可能会导致分割错误。

尝试调试代码并向我们提供更多详细信息,包括代码片段,以便进一步缩小可能性。

我扩展了if语句的范围,因此它不是在行之后关闭,而是在行matcher->match(descriptorsObject, descriptors, matches);之后关闭,而是在行上方的末尾关闭namedWindow("Output");

最新更新