gpt4 book ai didi

opencv - 'OpenCV'使用单个简单模板将多个对象与旋转匹配

转载 作者:行者123 更新时间:2023-12-02 17:46:22 26 4
gpt4 key购买 nike

我尝试使用简单的模板(例如笑脸template)将旋转的多对象匹配
,我想在像test image的测试图像中检测到它

我尝试使用Feature2D和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 );
`

我是计算机视觉的初学者,这是我第一次在这个论坛上提问。非常感谢您的帮助!

最佳答案

如果您的问题是仅检测这类图像,那么您可以做的一件简单的事情就是使用circle detector。您可以将较大圆圈(头部)的点和眼睛的点归为一组。如果您知道这3个圆的centroids的位置,则可以通过研究眼睛的位置来了解面部的位置和旋转。

enter image description here

在图中,红色点代表圆的质心,可以通过找到主要质心所在的位置来获得头部位置,alpha是右眼与主要质心之间的 Angular 。如果您可以找到新 Angular ,则可以计算theta来指示面部旋转,即使缩放比例更改也可能有效

关于opencv - 'OpenCV'使用单个简单模板将多个对象与旋转匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34628393/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com