我试图通过为每帧调用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
}
请解释一下?
当您的good_matches (<=4)太少时可能会出现这种情况。