0

我正在尝试开发一个匹配 android 应用程序的模板,我正在使用 Camera API 预览;如何获取模板图像(裁剪)并保存?我需要一个数据库来保存模板图像吗?或者只是将它们保存到特定文件夹?

4

1 回答 1

1

你可以按照这种方式,它的工作原理:

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);
于 2013-10-18T15:40:56.880 回答