Exemplo n.º 1
0
  public MatOfPoint getContourOfBestMatch() {

    if (matches == null) return null;

    Mat threshold = new Mat(imgGray.size(), imgGray.type());
    Imgproc.threshold(imgGray, threshold, 70, 255, Imgproc.THRESH_TOZERO);

    List<MatOfPoint> contours = new ArrayList<MatOfPoint>();
    Imgproc.findContours(
        threshold, contours, new Mat(), Imgproc.RETR_CCOMP, Imgproc.CHAIN_APPROX_NONE);
    // HashMap<Integer,MatOfPoint> coordinates = computeCoord(contours,)

    if (contours.size() == 0) return null;
    List<DMatch> matchList = matches.toList();
    List<KeyPoint> keyPointList = keyPointImg.toList();

    HashMap<Integer, Double> contourDensityMap = new HashMap<Integer, Double>();

    Log.i("getContourBestMatch::", "contour size::" + contours.size());

    for (int idx = 0; idx < contours.size(); idx++) {
      MatOfPoint2f ctr2f = new MatOfPoint2f(contours.get(idx).toArray());
      // double contourarea = Imgproc.contourArea(contours.get(idx));

      double contourarea = contours.get(idx).rows();
      if (contourarea < 50) continue;

      Rect r = Imgproc.boundingRect(contours.get(idx));

      double count = 0;
      // Log.i("contour area","contour area is::"+contourarea);
      for (DMatch match : matchList) {

        Point q = keyPointList.get(match.queryIdx).pt;
        if (q.x >= r.x && q.x <= (r.x + r.width) && q.y >= r.y && q.y <= (r.y + r.height)) count++;

        //
        // if(Imgproc.pointPolygonTest(ctr2f,keyPointList.get(match.queryIdx).pt,true)>0){
        //                    if(null ==contourDensityMap.get(idx))
        //                        contourDensityMap.put(idx,1.0);
        //
        //                    else{
        //                        contourDensityMap.put(idx,((Double)contourDensityMap.get(idx))+1);
        //                    }
        //
        //                }

      }
      //            if(contourDensityMap.containsKey(idx)) {
      //
      // Log.i("contourPoint","idx::"+idx+"count::"+contourDensityMap.get(idx)+"contour
      // area::"+contourarea);
      //                contourDensityMap.put(idx, contourDensityMap.get(idx) / contourarea);
      //            }
      if (count != 0) {
        contourDensityMap.put(idx, count / contourarea);
      }
    }

    Log.i("MarkerTracker", "contour density size::" + contourDensityMap.size());

    Map.Entry<Integer, Double> maxEntry = null;

    for (Map.Entry<Integer, Double> entry : contourDensityMap.entrySet()) {
      Log.i("contourDensityMap", "Entry value::" + entry.getValue());
      if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0) {
        maxEntry = entry;
      }
    }
    Log.i("maxEntry::", "" + (maxEntry == null ? null : maxEntry.getKey()));
    // return contours;
    return contours.get(maxEntry != null ? maxEntry.getKey() : 0);
  }
Exemplo n.º 2
0
  private void detectObject() {

    readLock.lock();

    featureDetector.detect(img_scene, keypoints_scene);
    featureDetector.detect(img_object, keypoints_object);

    extractor.compute(img_object, keypoints_object, descriptors_object);
    extractor.compute(img_scene, keypoints_scene, descriptors_scene);

    readLock.unlock();

    if (!descriptors_scene.empty()) {
      matcher.match(descriptors_object, descriptors_scene, matches);

      // readLock.unlock();

      //
      listMatches = matches.toList();

      int size = descriptors_object.rows();

      // -- Quick calculation of max and min distances between keypoints
      for (int i = 0; i < size; i++) {
        double dist = listMatches.get(i).distance;
        if (dist < min_dist) {
          min_dist = dist;
        }
      }

      Log.e("Min", min_dist + "");

      threeMinDist = 3 * min_dist;

      listGoodMatches.removeAll(listGoodMatches);

      for (int i = 0; i < size; i++) {
        DMatch dMatch = listMatches.get(i);

        float distance = dMatch.distance;

        if (distance < threeMinDist) {
          listGoodMatches.add(dMatch);
        }
      }

      // good_matches.fromList(listGoodMatches);

      Log.e("Matches", listMatches.size() + "");
      Log.e("Good Matches", listGoodMatches.size() + "");
      //

      if (listGoodMatches.size() > 4) {
        Point pointObj[] = new Point[listGoodMatches.size()];
        Point pointScene[] = new Point[listGoodMatches.size()];

        listKeyPointObject = keypoints_object.toList();
        listKeyPointScene = keypoints_scene.toList();

        // listPointScene.removeAll(listPointScene);
        for (int i = 0; i < listGoodMatches.size(); i++) {
          // -- Get the keypoints from the good matches
          pointObj[i] = listKeyPointObject.get(listGoodMatches.get(i).queryIdx).pt;
          pointScene[i] = listKeyPointScene.get(listGoodMatches.get(i).trainIdx).pt;

          // listPointScene.add(listKeyPointScene.get(listGoodMatches.get(i).trainIdx).pt);
        }

        obj.fromArray(pointObj);
        scene.fromArray(pointScene);

        Log.e("Before findHomography", "");

        H = Calib3d.findHomography(obj, scene, Calib3d.RANSAC, 9);

        Log.e("AFTERRR findHomography", "");

        pointObjConners[0] = new Point(0, 0);
        pointObjConners[1] = new Point(img_object.cols(), 0);
        pointObjConners[2] = new Point(img_object.cols(), img_object.rows());
        pointObjConners[3] = new Point(0, img_object.rows());

        obj_corners.fromArray(pointObjConners);

        Core.perspectiveTransform(obj_corners, scene_corners, H);

        p0 = new Point(scene_corners.toList().get(0).x, scene_corners.toList().get(0).y + 0);
        p1 = new Point(scene_corners.toList().get(1).x, scene_corners.toList().get(1).y + 0);
        p2 = new Point(scene_corners.toList().get(2).x, scene_corners.toList().get(2).y + 0);
        p3 = new Point(scene_corners.toList().get(3).x, scene_corners.toList().get(3).y + 0);

        Log.e("POINT THREAD", p0.toString() + p1.toString() + p2.toString() + p3.toString());

        Log.e("detect ok", "detect ok");
      }

    } else {
      Log.e("No descritor", "No descritor");

      // readLock.unlock();
    }
  }