2012-06-15 15 views
5

मैं निम्नलिखित कोड सर्फ डिटेक्टर से जुड़े से एक वस्तु का पता लगाने की कोशिश कर रहा हूँ का उपयोग कर, मैं मैचों आकर्षित करने के लिए नहीं करना चाहते, मैं पता लगाया वस्तु के चारों ओर एक आयत बनाने के लिए चाहते हैं, लेकिन किसी भी तरह मैं करने में असमर्थ हूँ सही homography मिलता है, कृपया किसी को भी बाहर बिंदु जहां मैं गलत जा रहा हूँ कर सकते हैं।आकर्षित आयत के आसपास वस्तु का पता चला सर्फ

#include <stdio.h> 
#include <iostream> 
#include "opencv2/core/core.hpp" 
#include "opencv2/features2d/features2d.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 
#include "opencv2/calib3d/calib3d.hpp" 

using namespace cv; 

int main() 
{ 
    Mat object = imread("sample.jpeg", CV_LOAD_IMAGE_GRAYSCALE); 

    if(!object.data) 
    { 
     std::cout<< "Error reading object " << std::endl; 
     return -1; 
    } 

    //Detect the keypoints using SURF Detector 
    int minHessian = 500; 

    SurfFeatureDetector detector(minHessian); 
    std::vector<KeyPoint> kp_object; 

    detector.detect(object, kp_object); 

    //Calculate descriptors (feature vectors) 
    SurfDescriptorExtractor extractor; 
    Mat des_object; 

    extractor.compute(object, kp_object, des_object); 

    FlannBasedMatcher matcher; 

    VideoCapture cap(0); 

    namedWindow("Good Matches",0); 
    cvResizeWindow("Good Matches",800,800); 

    std::vector<Point2f> obj_corners(4); 

    //Get the corners from the object 
    obj_corners[0] = (cvPoint(0,0)); 
    obj_corners[1] = (cvPoint(object.cols,0)); 
    obj_corners[2] = (cvPoint(object.cols,object.rows)); 
    obj_corners[3] = (cvPoint(0, object.rows)); 

    char key = 'a'; 
    int framecount = 0; 
    while (key != 27) 
    { 
     Mat frame; 
     cap >> frame; 

     if (framecount < 5) 
     { 
      framecount++; 
      continue; 
     } 

     Mat des_image, img_matches; 
     std::vector<KeyPoint> kp_image; 
     std::vector<vector<DMatch > > matches; 
     std::vector<DMatch > good_matches; 
     std::vector<Point2f> obj; 
     std::vector<Point2f> scene; 
     std::vector<Point2f> scene_corners(4); 
     Mat H; 
     Mat image; 

     cvtColor(frame, image, CV_RGB2GRAY); 

     detector.detect(image, kp_image); 
     extractor.compute(image, kp_image, des_image); 

     matcher.knnMatch(des_object, des_image, matches, 2); 

     for(int i = 0; i < min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS 
     { 
      if((matches[i][0].distance < 0.6*(matches[i][1].distance)) && ((int) matches[i].size()<=2 && (int) matches[i].size()>0)) 
     { 
       good_matches.push_back(matches[i][0]); 
      } 
     } 

     //Draw only "good" matches 
    // drawMatches(object, kp_object, image, kp_image, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector<char>(),DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS); 

     if (good_matches.size() >= 4) 
     { 
      for(int i = 0; i < good_matches.size(); i++) 
      { 
       //Get the keypoints from the good matches 
       obj.push_back(kp_object[ good_matches[i].queryIdx ].pt); 
       scene.push_back(kp_image[ good_matches[i].trainIdx ].pt); 
      } 

      H = findHomography(obj, scene, CV_RANSAC); 

      perspectiveTransform(obj_corners, scene_corners, H); 

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

     //Show detected matches 
     imshow("Good Matches", image); 

     key = waitKey(1); 
    } 
    return 0; 
} 
+0

किसी भी तरह किस तरह से असमर्थ में छवि दिखाई? आप काफी अच्छा मैचों नहीं मिल रहा है, गणना homography सिर्फ सादा गलत क्या है? हो सकता है आप वस्तुओं के बाहर कुछ मैचों है? – penelope

+0

@penelope मैचों काफी अच्छा कर रहे हैं, लेकिन आयत जो वस्तु के आसपास माना जाता है उसके चारों ओर नहीं है, इसलिए मुझे लगता है मैं गलत निर्देशांक की गणना कर रहा हूँ लगता है। – sum2000

उत्तर

4

आप पता लगाया वस्तु जब आप सीमा रेखा कैसे img_matches सरणी और नहीं छवि उपयोग करने की आवश्यकता के आसपास एक दूसरे को बाउंडिंग आयत के साथ दोनों तस्वीरें देखने के लिए चाहते हैं।

line(image, scene_corners[0], scene_corners[1], Scalar(0, 255, 0), 4);

:

आप चिह्नित वस्तु और छवियों की नहीं जोड़ी के साथ ही छवि देखने के लिए (के रूप में आप छवि में ड्राइंग लाइनों द्वारा परिभाषित) चाहते हैं, तो आप सिर्फ करने के लिए अपने कोड को बदलने की जरूरत

line(image, scene_corners[1], scene_corners[2], Scalar(0, 255, 0), 4);

line(image, scene_corners[2], scene_corners[3], Scalar(0, 255, 0), 4);

line(image, scene_corners[3], scene_corners[0], Scalar(0, 255, 0), 4);

और एक नई विंडो

imshow("Result", image);

संबंधित मुद्दे