Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.opencv_templatematching;
- import org.opencv.android.Utils;
- import org.opencv.core.Core;
- import org.opencv.core.Core.MinMaxLocResult;
- import org.opencv.core.CvType;
- import org.opencv.core.Mat;
- import org.opencv.core.Point;
- import org.opencv.core.Scalar;
- import org.opencv.highgui.Highgui;
- import org.opencv.imgproc.Imgproc;
- import android.graphics.Bitmap;
- class MatchingDemo
- {
- public Mat run(String inFile, String templateFile, String outFile, int match_method) {
- System.out.println("\nRunning Template Matching");
- Mat img = Highgui.imread(inFile);
- Mat templ = Highgui.imread(templateFile);
- // / Create the result matrix
- 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);
- 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 Point(matchLoc.x + templ.cols(),
- matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
- // Save the visualized detection.
- System.out.println("Writing "+ outFile);
- Highgui.imwrite(outFile, img);
- return img;
- }
- }
- public class TemplateMatching {
- public Bitmap main(String[] args) {
- String infile = args[0].toString();
- String templatefile = args[1].toString();
- String outfile = args[2].toString();
- Mat image = new MatchingDemo().run(infile, templatefile, outfile, Imgproc.TM_CCOEFF);
- Bitmap bitmap = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
- Utils.matToBitmap(image, bitmap);
- return bitmap;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement