Fixed the algorithm to convert the features to a json string.

parent 03c4b6bd
package com.aluxoft.earrecognition;
import com.aluxoft.earrecognition.utils.OpenCvUtils;
import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.opencv.core.Mat;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Size;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.FeatureDetector;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import java.lang.reflect.Type;
import java.util.Vector;
import java.util.ArrayList;
/**
* Created by josejuliomartinez on 06/10/15.
......@@ -50,14 +45,21 @@ public class EarIdentifier {
keypoints = new MatOfKeyPoint();
image = Highgui.imread(imagePath, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
OpenCvUtils.resizeMat(image, 160, 120);
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT);
detector.detect(image, keypoints);
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
extractor.compute(image, keypoints, descriptors);
Vector<Integer> features = new Vector<Integer>();
ArrayList<ArrayList<Integer>> features = new ArrayList<>();
for (int i=0;i<descriptors.rows();++i) {
features.add((int)descriptors.get(i, 0)[0]);
ArrayList<Integer> feature = new ArrayList<>();
for (int j=0;j<descriptors.cols();++j) {
feature.add((int)descriptors.get(i, j)[0]);
}
features.add(feature);
}
Gson gson = new Gson();
return gson.toJson(features);
......@@ -68,24 +70,4 @@ public class EarIdentifier {
System.loadLibrary("nonfree");
}
private void resizeMat(Mat image) {
final double kMaxWidth = 160;
final double kMaxHeight = 120;
if (image.width() <= kMaxWidth && image.height() <= kMaxHeight) {
return;
}
double ratio = (double)image.width() / (double)image.height();
double width, height;
if (ratio > 1) {
width = kMaxWidth;
height = kMaxWidth * ((double)image.height()/(double)image.width());
} else {
height = kMaxHeight;
width = kMaxHeight* ratio;
}
Imgproc.resize(image, image, new Size(width, height));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment