我正在尝试使用opencv的matchTemplate函数比较两个面。我有以下代码:
Mat img1 = new Mat();
Utils.bitmapToMat(bmpimg1, img1);
Mat img2 = new Mat();
Utils.bitmapToMat(bmpimg2, img2);
Mat img = new Mat();
Utils.bitmapToMat(bmpimg1, img);
Mat templ = new Mat();
Utils.bitmapToMat(bmpimg2, templ);
int match_method = Imgproc.TM_CCOEFF;
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
// / Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, match_method);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
// / Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
org.opencv.core.Point matchLoc;
if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
// / Show me what you got
Core.rectangle(img, matchLoc, new org.opencv.core.Point(matchLoc.x + templ.cols(),
matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
Toast.makeText(getApplicationContext(), "(" + matchLoc.x + templ.cols() + "," + matchLoc.y + templ.rows() + ")", Toast.LENGTH_LONG).show();
Bitmap bitmap = Bitmap.createBitmap(img.cols(), img.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(img, bitmap);
Toast.makeText(getApplicationContext(), " "+saveToInternalStorage(bitmap),Toast.LENGTH_LONG).show();
但即使两个图像完全不同,此代码也会绘制矩形。为什么?如何在两个图像匹配时才能绘制矩形?任何建议都有很大帮助。