我正在尝试使用模板匹配在Android中与照相机输入匹配。 当我尝试使用 static 2图像时,如下所示: 在安卓的OpenCV模板匹配示例中,所有的工作都很好。 但是当我尝试使用相机拍摄的图像时,我没有得到正确的结果。 下面是我编写的代码:String baseDir = Environment.getExternalStorageDirectory().getAbsolutePath();
Mat img = Highgui.imread(baseDir +"/mediaAppPhotos/img2.png");
Mat templ = Highgui.imread(baseDir+"/mediaAppPhotos/chars.png");
int result_cols = img.cols() - templ.cols() + 1;
int result_rows = img.rows() - templ.rows() + 1;
Mat result = new Mat(result_cols, result_rows, CvType.CV_32FC1);
///Do the Matching and Normalize
Imgproc.matchTemplate(img, templ, result, Imgproc.TM_CCOEFF);
Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1,
new Mat());
///Localizing the best match with minMaxLoc
MinMaxLocResult mmr = Core.minMaxLoc(result);
Point matchLoc;
if (Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF
|| Imgproc.TM_CCOEFF == Imgproc.TM_SQDIFF_NORMED) {
matchLoc = mmr.minLoc;
} else {
matchLoc = mmr.maxLoc;
}
///Show me what you got
Core.rectangle(
img,
matchLoc,
new Point(matchLoc.x + templ.cols(), matchLoc.y
+ templ.rows()), new Scalar(0, 255, 0));
//Save the visualized detection.
System.out.println("Writing" + baseDir+"/mediaAppPhotos/result.png");
Highgui.imwrite(baseDir +"/mediaAppPhotos/result.png", img);
我想要这样的模板匹配,当图像从摄像机捕获时。 我们感激任何方式的协助。