我尝试使用像笑脸template这样的简单模板将多个对象与旋转匹配 ,我想在测试图像中检测到它,如test image
我曾尝试使用Features2D和Homography进行检测,但是存在很多问题。
P1:看来这个关键点匹配方法对于SIMPLE模板来说并不准确(我在另一个模板中尝试了这个方法,这个方法要复杂得多,匹配结果更好)。这个问题有什么方法吗?
P2:这种方法绝对不适用于多物体测试图像。如何使用单个模板匹配多个对象?(前提是我不知道模板中对象的数量和位置)
以下是我的功能代码。
`//load image
Mat img1 = imread( "2.png", CV_LOAD_IMAGE_GRAYSCALE );
Mat img2 = imread( "1.png", CV_LOAD_IMAGE_GRAYSCALE );
//-- Step 1: Detect the keypoints using SURF Detector
SurfFeatureDetector detector( hessian );
vector<KeyPoint> keypoints1, keypoints2;
detector.detect( img1, keypoints1 );
detector.detect( img2, keypoints2 );
//-- Step 2: Extract the keypoints using SURF Extractor
Mat descriptors1,descriptors2;// extract keypoints
SurfDescriptorExtractor extractor; //Create Descriptor Extractor
extractor.compute( img1, keypoints1, descriptors1 );
extractor.compute( img2, keypoints2, descriptors2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
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
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_object.rows; i++ )
{ if( matches[i].distance < 3*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(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 );
`
我是计算机视觉的初学者,这是我第一次在这个论坛上提问。非常感谢你的帮助!
答案 0 :(得分:0)
如果您的问题只是检测那种图像,那么您可以做的一件简单事就是使用circle detector。你可以将较大圆圈(头部)的点和眼睛的点分组。如果您知道这3个圆的centroids的位置,您可以通过研究眼睛的位置来获得面部的位置和旋转。
在图像中,红点代表圆的质心,你可以通过找到主质心的位置来获得头部位置,alpha是右眼和主质心之间的角度。如果你能找到新的角度,你可以计算θ,它将指示面部的旋转,也许这甚至可以用于比例变化