SHARE
TWEET

Template Matching - TemplateMatching

Mytheral Feb 19th, 2015 (edited) 98 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.opencv_templatematching;
  2.  
  3. import org.opencv.android.Utils;
  4. import org.opencv.core.Core;
  5. import org.opencv.core.Core.MinMaxLocResult;
  6. import org.opencv.core.CvType;
  7. import org.opencv.core.Mat;
  8. import org.opencv.core.Point;
  9. import org.opencv.core.Scalar;
  10. import org.opencv.highgui.Highgui;
  11. import org.opencv.imgproc.Imgproc;
  12.  
  13. import android.graphics.Bitmap;
  14.  
  15. class MatchingDemo
  16. {
  17.     public Mat run(String inFile, String templateFile, String outFile, int match_method) {
  18.         System.out.println("\nRunning Template Matching");
  19.  
  20.         Mat img = Highgui.imread(inFile);
  21.         Mat templ = Highgui.imread(templateFile);
  22.  
  23.         // / Create the result matrix
  24.         int result_cols = img.cols() - templ.cols() + 1;
  25.         int result_rows = img.rows() - templ.rows() + 1;
  26.         Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
  27.  
  28.         // / Do the Matching and Normalize
  29.         Imgproc.matchTemplate(img, templ, result, match_method);
  30.         Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
  31.  
  32.         // / Localizing the best match with minMaxLoc
  33.         MinMaxLocResult mmr = Core.minMaxLoc(result);
  34.  
  35.         Point matchLoc;
  36.         if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
  37.             matchLoc = mmr.minLoc;
  38.         } else {
  39.             matchLoc = mmr.maxLoc;
  40.         }
  41.  
  42.         // / Show me what you got
  43.         Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
  44.                 matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
  45.  
  46.         // Save the visualized detection.
  47.         System.out.println("Writing "+ outFile);
  48.         Highgui.imwrite(outFile, img);
  49.        
  50.         return img;
  51.     }
  52. }
  53.  
  54. public class TemplateMatching {
  55.     public Bitmap main(String[] args) {
  56.        
  57.         String infile = args[0].toString();
  58.         String templatefile  = args[1].toString();
  59.         String outfile = args[2].toString();
  60.        
  61.          Mat image = new MatchingDemo().run(infile, templatefile, outfile, Imgproc.TM_CCOEFF);
  62.          
  63.          Bitmap bitmap = Bitmap.createBitmap(image.cols(), image.rows(), Bitmap.Config.ARGB_8888);
  64.          Utils.matToBitmap(image, bitmap);
  65.          
  66.          return bitmap;
  67.     }
  68. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top