问题
I'm trying to make an application which compares a pictures taken from the camera with others stored in the SD card. If I try to compare two images stored in SD card it works fine, but when I try to use the camera it freezes.
That's a part of my code:
private boolean isSame = false;
final int c = 2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (!OpenCVLoader.initDebug()) {
System.out.println("Errore");
}
final Mat[] vector = new Mat[2];
findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
File fotofatte = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/images"+"/taken");
if(!fotofatte.exists()) {
fotofatte.mkdirs();
}
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
File image = new File(fotofatte, "image_001.jpeg");
Uri uriSavedImage = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(imageIntent, 1);
for (int i = 0; i < c; i++) {
final String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
if (baseDir == null) {
throw new IOException();
}
Bitmap bm = BitmapFactory.decodeResource( getResources(), R.drawable.tmp);
Mat img = new Mat(bm.getHeight(), bm.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bm, img);
Bitmap bm2 = BitmapFactory.decodeFile(fotofatte+ "/image_001.jpeg");
Mat templ = new Mat(bm2.getHeight(), bm2.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bm2, templ);
Bitmap bm3 = BitmapFactory.decodeResource( getResources(), R.drawable.dd);
Mat img2 = new Mat(bm3.getHeight(), bm3.getWidth(), CvType.CV_8UC1);
Utils.bitmapToMat(bm3, img2);
vector[0] = img;
vector[1] = img2;
int result_cols = templ.cols() - vector[i].cols() + 1;
int result_rows = templ.rows() - vector[i].rows() + 1;
Mat result = new Mat(result_cols, result_rows, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(templ, vector[i], result, Imgproc.TM_CCOEFF_NORMED);
/* Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1,
new Mat());*/
// / Localizing the best match with minMaxLoc
Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
// Point matchLoc;
/* if (Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF
|| Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
System.out.println(mmr.maxVal);
System.out.println(mmr.minVal);
} else {
matchLoc = mmr.maxLoc;
System.out.println(mmr.maxVal);
System.out.println(mmr.minVal);
}*/
System.out.println(mmr.maxVal);
}
} catch (IOException e) {
System.out.println(e.toString());
}
}
});
}}
I don't receive any error in my log.
Thank you for the help.
UPDATE
Hi guys, I've resized my bitmaps and now I'm able to compare pictures taken from the camera with templates stored in the SD card.
But now I'm facing a new problem. If I make a photo of a glass to use it as template and then I use another photo with the same glass for the comparison, I have only 0.4175666272640228 as result (mmr.MaxValue). How can I fix this?
来源:https://stackoverflow.com/questions/38696938/android-opencv-template-matching-with-camera-picture