Exemplo n.º 1
0
  public void performMatch() {

    // create feature detectors and feature extractors
    FeatureDetector orbDetector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor orbExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

    // set the keypoints
    keyPointImg = new MatOfKeyPoint();
    orbDetector.detect(imgGray, keyPointImg);

    MatOfKeyPoint keyPointTempl = new MatOfKeyPoint();
    orbDetector.detect(templGray, keyPointTempl);

    // get the descriptions
    descImg = new Mat(image.size(), image.type());
    orbExtractor.compute(imgGray, keyPointImg, descImg);

    Mat descTempl = new Mat(template.size(), template.type());
    orbExtractor.compute(templGray, keyPointTempl, descTempl);

    // perform matching
    matches = new MatOfDMatch();
    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
    matcher.match(descImg, descTempl, matches);

    Log.i("perform match result", matches.size().toString());
  }
Exemplo n.º 2
0
  public void onCameraViewStarted(int width, int height) {
    try {

      detectThread = null;

      if (detectThread == null) {
        detectThread = new DetectThread();
        thread = new Thread(detectThread);
        thread.setPriority(Thread.MIN_PRIORITY);
        thread.start();
      }

      // -- Step 1: Detect the keypoints using SURF Detector
      featureDetector = FeatureDetector.create(FeatureDetector.FAST);

      // -- Step 2: Calculate descriptors (feature vectors)
      extractor = DescriptorExtractor.create(DescriptorExtractor.FREAK);

      // -- Step 3: Matching descriptor vectors using FLANN matcher
      matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_L1);

      img_object = Utils.loadResource(this, R.drawable.cardobj, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

      img_scene = new Mat();
      // img_scene = Utils.loadResource(this, R.drawable.cardscene,
      // Highgui.CV_LOAD_IMAGE_GRAYSCALE);

      matches = new MatOfDMatch();

      listGoodMatches = new ArrayList<>();

      keypoints_object = new MatOfKeyPoint();
      keypoints_scene = new MatOfKeyPoint();
      descriptors_object = new Mat();
      descriptors_scene = new Mat();

      obj = new MatOfPoint2f();
      scene = new MatOfPoint2f();

      H = new Mat();
      obj_corners = new MatOfPoint2f();

      scene_corners = new MatOfPoint2f();
      min_dist = 9999999;

      p0 = new Point(0, 0);
      p1 = new Point(0, 0);
      p2 = new Point(0, 0);
      p3 = new Point(0, 0);

      good_matches = new MatOfDMatch();

      listPointScene = new ArrayList<>();

    } catch (Exception ex) {

    }
  }
  private Mat getTestDescriptors(Mat img) {
    MatOfKeyPoint keypoints = new MatOfKeyPoint();
    Mat descriptors = new Mat();

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.FAST);
    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.BRIEF);

    detector.detect(img, keypoints);
    extractor.compute(img, keypoints, descriptors);

    return descriptors;
  }
Exemplo n.º 4
0
  public Template performMatches(Map<String, Template> templates) {

    // create feature detectors and feature extractors
    FeatureDetector orbDetector = FeatureDetector.create(FeatureDetector.ORB);
    DescriptorExtractor orbExtractor = DescriptorExtractor.create(DescriptorExtractor.ORB);

    MatOfKeyPoint keyPointImgT;
    Mat descImgT;
    // set the keypoints
    keyPointImgT = new MatOfKeyPoint();
    orbDetector.detect(imgGray, keyPointImgT);

    descImgT = new Mat(image.size(), image.type());
    orbExtractor.compute(imgGray, keyPointImgT, descImgT);

    Template best = null;
    matches = null;
    Map.Entry<String, Template> maxEntry = null;
    //  MatOfDMatch matches = new MatOfDMatch();

    for (Map.Entry<String, Template> entry : templates.entrySet()) {

      MatOfKeyPoint keyPointTempl = null;
      Mat descTempl = null;
      Mat tGray = null;

      Template t = entry.getValue();
      if (null == t.getTemplGray() || null == t.getDescTempl() || null == t.getKeyPointTempl()) {
        // read image from stored data
        Mat templ = readImgFromFile(t.getTemplName());

        tGray = new Mat(templ.size(), templ.type());
        Imgproc.cvtColor(templ, tGray, Imgproc.COLOR_BGRA2GRAY);

        keyPointTempl = new MatOfKeyPoint();
        orbDetector.detect(tGray, keyPointTempl);

        descTempl = new Mat(templ.size(), templ.type());
        orbExtractor.compute(tGray, keyPointTempl, descTempl);

        t.setKeyPointTempl(keyPointTempl);
        t.setDescTempl(descTempl);
      } else {
        descTempl = t.getDescTempl();
      }

      MatOfDMatch matchWithT = new MatOfDMatch();
      DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE_HAMMING);
      // matcher.radiusMatch(descImgT, descTempl, matchWithT,200);//
      matcher.match(descImgT, descTempl, matchWithT);
      List<DMatch> matchList = matchWithT.toList();
      //            float min = Float.MAX_VALUE;
      //            float max = Float.MIN_VALUE;
      //            for(int i=0;i<matchList.size();i++){
      //                min = matchList.get(i).distance<min?matchList.get(i).distance:min;
      //                max = matchList.get(i).distance>max?matchList.get(i).distance:max;
      //            }
      //            Log.i("min distance","min distance is::"+min+"max
      // distance::"+max+"size::"+matchList.size());

      //            Collections.sort(matchList, new Comparator<DMatch>() {
      //                @Override
      //                public int compare(DMatch o1, DMatch o2) {
      //                    if (o1.distance < o2.distance)
      //                        return -1;
      //                    if (o1.distance > o2.distance)
      //                        return 1;
      //                    return 0;
      //                }
      //            });

      float ratio = -1;
      if (matchList.size() > 0) ratio = findMinTwoRatio(matchList);

      if (ratio > 0.8 || ratio == -1) continue;
      Log.i("match", "ratio::" + ratio);

      // Todo:revisit logic
      if (matches == null || (matchWithT.size().height > matches.size().height)) {
        matches = matchWithT;
        keyPointImg = keyPointImgT;
        descImg = descImgT;
        best = t;
      }
    }

    //  Log.i("perform match result", matches.size().toString());

    return best;
  }