2016-08-16 1 views
0

J'utilise le code suivant pour trouver des correspondances entre les images:Comment dessiner un objet détecté avec des fonctionnalités SIFT sur OpenCV 3.1?

#include <opencv2/highgui/highgui.hpp> 
#include <opencv2/xfeatures2d/nonfree.hpp> 
#include <opencv2/xfeatures2d.hpp> 

#include <vector> 

using namespace std; 
using namespace cv; 

int main(int argc, char *argv[]) 
{ 
    //cv::initModule_nonfree(); 
    //initModule_features2d(); 
    Mat img_1 = imread("C:/Users/Dan/Desktop/0.jpg", 1); 
    Mat img_2 = imread("C:/Users/Dan/Desktop/0.jpg", 1); 

    cv::Ptr<Feature2D> f2d = xfeatures2d::SIFT::create(); 

    //-- Step 1: Detect the keypoints: 
    std::vector<KeyPoint> keypoints_1, keypoints_2; 
    f2d->detect(img_1, keypoints_1); 
    f2d->detect(img_2, keypoints_2); 

    //-- Step 2: Calculate descriptors (feature vectors)  
    Mat descriptors_1, descriptors_2; 
    f2d->compute(img_1, keypoints_1, descriptors_1); 
    f2d->compute(img_2, keypoints_2, descriptors_2); 

    Mat out0; 
    drawKeypoints(img_1, keypoints_1, out0); 
    imshow("KeyPoint0.jpg", out0); 

    //-- Step 3: Matching descriptor vectors using BFMatcher : 
    BFMatcher matcher; 
    std::vector<DMatch> matches; 
    matcher.match(descriptors_1, descriptors_2, matches); 


    Mat img_matches = Mat::zeros(img_1.size(), CV_8UC3); 
    drawMatches(img_1,keypoints_1,img_2,keypoints_2,matches,img_matches); 
    imshow("matches", img_matches); 

    waitKey(0); // Keep window there until user presses 'q' to quit. 

    return 0; 

} 

Depuis OpenCV 3.1 fonctions ont été changées, je cherchais un exemple de code qui utilise SURF ou EIPD dans mon cas, mais je ne pouvais pas trouver tout.

Pouvez-vous m'aider et signaler l'achèvement du code afin qu'il dessine les contours autour des objets détectés comme in the code of the other version of OpenCV.

Merci, Dan.

+0

http://docs.opencv.org/trunk/d7/dff/ tutorial_feature_homog raphy.html # gsc.tab = 0 – Miki

Répondre

0

Vous devez utiliser findHomography pour obtenir la transformation qui concerne l'image de votre formation (img_1) à l'image à détecter (img_2)

Ensuite, vous pouvez tout simplement faire un perspectiveTransform sur une zone de délimitation de l'image de la formation (à l'origine) en utilisant l'homographie obtenu, pour placer la zone de délimitation correcte sur l'image détectée

code lié prise de ORB detection example

Mat inlier_mask, homography; 
vector<KeyPoint> inliers1, inliers2; 
vector<DMatch> inlier_matches; 
if(matched1.size() >= 4) { 
    homography = findHomography(Points(matched1), Points(matched2), 
           RANSAC, ransac_thresh, inlier_mask); 
} 

for(unsigned i = 0; i < matched1.size(); i++) { 
    if(inlier_mask.at<uchar>(i)) { 
     int new_i = static_cast<int>(inliers1.size()); 
     inliers1.push_back(matched1[i]); 
     inliers2.push_back(matched2[i]); 
     inlier_matches.push_back(DMatch(new_i, new_i, 0)); 
    } 
} 
stats.inliers = (int)inliers1.size(); 
stats.ratio = stats.inliers * 1.0/stats.matches; 

vector<Point2f> new_bb; 
perspectiveTransform(object_bb, new_bb, homography); 
Mat frame_with_bb = frame.clone(); 
if(stats.inliers >= bb_min_inliers) { 
    drawBoundingBox(frame_with_bb, new_bb); 
} 
Mat res; 
drawMatches(first_frame, inliers1, frame_with_bb, inliers2, 
      inlier_matches, res, 
      Scalar(255, 0, 0), Scalar(255, 0, 0));