简体   繁体   中英

OpenCV with Android Template Matching

I am trying to develop a template matching android application, I am using the Camera API preview; how can I get the template image (crop it) and save it ? do I need a database to save template images ? or just save them to a specific folder?

You can follow this way, it works:

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM