OpenCV - 仅在绘制单应性时打印对象名称

OpenCV - print object name only when homography is drawn

我有一个 OpenCV 程序,它使用 SURF 来检测是否在视频流中检测到模板对象。我希望在检测到对象时打印出对象名称,但目前似乎只要发现 "good" 特征匹配就会打印出来,这在大多数情况下都是误报。

我的程序如下:

    //Step 1: Detect keypoints using SURF detector
    //Step 2: Calculate descriptors (feature vectors)
    //Step 3: Matching descriptor vectors using FLANN matcher
    //Step 4: Localise the object
  std::vector<Point2f> obj;
  std::vector<Point2f> scene;

  for( int i = 0; i < good_matches.size(); i++ )
  {
    //-- Get the keypoints from the good matches
    obj.push_back( keypoints_object[ good_matches[i].queryIdx ].pt );
    scene.push_back( keypoints_scene[ good_matches[i].trainIdx ].pt );
  }

  Mat H = findHomography( obj, scene, CV_RANSAC );
     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);

      //-- Draw lines between the corners (the mapped object in the scene - image_2 )
      line( img_matches, scene_corners[0] + Point2f( img_object.cols, 0), scene_corners[1] + Point2f( img_object.cols, 0), Scalar(0, 255, 0), 4 );
      line( img_matches, scene_corners[1] + Point2f( img_object.cols, 0), scene_corners[2] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
      line( img_matches, scene_corners[2] + Point2f( img_object.cols, 0), scene_corners[3] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );
      line( img_matches, scene_corners[3] + Point2f( img_object.cols, 0), scene_corners[0] + Point2f( img_object.cols, 0), Scalar( 0, 255, 0), 4 );

      if() {
        std::cout << fileNamePostCut << std::endl;
      }
    ...

我不确定要声明哪个条件才能打印对象名称(fileNamePostCut)

您的目标是消除误报。您应该采用两种方法:

  1. 首先,使用 SIFT ratio test 消除不明确的匹配项
  2. 您正在根据匹配项计算单应性。将其用作正确匹配的模型:cv::findHomography 有一个名为 maskoptional output。使用它来确定有多少匹配实际对单应性有贡献(这些称为内点)。越多越好 - 例如,如果您有超过 10 个内点,则只打印对象的名称。