Ask Your Question

Revision history [back]

I have been trying to write this in java but couldn't get correct results...Even after using the above code and changes from comments I am not getting correct results oDetection1.png. Please tell me what mistake I am doing?.

Using the above code and changes,code:

public class FindO { public void run() {

    System.out.println("\nRunning FindObject");
    System.loadLibrary("opencv_java244");


    String object_filename = "D:\\box.png";
    String scene_filename = "D:\\box_in_scene.png";

    Mat img_object = Highgui.imread(object_filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE); // 0 =
                                                            // CV_LOAD_IMAGE_GRAYSCALE
    Mat img_scene = Highgui.imread(scene_filename, Highgui.CV_LOAD_IMAGE_GRAYSCALE);

    FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF); // 4 = SURF

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

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

    DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF); // 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
    MatOfDMatch matches = new MatOfDMatch();

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

    Double max_dist = 0.0;
    Double min_dist = 100.0;

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

    System.out.println("-- Max dist : " + max_dist);
    System.out.println("-- Min dist : " + min_dist);

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

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


    gm.fromList(good_matches);

    Mat img_matches = new Mat();
    Features2d.drawMatches(img_object, keypoints_object, img_scene,
            keypoints_scene, gm, img_matches, new Scalar(255, 0, 0),
            new Scalar(0, 0, 255), new MatOfByte(), 2);

    String filename = "D:\\oDetection.png";

    System.out.println(String.format("Writing %s", filename));
    Highgui.imwrite(filename, img_matches);

    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 H = Calib3d.findHomography(obj, scene);





    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()});

    Core.perspectiveTransform(obj_corners,scene_corners, H);

    Mat img = Highgui.imread(scene_filename, Highgui.CV_LOAD_IMAGE_COLOR);

    Core.line(img, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
    Core.line(img, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
    Core.line(img, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
    Core.line(img, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);

    filename = "D:\\oDetection1.png";

    System.out.println(String.format("Writing %s", filename));
    Highgui.imwrite(filename, img);
}

public static void main(String[] args) {
    System.loadLibrary("opencv_java244");
    new FindO().run();
}

}

Thanks in advance!!