Keypoint matching just works two times...? (java opencv)

asked 2014-09-09 14:33:55 -0500

Hey there,

I have a very strange problem. I'm using this code to detect an image in another one:

public void startRecognition() {
    run++;
    System.out.println("RUN NO: " + run);
    Mat img_scene = Highgui.imread((String)pics.keySet().toArray()[current_scene]);
    Mat img_object = Highgui.imread(pics.get((String)pics.keySet().toArray()[current_scene]).get(current_obj));

    try {
        FileWriter writer = new FileWriter("D:/opencvws/ImageRecognition/src/main/resources/test.yaml");

            writer.write("%YAML:1.0\nhessianThreshold: "+hess_thresh+".\n");

        writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    System.out.println(new Date().getTime());
    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
    detector.read("D:/opencvws/ImageRecognition/src/main/resources/test.yaml");

    MatOfKeyPoint keypoints_object = new MatOfKeyPoint();
    MatOfKeyPoint keypoints_scene  = new MatOfKeyPoint();

    detector.detect(img_object, keypoints_object);
    detector.detect(img_scene, keypoints_scene);
    System.out.println("OK: " + keypoints_object.total());
    System.out.println("SK: " + keypoints_scene.total());


    DescriptorExtractor extractor = DescriptorExtractor.create(2); //2 = SURF;

    Mat descriptor_object = new Mat();
    Mat descriptor_scene = new Mat() ;

    extractor.compute(img_object, keypoints_object, descriptor_object);
    extractor.compute(img_scene, keypoints_scene, descriptor_scene);

    DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED); // 1 = FLANNBASED
    matcher.clear();
    MatOfDMatch matches = new MatOfDMatch();

    matcher.match(descriptor_object, descriptor_scene, matches);
    List<DMatch> matchesList = matches.toList();
    System.out.println(matches.total());

    Double max_dist = 0.0;
    Double min_dist = 100.0;

    for(int i = 0; i < descriptor_object.rows(); i++){
        Double dist = (double) matchesList.get(i).distance;
        if(dist < min_dist) min_dist = dist;
        if(dist > max_dist) max_dist = dist;
    }

    LinkedList<DMatch> good_matches = new LinkedList<DMatch>();
    MatOfDMatch gm = new MatOfDMatch();

    for(int i = 0; i < descriptor_object.rows(); i++){
        if(matchesList.get(i).distance < 2*min_dist){
            good_matches.addLast(matchesList.get(i));
        }
    }

    gm.fromList(good_matches);

    Mat img_matches = new Mat();


    LinkedList<Point> objList = new LinkedList<Point>();
    LinkedList<Point> sceneList = new LinkedList<Point>();

    List<KeyPoint> keypoints_objectList = keypoints_object.toList();
    List<KeyPoint> keypoints_sceneList = keypoints_scene.toList();

    for(int i = 0; i<good_matches.size(); i++){
        objList.addLast(keypoints_objectList.get(good_matches.get(i).queryIdx).pt);
        sceneList.addLast(keypoints_sceneList.get(good_matches.get(i).trainIdx).pt);
    }

    MatOfPoint2f obj = new MatOfPoint2f();
    obj.fromList(objList);

    MatOfPoint2f scene = new MatOfPoint2f();
    scene.fromList(sceneList);

    Mat hg = Calib3d.findHomography(obj, scene, 8,3);
    System.out.println(hg.toString());
    Mat obj_corners = new Mat(4,1,CvType.CV_32FC2);
    Mat scene_corners = new Mat(4,1,CvType.CV_32FC2);

    obj_corners.put(0, 0, new double[] {0,0});
    obj_corners.put(1, 0, new double[] {img_object.cols(),0});
    obj_corners.put(2, 0, new double[] {img_object.cols(),img_object.rows()});
    obj_corners.put(3, 0, new double[] {0,img_object.rows()});
    System.out.println(hg.get(0, 0)[0] + ", " + hg.get(1, 0)[0] + ", " +hg.get(2, 0)[0]);
    System.out.println(hg.get(0, 1)[0] + ", " + hg.get(1, 1)[0] + ", " +hg.get(2, 1)[0]);
    System.out.println(hg.get(0, 2)[0] + ", " + hg.get(1, 2)[0] + ", " +hg.get(2, 2)[0]);

    Core.perspectiveTransform(obj_corners,scene_corners, hg);


    Point p1 = new Point(scene_corners.get(0,0)[0]+img_object.cols(), scene_corners.get(0,0)[1]);
    Point p2 = new Point(scene_corners.get(1,0)[0]+img_object.cols(), scene_corners.get(1,0)[1]);
    Point p3 = new Point(scene_corners.get(2,0)[0]+img_object.cols(), scene_corners.get(2,0)[1]);
    Point p4 = new Point(scene_corners.get(3,0 ...
(more)
edit retag flag offensive close merge delete