【发布时间】:2021-05-27 17:44:23
【问题描述】:
我想从图像中找到一些东西。
就像人脸检测一样,但没有检测到人脸,我想检测其他东西。
所以我使用SURF算法找到关键点,并使用FLANN算法匹配关键点。
但我怎么知道图像是否匹配?
我认为如果source images Key Points distribute and template images Key Points 分布需要非常相似。那么两个iamge匹配。但是怎么办?
int main( int argc, char** argv )
{
std::string templateStr = "D:\\template2.jpg";
std::string srcString = "D:\\IMG_0284.jpg";
Mat img_1 = imread(templateStr, CV_LOAD_IMAGE_GRAYSCALE );
Mat img_2 = imread(srcString, CV_LOAD_IMAGE_GRAYSCALE );
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 500;
SurfFeatureDetector detector( minHessian );
std::vector<KeyPoint> keypoints_1, keypoints_2;
detector.detect( img_1, keypoints_1 );
detector.detect( img_2, keypoints_2 );
//show keypoint,only test
Mat img_11 = imread(templateStr, CV_LOAD_IMAGE_GRAYSCALE );
Mat img_21 = imread(srcString, CV_LOAD_IMAGE_GRAYSCALE );
drawKeypoints (img_11, keypoints_1, img_11, cv::Scalar::all(0), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
drawKeypoints (img_21, keypoints_2, img_21, cv::Scalar::all(0), cv::DrawMatchesFlags::DRAW_RICH_KEYPOINTS);
cv::namedWindow ("img_11");
cv::imshow ("img_11",img_11);
cv::namedWindow ("img_21");
cv::imshow ("img_21",img_21);
cv::waitKey (0);
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors_1, descriptors_2;
extractor.compute( img_1, keypoints_1, descriptors_1 );
extractor.compute( img_2, keypoints_2, descriptors_2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector<DMatch> matches;
tt = (double)cvGetTickCount();
matcher.match( descriptors_1, descriptors_2, matches );
double max_dist = 0; double min_dist = 100;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors_1.rows; i++ )
{
double dist = matches[i].distance;
if( dist < min_dist )
min_dist = dist;
if( dist > max_dist )
max_dist = dist;
}
printf("-- Max dist : %f \n", max_dist );
printf("-- Min dist : %f \n", min_dist );
//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist )
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors_1.rows; i++ )
{
if( matches[i].distance < 3*min_dist )
{
good_matches.push_back( matches[i]);
}
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches( img_1, keypoints_1, img_2, keypoints_2, good_matches, img_matches, \
Scalar::all(-1), Scalar::all(-1),vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//now I have two group keypoint,keypoints_1 and keypoints_2,and they is match.
//keypoints_1 is tmeplate image`s keypoints,
//keypoints_2 is source image`s keypoints,
//so I how to compare distribution of keypoints_1 and keypoints_2?
//if the two group keypoint`s distribute is very similarity,I will think the two image is match
return 0;
}
我使用 OpenCV2.4.9,VS 2010。
【问题讨论】: