繁体   English   中英

C ++ OpenCV筛选位置检测

[英]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,但这是相同的问题。 我如何找到它在哪里?

  1. 对我来说,一个大问题是,为什么只有行循环? 应该不超过行和列吗? 在我的目的地,它应该像在数组(x,y)中一样工作,并且我检查每个位置是否相同...(有没有简单数据类型的问题...)

  2. 我在哪里可以找到/或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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM