我正在开发一个 android 应用程序来比较两个静止图像。但是当尝试调试项目时,我在 ActivityThread 中找不到源。这是我的代码
查找器类
package com.example.testmatching;
import java.util.LinkedList;
import java.util.List;
import org.opencv.calib3d.Calib3d;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfByte;
import org.opencv.core.MatOfDMatch;
import org.opencv.core.MatOfKeyPoint;
import org.opencv.core.MatOfPoint2f;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
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.features2d.KeyPoint;
import org.opencv.highgui.Highgui;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import android.widget.RelativeLayout.LayoutParams;
//import org.opencv.core.MatOfPoint;
//import android.view.Menu;
public class Finder extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comparemain);
}
private static final int CV_LOAD_IMAGE_GRAYSCALE = 0;
private static final String TAG = "OCV::Activity";
//@Override
/*public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.comparemain, menu);
return true;
}*/
public void run(String pathObject, String pathScene, String pathResult) {
System.out.println("\nRunning FindObject");
Mat img_object = new Mat();
Utils.bitmapToMat(theImage, theImageMat ); // converts some Bitmap to some Mat*/
Mat img_object = Highgui.imread("C:/work/OpenCV4Android/".concat(pathObject), CV_LOAD_IMAGE_GRAYSCALE); //0 = CV_LOAD_IMAGE_GRAYSCALE
Mat img_scene = Highgui.imread("C:/work/OpenCV4Android/".concat(pathScene), CV_LOAD_IMAGE_GRAYSCALE);
if( (img_object.empty()) || (img_scene.empty()) )
{ Log.d(TAG,"failing to read images");
//System.out.println("images non lues");
return;}
FeatureDetector detector = FeatureDetector.create(4); //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(2); //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(1); // 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 < descriptor_object.rows(); 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 < descriptor_object.rows(); 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);
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);
//findHomography sert à trouver la transformation entre les good matches
Mat hg = 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()});
//obj_corners:input
Core.perspectiveTransform(obj_corners,scene_corners, hg);
Core.line(img_matches, new Point(scene_corners.get(0,0)), new Point(scene_corners.get(1,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(1,0)), new Point(scene_corners.get(2,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(2,0)), new Point(scene_corners.get(3,0)), new Scalar(0, 255, 0),4);
Core.line(img_matches, new Point(scene_corners.get(3,0)), new Point(scene_corners.get(0,0)), new Scalar(0, 255, 0),4);
//Sauvegarde du résultat
System.out.println(String.format("Writing %s", pathResult));
Boolean bool=false;
Highgui.imwrite(pathResult, img_matches);
if (bool == true)
Log.d(TAG, "SUCCESS writing image to external storage");
else
Log.d(TAG, "Fail writing image to external storage");
ImageView imageView = new ImageView(getApplicationContext());
//ImageView jpgView = (ImageView)findViewById(R.id.ImageView01);
LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
/**/Bitmap image = BitmapFactory.decodeFile(pathResult);
imageView.setImageBitmap(image);
//RelativeLayout rl = (RelativeLayout) findViewById(R.id.);
addContentView(imageView, lp);
}
}
主要活动
package com.example.testmatching;
import com.example.testmatching.Finder;
public class COMPAREMainActivity {
public static Finder fn=new Finder();
public static void main(String[] args) {
// System.loadLibrary("opencv_java246");
fn.run("input1.png", "input2.png","resultat.png");
}
}
我在那行放了一个断点
Mat img_object = Highgui.imread("C:/work/OpenCV4Android/".concat(pathObject), CV_LOAD_IMAGE_GRAYSCALE);
我想测试从该文件夹读取图像的操作是否有效