2013-03-18 60 views
1

我試圖通過調用SURF功能對每幀找到視頻中的物體...... 這是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 
} 

有什麼解釋嗎? 在此先感謝

+0

我可以知道你在哪裏定義了square和in_box – 2013-07-12 13:31:26

回答

5

類似的東西可能是當你有很少的good_matches(< = 4)。你需要跳過這些幀。

+0

,即使我不明白這個條件的好處是什麼,但是它解決了這個問題if(good_matches.size()> = 4) – seereen 2013-04-13 03:11:09

+0

這是一個需求的方法「findHomography」,在我看來,應該提供更多的信息。 – 2013-05-09 12:48:52

+0

非常感謝,@McBodik,它像一個魅力:D – 2013-05-09 12:49:15