Se termina la identificación de dos orejas.

parent 4c43ae5e
...@@ -24,17 +24,23 @@ import android.widget.Button; ...@@ -24,17 +24,23 @@ import android.widget.Button;
import android.widget.ImageView; import android.widget.ImageView;
import org.opencv.android.Utils; import org.opencv.android.Utils;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.Mat; import org.opencv.core.Mat;
import org.opencv.core.MatOfByte; import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch; import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint; import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar; import org.opencv.core.Scalar;
import org.opencv.core.Size; import org.opencv.core.Size;
import org.opencv.core.TermCriteria;
import org.opencv.features2d.DMatch; import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor; import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher; import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector; import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d; import org.opencv.features2d.Features2d;
import org.opencv.features2d.KeyPoint;
import org.opencv.highgui.Highgui; import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc; import org.opencv.imgproc.Imgproc;
...@@ -46,20 +52,35 @@ import java.util.Vector; ...@@ -46,20 +52,35 @@ import java.util.Vector;
public class CameraActivity extends ActionBarActivity { public class CameraActivity extends ActionBarActivity {
String image1 = "/storage/emulated/0/DCIM/Camera/000_down_ear.jpg";
String image2 = "/storage/emulated/0/DCIM/Camera/001_front_ear.jpg";
String image3 = "/storage/emulated/0/DCIM/Camera/000_front_ear.jpg";
String image4 = "/storage/emulated/0/DCIM/Camera/test_1.png";
String image5 = "/storage/emulated/0/DCIM/Camera/test_3.png";
String image6 = "/storage/emulated/0/DCIM/Camera/test_2.png";
String image_patty_01 = "/storage/emulated/0/DCIM/Camera/PATTY_01.jpg";
String image_patty_02 = "/storage/emulated/0/DCIM/Camera/PATTY_02.jpg";
String image_elena_01 = "/storage/emulated/0/DCIM/Camera/ELENA_01.jpg";
String image_elena_02 = "/storage/emulated/0/DCIM/Camera/ELENA_02.jpg";
private static final int REQUEST_IMAGE_CAPTURE = 1; private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int SELECT_PICTURE = 0; private static final int SELECT_PICTURE = 0;
private String mCurrentPhotoPath; private String mCurrentPhotoPath;
private String mCurrentPhotoPath1; private String mCurrentPhotoPath1 = null;//image_patty_02;
private String mCurrentPhotoPath2; private String mCurrentPhotoPath2 = null;//image_elena_01;
private String mCurrentPhotoPath3 = null;//image_elena_02;
private int previewWidth = 150; private int previewWidth = 150;
private int previewHeight = 150; private int previewHeight = 150;
private ImageView previewImage1; private ImageView previewImage1;
private ImageView previewImage2; private ImageView previewImage2;
private ImageView previewImage3;
private ImageView previewImage; private ImageView previewImage;
public void resizeMatToVga(Mat image) { public void resizeMat(Mat image) {
final double kMaxWidth = 640; final double kMaxWidth = 160;
final double kMaxHeight = 480; final double kMaxHeight = 120;
if (image.width() <= kMaxWidth && image.height() <= kMaxHeight) { if (image.width() <= kMaxWidth && image.height() <= kMaxHeight) {
return; return;
...@@ -69,7 +90,7 @@ public class CameraActivity extends ActionBarActivity { ...@@ -69,7 +90,7 @@ public class CameraActivity extends ActionBarActivity {
double width, height; double width, height;
if (ratio > 1) { if (ratio > 1) {
width = kMaxWidth; width = kMaxWidth;
height = kMaxWidth * (image.height()/image.width()); height = kMaxWidth * ((double)image.height()/(double)image.width());
} else { } else {
height = kMaxHeight; height = kMaxHeight;
width = kMaxHeight* ratio; width = kMaxHeight* ratio;
...@@ -77,43 +98,95 @@ public class CameraActivity extends ActionBarActivity { ...@@ -77,43 +98,95 @@ public class CameraActivity extends ActionBarActivity {
Imgproc.resize(image, image, new Size(width, height)); Imgproc.resize(image, image, new Size(width, height));
} }
public void doOpenCvMagic() {
System.loadLibrary("opencv_java"); public double distanceFromData(DescriptorMatcher matcher, MatOfKeyPoint keypoints1, Mat descriptors1, MatOfKeyPoint keypoints2, Mat descriptors2) {
System.loadLibrary("nonfree"); MatOfDMatch matches = new MatOfDMatch();
matcher.match(descriptors1, descriptors2, matches);
DMatch[] aMatches = matches.toArray();
double distance = 0;
for (int i=0;i<aMatches.length;++i) {
distance += aMatches[i].distance;
}
return distance;
}
public void doOpenCvMagic() {
Mat img1 = Highgui.imread(mCurrentPhotoPath1, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat img1 = Highgui.imread(mCurrentPhotoPath1, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = Highgui.imread(mCurrentPhotoPath2, Highgui.CV_LOAD_IMAGE_GRAYSCALE); resizeMat(img1);
Mat img2 = Highgui.imread(mCurrentPhotoPath2, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
resizeMat(img2);
resizeMatToVga(img1); Mat img3 = Highgui.imread(mCurrentPhotoPath3, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
resizeMatToVga(img2); resizeMat(img3);
int minHessian = 400; FeatureDetector detector = FeatureDetector.create(FeatureDetector.SIFT);
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint(); MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint(); MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
MatOfKeyPoint keypoints3 = new MatOfKeyPoint();
detector.detect(img1, keypoints1); detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2); detector.detect(img2, keypoints2);
detector.detect(img3, keypoints3);
Mat descriptors1 = new Mat(); Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat(); Mat descriptors2 = new Mat();
Mat descriptors3 = new Mat();
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF); DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SIFT);
extractor.compute(img1, keypoints1, descriptors1); extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2); extractor.compute(img2, keypoints2, descriptors2);
extractor.compute(img3, keypoints3, descriptors3);
MatOfDMatch matches = new MatOfDMatch(); DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.BRUTEFORCE);
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED); Mat outImg1 = new Mat();
matcher.match(descriptors1, descriptors2, matches); Mat outImg2 = new Mat();
Mat outImg3 = new Mat();
double max_dist = 0; double distance13 = distanceFromData(matcher, keypoints3, descriptors3, keypoints1, descriptors1);
double min_dist = 100; double distance23 = distanceFromData(matcher, keypoints3, descriptors3, keypoints2, descriptors2);
DMatch[] aMatches = matches.toArray(); String message = "";
if (distance13 < distance23) {
message = "It's more likely to be the subject 1";
} else if (distance23 < distance13) {
message = "It's more likely to be the subject 2";
} else {
message = "Both subjects are equally likely";
}
new AlertDialog.Builder(this)
.setTitle("Identification")
.setMessage(message)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
Features2d.drawKeypoints(img1, keypoints1, outImg1);
Features2d.drawKeypoints(img2, keypoints2, outImg2);
Features2d.drawKeypoints(img3, keypoints3, outImg3);
Bitmap bitmap = Bitmap.createBitmap(outImg1.cols(), outImg1.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outImg1, bitmap);
previewImage1.setImageBitmap(bitmap);
bitmap = Bitmap.createBitmap(outImg2.cols(), outImg2.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outImg2, bitmap);
previewImage2.setImageBitmap(bitmap);
bitmap = Bitmap.createBitmap(outImg3.cols(), outImg3.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(outImg3, bitmap);
previewImage3.setImageBitmap(bitmap);
/*
for (int i=0;i<aMatches.length;++i) { for (int i=0;i<aMatches.length;++i) {
double dist = aMatches[i].distance; double dist = aMatches[i].distance;
...@@ -140,16 +213,21 @@ public class CameraActivity extends ActionBarActivity { ...@@ -140,16 +213,21 @@ public class CameraActivity extends ActionBarActivity {
Bitmap bitmap = Bitmap.createBitmap(image_matches.cols(), image_matches.rows(), Bitmap.Config.ARGB_8888); Bitmap bitmap = Bitmap.createBitmap(image_matches.cols(), image_matches.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image_matches, bitmap); Utils.matToBitmap(image_matches, bitmap);
previewImage1.setImageBitmap(bitmap); previewImage1.setImageBitmap(bitmap);*/
} }
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
System.loadLibrary("opencv_java");
System.loadLibrary("nonfree");
setContentView(R.layout.activity_camera); setContentView(R.layout.activity_camera);
previewImage1 = (ImageView)findViewById(R.id.imageView); previewImage1 = (ImageView)findViewById(R.id.imageView);
previewImage2 = (ImageView)findViewById(R.id.imageView2); previewImage2 = (ImageView)findViewById(R.id.imageView2);
previewImage3 = (ImageView)findViewById(R.id.imageView3);
final Button takeButton = (Button)findViewById(R.id.button); final Button takeButton = (Button)findViewById(R.id.button);
takeButton.setOnClickListener(new View.OnClickListener() { takeButton.setOnClickListener(new View.OnClickListener() {
@Override @Override
...@@ -171,12 +249,22 @@ public class CameraActivity extends ActionBarActivity { ...@@ -171,12 +249,22 @@ public class CameraActivity extends ActionBarActivity {
} }
}); });
final Button toIdentifyButton = (Button) findViewById(R.id.toidentify);
toIdentifyButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
previewImage = previewImage3;
takePhoto(takeButton);
mCurrentPhotoPath3 = mCurrentPhotoPath;
}
});
final Button processButton = (Button) findViewById(R.id.button3); final Button processButton = (Button) findViewById(R.id.button3);
processButton.setOnClickListener(new View.OnClickListener(){ processButton.setOnClickListener(new View.OnClickListener(){
@Override @Override
public void onClick(View v) { public void onClick(View v) {
// do opencv magic here... // do opencv magic here...
if (mCurrentPhotoPath1 != null && mCurrentPhotoPath2 != null) { if (mCurrentPhotoPath1 != null && mCurrentPhotoPath2 != null && mCurrentPhotoPath3 != null) {
doOpenCvMagic(); doOpenCvMagic();
} }
} }
...@@ -271,6 +359,7 @@ public class CameraActivity extends ActionBarActivity { ...@@ -271,6 +359,7 @@ public class CameraActivity extends ActionBarActivity {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA); int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
if (cursor.moveToFirst()) { if (cursor.moveToFirst()) {
IMAGE_FILEPATH = cursor.getString(column_index); IMAGE_FILEPATH = cursor.getString(column_index);
System.out.println(IMAGE_FILEPATH);
Bitmap galleryImage = getBitMapForPreview(IMAGE_FILEPATH,previewWidth,previewHeight); Bitmap galleryImage = getBitMapForPreview(IMAGE_FILEPATH,previewWidth,previewHeight);
previewImage.setImageBitmap(galleryImage); previewImage.setImageBitmap(galleryImage);
} }
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
<Button <Button
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Cargar primera imagen" android:text="Capture First ear"
android:id="@+id/button" android:id="@+id/button"
android:layout_marginTop="47dp" android:layout_marginTop="47dp"
android:layout_alignParentTop="true" android:layout_alignParentTop="true"
...@@ -25,13 +25,23 @@ ...@@ -25,13 +25,23 @@
<Button <Button
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Cargar segunda imagen" android:text="Capture Second ear"
android:id="@+id/button2" android:id="@+id/button2"
android:layout_centerVertical="true" android:layout_centerVertical="true"
android:layout_alignParentLeft="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_alignParentStart="true"
android:layout_below="@+id/imageView"/> android:layout_below="@+id/imageView"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Capture ear to identify"
android:id="@+id/toidentify"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/imageView2"/>
<ImageView <ImageView
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
...@@ -54,12 +64,23 @@ ...@@ -54,12 +64,23 @@
android:layout_alignEnd="@+id/button2" android:layout_alignEnd="@+id/button2"
android:maxHeight="150dp" /> android:maxHeight="150dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView3"
android:layout_below="@+id/toidentify"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="@+id/button2"
android:layout_alignEnd="@+id/button2"
android:maxHeight="150dp" />
<Button <Button
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Procesar" android:text="Identify"
android:id="@+id/button3" android:id="@+id/button3"
android:layout_below="@+id/imageView2" android:layout_below="@+id/imageView3"
android:layout_alignParentLeft="true" /> android:layout_alignParentLeft="true" />
......
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