You are on page 1of 3

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package surfdetector;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.*;
import org.opencv.features2d.*;
import org.opencv.highgui.Highgui;
import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
//import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.opencv.imgproc.Imgproc;
//import org.opencv.imgproc.Imgproc;
//import static org.opencv.imgproc.Imgproc.contourArea;
//import org.opencv.photo.Photo;

public class SURFDetector {


public static void main(String[] args) {
File lib = null;
String os = System.getProperty("os.name");
String bitness = System.getProperty("sun.arch.data.model");

if (os.toUpperCase().contains("WINDOWS")) {
if (bitness.endsWith("64")) {
lib = new File("C://opencv//build//java//x64//" +
System.mapLibraryName("opencv_java2413"));
} else {
lib = new File("C://opencv//build//java//x86//" +
System.mapLibraryName("opencv_java2413"));
}
}

System.out.println(lib.getAbsolutePath());
System.load(lib.getAbsolutePath());

//DATA UJI
String Scene = "images//aaa.jpg";
Mat sceneImage = Highgui.imread(Scene, Highgui.CV_LOAD_IMAGE_COLOR);

////////////////////////////////DATASET////////////////////////////////////////////
/////////////
String[] Object = new String[6];
ArrayList<Mat>objectImage = new ArrayList<Mat>();
ArrayList<MatOfKeyPoint> objectKeyPoints = new ArrayList<MatOfKeyPoint>();
FeatureDetector featureDetector =
FeatureDetector.create(FeatureDetector.SURF);
ArrayList <Mat> objectDescriptors = new ArrayList<Mat>();
DescriptorExtractor descriptorExtractor =
DescriptorExtractor.create(DescriptorExtractor.SURF);
Mat[] outputImage = new Mat[6];
for (int i = 0; i <Object.length; i++) {
Object[i]="images//dataset//"+i+".jpg";
objectImage.add(Highgui.imread(Object[i],
Highgui.CV_LOAD_IMAGE_COLOR));
//System.out.println(objectImage.get(i));
Imgproc.resize(objectImage.get(i), objectImage.get(i), new Size(640,
640));
//System.out.println(objectImage.get(i));
}
featureDetector.detect(objectImage, objectKeyPoints);

descriptorExtractor.compute(objectImage, objectKeyPoints,
objectDescriptors);

Scalar newKeypointColor = new Scalar(255, 0, 0);

for (int i = 0; i <objectImage.size(); i++) {


outputImage[i] = new Mat(objectImage.get(i).rows(),
objectImage.get(i).cols(), Highgui.CV_LOAD_IMAGE_COLOR);
Features2d.drawKeypoints(objectImage.get(i), objectKeyPoints.get(i),
outputImage[i], newKeypointColor, 0);
System.out.println(objectDescriptors.get(i));
//showResult(outputImage[i]);
}

////////////////////////////////////DATASET////////////////////////////////////////
///////////////////////

/////////////////////////////////////SCENE/////////////////////////////////////////
//////////////////////
MatOfKeyPoint sceneKeyPoints = new MatOfKeyPoint();
MatOfKeyPoint sceneDescriptors = new MatOfKeyPoint();
featureDetector.detect(sceneImage, sceneKeyPoints);
descriptorExtractor.compute(sceneImage, sceneKeyPoints, sceneDescriptors);

Mat matchoutput = new Mat(sceneImage.rows() * 2, sceneImage.cols() * 2,


Highgui.CV_LOAD_IMAGE_COLOR);
Scalar matchestColor = new Scalar(0, 255, 0);

List<MatOfDMatch> matches = new LinkedList<MatOfDMatch>();


DescriptorMatcher descriptorMatcher =
DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);

//////////////////////////////////////SCENE////////////////////////////////////////
///////////////////////

////////////////////////////////////MATCHING///////////////////////////////////////
///////////////////////
descriptorMatcher.knnMatch(sceneDescriptors, matches, 2, objectImage,
true);
System.out.println(matches.toString());

Features2d.drawMatches(objectImage, objectKeyPoints, sceneImage, sceneKe


}
public static void showResult(Mat img) {
//Imgproc.resize(img, img, new Size(640, 480));
MatOfByte matOfByte = new MatOfByte();
Highgui.imencode(".jpg", img, matOfByte);
byte[] byteArray = matOfByte.toArray();
BufferedImage bufImage = null;
try {
InputStream in = new ByteArrayInputStream(byteArray);
bufImage = ImageIO.read(in);
JFrame frame = new JFrame();
frame.getContentPane().add(new JLabel(new ImageIcon(bufImage)));
frame.pack();
frame.setLocationRelativeTo(frame);
frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}