簡體   English   中英

OpenCV Sift / Surf / Orb:drawMatch功能運行不正常

[英]OpenCV Sift/Surf/Orb : drawMatch function is not working well

我使用Sift / Surf和ORB,但有時我的drawMatch函數有問題。

這里的錯誤:

OpenCV錯誤:斷言失敗(i2> = 0 && i2 <static_cast(keypoints2.size()))在drawMatches中,文件/home/opencv-2.4.6.1/modules/features2d/src/draw.cpp,第208行終止后調用拋出'cv :: Exception'的實例what():/ home / opencv-2.4.6.1 / modules / features2d / src / draw.cpp:208:error:( - 1515)i2> = 0 && i2 <static_cast( keyPoint2.size())在函數drawMatche中

代碼 :

drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,matches);

我嘗試將img 1,keypoints_img1與img2和keypoints_img2反轉為:

drawMatchPoints(img2,keypoints_img2,img1,keypoints_img1,matches);

對應我正在做單應性的功能:

void drawMatchPoints(cv::Mat image1,std::vector<KeyPoint> keypoints_img1,
                                      cv::Mat image2,std::vector<KeyPoint> keypoints_img2,std::vector<cv::DMatch> matches){

    cv::Mat img_matches;
    drawMatches( image1, keypoints_img1, image2, keypoints_img2,
                         matches, img_matches, Scalar::all(-1), Scalar::all(-1),
                         vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
            std::cout << "Number of good matching " << (int)matches.size() << "\n" << endl;



            //-- Localize the object
            std::vector<Point2f> obj;
            std::vector<Point2f> scene;

            for( int i = 0; i < matches.size(); i++ )
            {
              //-- Get the keypoints from the good matches
              obj.push_back( keypoints_img1[ matches[i].queryIdx ].pt );
              scene.push_back( keypoints_img2[matches[i].trainIdx ].pt );
            }

            Mat H = findHomography( obj, scene, CV_RANSAC );
            std::cout << "Size of homography " << *H.size << std::endl ;

            //-- Get the corners from the image_1 ( the object to be "detected" )
            std::vector<Point2f> obj_corners(4);
            obj_corners[0] = cvPoint(0,0); obj_corners[1] = cvPoint( image1.cols, 0 );
            obj_corners[2] = cvPoint( image1.cols, image1.rows ); obj_corners[3] = cvPoint( 0, image1.rows );
            std::vector<Point2f> scene_corners(4);


            perspectiveTransform( obj_corners, scene_corners, H);


            //-- Draw lines between the corners (the mapped object in the scene - image_2 )
            line( img_matches, scene_corners[0] + Point2f( image1.cols, 0), scene_corners[1] + Point2f( image1.cols, 0), Scalar(0, 255, 0), 4 );
            line( img_matches, scene_corners[1] + Point2f( image1.cols, 0), scene_corners[2] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );
            line( img_matches, scene_corners[2] + Point2f( image1.cols, 0), scene_corners[3] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );
            line( img_matches, scene_corners[3] + Point2f( image1.cols, 0), scene_corners[0] + Point2f( image1.cols, 0), Scalar( 0, 255, 0), 4 );

            //-- Show detected matches
            cv::imshow( "Good Matches & Object detection", img_matches );
            cv::waitKey(5000);

}

但我仍然有錯誤!

我注意到當我的keypoints_img1的大小低於我的keypoints_img2的大小時發生了錯誤:

大小keyPoint1:244 - 大小keyPoint2:400

因此,如果我反轉我的兩張照片的加載,這是有效的,但我現在不能提前,如果我的第一張照片將有更多的關鍵點,我的第二張照片......

我的代碼(最重要的一步)是為了創建功能:

init_Sift(400,5,0.04,25,1.6);
void init_Sift(int nf,int nOctaveL,double contrastThresh, double edgeThresh,double sigma){
this->nfeatureSift=nf;
this->nOctaveLayerSift=nOctaveL;
this->contrastThresholdSift=contrastThresh;
this->edgeThresholdSift=edgeThresh;
this->sigmaSift=sigma;}



 cv::FeatureDetector* detector=new SiftFeatureDetector(nfeatureSift,nOctaveLayerSift,contrastThresholdSift,edgeThresholdSift,sigmaSift);
cv::DescriptorExtractor* extractor=new SiftDescriptorExtractor

extractor->compute( image, keypoints, descriptors );

匹配部分:

    std::cout << "Type of matcher : " << type_of_matcher << std::endl;
if (type_of_matcher=="FLANN" || type_of_matcher=="BF"){
    std::vector<KeyPoint> keypoints_img1 = keyfeatures.compute_Keypoints(img1);
    std::vector<KeyPoint> keypoints_img2 = keyfeatures.compute_Keypoints(img2);

    cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1);
    cv::Mat descriptor_img2 = keyfeatures.compute_Descriptors(img2);

    std::cout << "Size keyPoint1 " << keypoints_img1.size() << "\n" << std::endl;
    std::cout << "Size keyPoint2 " << keypoints_img2.size() << "\n" << std::endl;

    //Flann with sift or surf
    if (type_of_matcher=="FLANN"){
        Debug::info("USING Matcher FLANN");
        fLmatcher.match(descriptor_img1,descriptor_img2,matches);

        double max_dist = 0; double min_dist = 100;

        //-- Quick calculation of max and min distances between keypoints
        for( int i = 0; i < descriptor_img1.rows; i++ ){
            double dist = matches[i].distance;
            if( dist < min_dist ) min_dist = dist;
            if( dist > max_dist ) max_dist = dist;
         }

        std::vector< DMatch > good_matches;

          for( int i = 0; i < descriptor_img1.rows; i++ )
          { if( matches[i].distance <= max(2*min_dist, 0.02) )
            { good_matches.push_back( matches[i]); }
          }

          std::cout << "Size of good match : " <<  (int)good_matches.size() << std::endl;
          //-- Draw only "good" matches
          if (!good_matches.empty()){
              drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,good_matches);

          }
          else {
              Debug::error("Flann Matcher : Pas de match");
              cv::Mat img_matches;
              drawMatches( img1, keypoints_img1, img2, keypoints_img2,
                                matches, img_matches, Scalar::all(-1), Scalar::all(-1),
                                vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
              cv::imshow( "No match", img_matches );
              cv::waitKey(5000);
          }

    }
    //BruteForce with sift or surf
    else if (type_of_matcher=="BF"){
        Debug::info("USING Matcher Brute Force");

        bFmatcher.match(descriptor_img1,descriptor_img2,matches);
        if (!matches.empty()){
            std::nth_element(matches.begin(),//Initial position
                             matches.begin()+24, //Position  of the sorted element
                             matches.end());//End position
            matches.erase(matches.begin()+25,matches.end());

            drawMatchPoints(img1,keypoints_img1,img2,keypoints_img2,matches);
            //drawMatchPoints(img2,keypoints_img2,img1,keypoints_img1,matches);
        }
        else {
            Debug::error("Brute Force matcher  : Pas de match");
            cv::Mat img_matches;
            drawMatches( img1, keypoints_img1, img2, keypoints_img2,
                              matches, img_matches, Scalar::all(-1), Scalar::all(-1),
                              vector<char>(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
            cv::imshow( "No match", img_matches );
            cv::waitKey(5000);

        }

}

你有什么建議或建議嗎?

編輯:我解決了我的問題。 我有一個c ++問題,因為我有兩個班級。 一個關於匹配,另一個關於查找keyFeature。 我已經寫了我的.h std :: vector和描述符相同。

class keyFeatures{

public:
...   
std::vector<keyPoint> keypoints;
...

我刪除了這個屬性,我做了一個函數,它接受參數std :: vector keypoints

cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1,keypoints_img1);

代替

cv::Mat descriptor_img1 = keyfeatures.compute_Descriptors(img1);

我認為當我進行匹配時存在沖突...但我不知道為什么我不應該在我的.h上寫它並在我的函數上做一個局部參數。

謝謝 !

對於像我這樣尋找此但卻無法找到解決方案的人。

斷言失敗(i2> = 0 && i2 <static_cast(keypoints2.size()))

這意味着由於i2小於0或i2小於keypoints2大小,斷言失敗。 但是i2是什么?

來自rbaleksandar在評論中提供的鏈接

int i2 = matches1to2 [m] .trainIdx;

trainIdx這里是keypoints2中的索引。 檢查i2 <static_cast(keypoints2.size())確保索引小於keypoints2.size()。

對我來說,它發生了,因為我在調用drawMatches之前丟棄了一些關鍵點,但在計算了描述符之后,即調用了DescriptorExtractor#compute。 這意味着當我更改這些關鍵點時,drawMatches通過描述符引用舊關鍵點。 最終結果是一些關鍵點具有大的idx但關鍵點大小很小因此錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM