【发布时间】:2013-10-27 12:43:13
【问题描述】:
我正在尝试使用模板匹配将图像与 Android 中的相机输入进行匹配。当我在这里尝试使用静态 2 图像时:OpenCV Template Matching example in Android,一切正常。但是当我尝试使用从相机捕获的图像时,我没有得到正确的结果。以下是我写的代码:
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);
我希望此模板匹配在从相机捕获图像时也能正常工作。非常感谢任何帮助!
【问题讨论】:
-
如果您能描述一下相机图像结果的问题,将会有所帮助。
-
如果我将源图像的一部分切掉并用作模板图像进行匹配,那么它就可以正常工作。但是如果我使用相同的模板来匹配相同的源图像,捕捉到轻微的高度或位置变化,那么它就不起作用了!
-
确保您的模板小于您匹配的图像。
-
好点,可能是内存问题。日志中有什么有用的吗?
-
@don_q : 你能在this question 上提供一些建议吗?
标签: android opencv android-camera mat template-matching