【发布时间】:2015-05-27 19:40:57
【问题描述】:
我使用 ubuntu,代码是 C++ 和 opencv。 我进行了一些测试以检测图片的某些部分。它工作得很好,但现在我想在我的大图中找到位置。代码如下:
#include...
using namespace cv;
int main(int argc, char** argv) {
Mat img = imread("/home/ubuntu/workspace2/sift/src/inputklein.jpg",CV_LOAD_IMAGE_GRAYSCALE);
while(1){
Mat img2 =imread("/home/ubuntu/workspace2/sift/src/input.jpeg",CV_LOAD_IMAGE_GRAYSCALE); //frame
//initialize SIFT
// Create smart pointer for SIFT feature detector.
SIFT sift;
vector<KeyPoint> key_points;
vector<KeyPoint> key_points2;
//-- Step 1: Detect the keypoints using SURF Detector
int minHessian = 100;
SurfFeatureDetector detector( minHessian );
detector.detect( img, key_points );
detector.detect( img2, key_points2 );
//-- Step 2: Calculate descriptors (feature vectors)
SurfDescriptorExtractor extractor;
Mat descriptors1;
Mat descriptors2;
extractor.compute( img, key_points, descriptors1 );
extractor.compute( img2, key_points2, descriptors2 );
//-- Step 3: Matching descriptor vectors using FLANN matcher
FlannBasedMatcher matcher;
std::vector< DMatch > matches;
matcher.match( descriptors1, descriptors2, matches );
double max_dist = 20; double min_dist = 10;
//-- Quick calculation of max and min distances between keypoints
for( int i = 0; i < descriptors1.rows; i++ )
{ double dist = matches[i].distance;
if( dist < min_dist ) min_dist = dist;
if( dist > max_dist ) max_dist = dist;
}
//std::cout<<"Max dist :"<< max_dist ;
//std::cout<<"Min dist :"<< min_dist ;
//-- Draw only "good" matches (i.e. whose distance is less than 2*min_dist,
//-- or a small arbitary value ( 0.02 ) in the event that min_dist is very
//-- small)
//-- PS.- radiusMatch can also be used here.
std::vector< DMatch > good_matches;
for( int i = 0; i < descriptors1.rows; i++ )
{ if( matches[i].distance <= max(2*min_dist, 0.02) )
{ good_matches.push_back( matches[i]); }
}
//-- Draw only "good" matches
Mat img_matches;
drawMatches( img, key_points, img2, key_points2,
good_matches, img_matches, Scalar::all(-1), Scalar::all(-1),
vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
//std::cout<<key_points[1].pt.x<<"\n";
//std::cout<<key_points2[1].pt.y<<"\n";
//-- 3. Apply the classifier to the frame
cv::imshow( "test", img_matches ); //img_matches
waitKey(30);
}
return 0;
}
好的,但是我怎样才能得到位置或关键点最多的位置。有人可以给我一个提示,我必须如何理解它? 我看到我可以使用这样的东西: "key_points[1].pt.x" 或 y,但我不是必须检查每个 x,y 位置吗? 接下来是: good_matches[1].queryIdx 但这是同一个问题。我怎样才能找到它在哪里?
对我来说一个大问题是,为什么行上只有一个循环? 它不应该超过 row 和 cols 吗?在我的目的地,它应该像在数组(x,y)中一样工作,我检查每个位置是否相同......(有没有简单数据类型的问题......)
在哪里可以找到/或如何找到 drawMatches 代码的位置(例如)。 正常方式我会尝试“打开声明”(使用 Eclipse、C++),但我只看到标题而不是真正的函数。 我需要代码并希望我可以在没有 opencv 的情况下更改所有内容,或者我可以执行循环...所以我必须了解如何阅读和使用向量 DMatch...
感谢您的帮助。 最好的问候,
【问题讨论】: