我使用ubuntu進行工作,Code是使用opencv的C++。 我測試了一下以檢測圖片的某些部分。它工作得很好,但現在我想在我的大局中找到位置。這裏是代碼:C++ Opencv sift Positiondetection
#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但這裏是同樣的問題。我如何找到它的位置?
對我來說一個很大的問題,爲什麼只有一個循環而已? 它不應該在行和列?在我的目的地它應該像在一個數組(x,y)中工作,我檢查每個位置,如果它是相同的...(有沒有簡單的數據類型的問題...)
哪裏可以找到/或drawMatches的代碼的位置(例如)。 正常的方式,我會嘗試「公開聲明」(使用Eclipse,C++),但我只看到頭,而不是真正的功能。 我需要的代碼,並希望我能夠改變這一切,而不OpenCV的,也許我可以做環......所以我必須明白,我怎麼能閱讀和使用矢量DMatch ...
THX您的幫助。 最好的問候,
我不知道我是否理解你的代碼。 Sry說我在2天后回答,但我認爲我不太清楚篩選作品是如何篩選的,我想先閱讀。但它沒有幫助。你是什麼意思與參數pointsInFirstAreaRight ...你的代碼中的問題是,我不能使用一些功能,如.push_back。我想在fpga中給出代碼,並且我的理解如何,它不喜歡函數庫。所以我需要一個非常簡單的代碼... – knowless
參數pointsInFirstArea是因爲我認爲你想要分類器的關鍵點取決於它們的位置,所以這個參數是關鍵點的矢量來保存(20×20)區域中的關鍵點。 –
關於我的push_back功能,它運行良好,我不知道爲什麼你可以使用它。在這種情況下,該功能將點2f中的關鍵點複製。看到我放在我的激光器中的sift代碼。 –