1

我试图通过为每一帧调用 SURF 函数来在视频中找到一个对象......这是 SURF 函数 {

void Identify_SURF_Frame (Mat img_object , Mat img_scene , CvRect in_box) 
{ 
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 1;
SurfFeatureDetector detector( minHessian , 15 , 3 );
std::vector<KeyPoint> keypoints_object, keypoints_scene;
detector.detect( img_object, keypoints_object );
detector.detect( img_scene, keypoints_scene );

//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_object, descriptors_scene;
extractor.compute( img_object, keypoints_object, descriptors_object );
extractor.compute( img_scene, keypoints_scene, descriptors_scene );

//-- Step 3: Matching descriptor vectors using FLANN matcher
//FlannBasedMatcher matcher;
BruteForceMatcher < L2 < float > > matcher;
//BFMatcher matcher( cv::NORM_L2SQR , false );
std::vector< DMatch > matches;
matcher.match( descriptors_object, descriptors_scene, matches );
double max_dist = 0; double min_dist = 100;

//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_object.rows; i++ )
{ 
    double dist = matches[i].distance;
    if( dist < min_dist ) min_dist = dist;
    if( dist > max_dist ) max_dist = dist;
}

//-- Draw only "good" matches (i.e. whose distance is less than 3*min_dist )
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_object.rows; i++ )
{ 
    if( matches[i].distance < 4 * min_dist )
    { 
        good_matches.push_back( matches[i]); 
    }
}

Mat img_matches;
drawMatches( img_object, keypoints_object, img_scene, keypoints_scene, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );

//-- Localize 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 );
    //-- Get the corners from the image_1 ( the object to be "detected" )
    std::vector<Point2f> obj_corners(2);
    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(2);
    perspectiveTransform( obj_corners, scene_corners, H);
    int x1 , x2 , y1 , y2 ;
    x1 = scene_corners[0].x + Point2f( img_object.cols, 0).x ; 
    y1 = scene_corners[0].y + Point2f( img_object.cols, 0).y ; 
    x2 = scene_corners[0].x + Point2f( img_object.cols, 0).x + in_box.width ; 
    y2 = scene_corners[0].y + Point2f( img_object.cols, 0).y + in_box.height ; 

    rectangle(img_matches , cvPoint(x1, y1) , cvPoint(x2, y2)  , Scalar( 255, 255, 255), 1 );
            // square is the global CvRect to use it in main 
    square.x = x1 - in_box.width ; 
    square.y = y1 ; 
    square.width = in_box.width ; 
    square.height = in_box.height ;  

//-- Show detected matches
imshow( "Good Matches & Object detection", img_matches );
}
}

使用此功能,当我发现问题时,我试图在对象周围绘制固定大小的正方形..有时我得到了这个错误,我不是什么意思..有时程序可以正常工作而没有这个错误..当这个错误发生时,程序从头开始

 {     
 OpenCV Error: Assertion failed (count >= 4) in cvFindHomography, file /Users/seereen2004/Desktop/OpenCV-2.4.3/modules/calib3d/src/fundam.cpp, line 235
 terminate called after throwing an instance of 'cv::Exception'
   what():  /Users/seereen2004/Desktop/OpenCV-2.4.3/modules/calib3d/src/fundam.cpp:235:    error: (-215) count >= 4 in function cvFindHomography

 Program received signal:  “SIGABRT”.
 sharedlibrary apply-load-rules all
 }

请问有什么解释吗?提前致谢

4

1 回答 1

5

当你的good_matches(<=4)太少时,可能会出现类似的情况。您需要跳过此帧。

于 2013-03-26T15:14:11.260 回答