[英]c++ Opencv sift Positiondetection
我使用ubuntu,並且代碼使用opencv編寫在C ++中。 我進行了一些測試以檢測圖片的某些部分。 它運作得很好,但是現在我想在我的全局中找到位置。 這是代碼:
#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 ...
謝謝您的幫助。 最好的祝福,
如果我理解得很好,您將找到關鍵點位置並對其進行分類。 要知道關鍵點的位置,您需要做一個穿過關鍵點向量的氣泡並將其保存在矩陣中。
然后,當您可以將這些關鍵點的位置與圖像區域(無論您想要什么)進行比較,並根據它們在圖像中的區域進行分類時。
Mat pointsInFirstAreaRight, pointInFirstAreaLeft;
for (int i = 0; i < 2; i++){
for(vector<DMatch>::const_iterator it = keypoints[i].begin(); it!= keypoints[i].end(); ++it){
// Get the position of keypoints
float x = [it->queryIdx].pt.x;
float y = [it->queryIdx].pt.y;
//If the point detected are in the 20 first pixels
if ( (x < 20) && (y > 20) )
{
//Classify this point in this area
if ( i == 0)
pointsInFirstAreaRight.push_back(Point2f(x, y));
else if (i == 1)
pointsInFistAreaLeft.push_back(Point2f(x, y));
}
}
}
關於篩選功能,它可以同時進行檢測和提取。 我在我的代碼中這樣使用它:
> SIFT sift;
>
> /* get keypoints on the images */
> sift(imagenI, Mat(), keypoints[0], descriptors[0]);
> sift(imagenD, Mat(), keypoints[1], descriptors[1]);
然后,我檢測關鍵點,並提取點的描述符。
然后,我只需要進行匹配即可。
希望對您有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.