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

时间:2015-01-28 09:54:26

标签: c++ opencv computer-vision surf object-detection

我有一个OpenCV程序,它使用SURF来检测视频流中是否检测到模板对象。我希望在检测到对象时打印出对象名称,但是当发现“良好”特征匹配时,它似乎在打印时,绝大部分时间都是误报。

我的计划如下:

    //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 个答案:

答案 0 :(得分:1)

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

  1. 首先,使用SIFT ratio test解除不清楚的匹配
  2. 您正在计算比赛中的单应性。将它用作正确匹配的模型:cv :: findHomography有一个名为掩码optional output。用它来确定实际上对单应性有多少匹配(这些被称为内点)。越多越好 - 例如,如果您的内容超过10个,则仅打印对象的名称。