Se termina el primer protitpo, se acopla el OpenCV al android, se transcribe...

Se termina el primer protitpo, se acopla el OpenCV al android, se transcribe parte del algoritmo que esta aquí: http://docs.opencv.org/doc/tutorials/features2d/feature_homography/feature_homography.html
parent a022cc3a
......@@ -27,23 +27,6 @@
</value>
</option>
</component>
<component name="ProjectInspectionProfilesVisibleTreeState">
<entry key="Project Default">
<profile-state>
<expanded-state>
<State>
<id />
</State>
<State>
<id>Android</id>
</State>
<State>
<id>Android Lint</id>
</State>
</expanded-state>
</profile-state>
</entry>
</component>
<component name="ProjectLevelVcsManager" settingsEditedManually="false">
<OptionsSetting value="true" id="Add" />
<OptionsSetting value="true" id="Remove" />
......
......@@ -23,22 +23,127 @@ import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.Scalar;
import org.opencv.core.Size;
import org.opencv.features2d.DMatch;
import org.opencv.features2d.DescriptorExtractor;
import org.opencv.features2d.DescriptorMatcher;
import org.opencv.features2d.FeatureDetector;
import org.opencv.features2d.Features2d;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Vector;
public class CameraActivity extends ActionBarActivity {
private static final int REQUEST_IMAGE_CAPTURE = 1;
private static final int SELECT_PICTURE = 0;
private String mCurrentPhotoPath;
private String mCurrentPhotoPath1;
private String mCurrentPhotoPath2;
private int previewWidth = 150;
private int previewHeight = 150;
private ImageView previewImage1;
private ImageView previewImage2;
private ImageView previewImage;
public void resizeMatToVga(Mat image) {
final double kMaxWidth = 640;
final double kMaxHeight = 480;
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 * (image.height()/image.width());
} else {
height = kMaxHeight;
width = kMaxHeight* ratio;
}
Imgproc.resize(image, image, new Size(width, height));
}
public void doOpenCvMagic() {
System.loadLibrary("opencv_java");
System.loadLibrary("nonfree");
Mat img1 = Highgui.imread(mCurrentPhotoPath1, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Mat img2 = Highgui.imread(mCurrentPhotoPath2, Highgui.CV_LOAD_IMAGE_GRAYSCALE);
resizeMatToVga(img1);
resizeMatToVga(img2);
int minHessian = 400;
FeatureDetector detector = FeatureDetector.create(FeatureDetector.SURF);
MatOfKeyPoint keypoints1 = new MatOfKeyPoint();
MatOfKeyPoint keypoints2 = new MatOfKeyPoint();
detector.detect(img1, keypoints1);
detector.detect(img2, keypoints2);
Mat descriptors1 = new Mat();
Mat descriptors2 = new Mat();
DescriptorExtractor extractor = DescriptorExtractor.create(DescriptorExtractor.SURF);
extractor.compute(img1, keypoints1, descriptors1);
extractor.compute(img2, keypoints2, descriptors2);
MatOfDMatch matches = new MatOfDMatch();
DescriptorMatcher matcher = DescriptorMatcher.create(DescriptorMatcher.FLANNBASED);
matcher.match(descriptors1, descriptors2, matches);
double max_dist = 0;
double min_dist = 100;
DMatch[] aMatches = matches.toArray();
for (int i=0;i<aMatches.length;++i) {
double dist = aMatches[i].distance;
if (dist < min_dist) {
min_dist = dist;
}
if (dist > max_dist) {
max_dist = dist;
}
}
Vector<DMatch> vgood_matches = new Vector<DMatch>();
for (int i=0;i<aMatches.length;++i) {
if (aMatches[i].distance < 3*min_dist) {
vgood_matches.add(aMatches[i]);
}
}
MatOfDMatch good_matches = new MatOfDMatch();
good_matches.fromList(vgood_matches);
Mat image_matches = new Mat();
Features2d.drawMatches(img1, keypoints1, img2, keypoints2, good_matches, image_matches, Scalar.all(-1), Scalar.all(-1), new MatOfByte(), Features2d.NOT_DRAW_SINGLE_POINTS);
Bitmap bitmap = Bitmap.createBitmap(image_matches.cols(), image_matches.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(image_matches, bitmap);
previewImage1.setImageBitmap(bitmap);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
......@@ -51,6 +156,7 @@ public class CameraActivity extends ActionBarActivity {
public void onClick(View v) {
previewImage = previewImage1;
takePhoto(takeButton);
mCurrentPhotoPath1 = mCurrentPhotoPath;
}
});
......@@ -61,6 +167,7 @@ public class CameraActivity extends ActionBarActivity {
public void onClick(View v) {
previewImage = previewImage2;
takePhoto(takeButton);
mCurrentPhotoPath2 = mCurrentPhotoPath;
}
});
......@@ -69,7 +176,9 @@ public class CameraActivity extends ActionBarActivity {
@Override
public void onClick(View v) {
// do opencv magic here...
if (mCurrentPhotoPath1 != null && mCurrentPhotoPath2 != null) {
doOpenCvMagic();
}
}
});
......
No preview for this file type
No preview for this file type
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