Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.53 KB | None | 0 0
  1. public class TemplateMatch {
  2.  
  3. public void run(String inFile, String templateFile, String outFile, int match_method) {
  4. System.out.println("\nRunning Template Matching");
  5.  
  6. Mat img = Highgui.imread(inFile);
  7. Mat templ = Highgui.imread(templateFile);
  8.  
  9. // / Create the result matrix
  10. int result_cols = img.cols() - templ.cols() + 1;
  11. int result_rows = img.rows() - templ.rows() + 1;
  12. Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
  13.  
  14. // / Do the Matching and Normalize
  15. Imgproc.matchTemplate(img, templ, result, match_method);
  16. Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
  17.  
  18. // / Localizing the best match with minMaxLoc
  19. MinMaxLocResult mmr = Core.minMaxLoc(result);
  20.  
  21. Point matchLoc;
  22. if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
  23. matchLoc = mmr.minLoc;
  24. } else {
  25. matchLoc = mmr.maxLoc;
  26. }
  27.  
  28. // / Show me what you got
  29. Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
  30. matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
  31.  
  32. // Save the visualized detection.
  33. System.out.println("Writing "+ outFile);
  34. Highgui.imwrite(outFile, img);
  35.  
  36. }
  37.  
  38.  
  39.  
  40. public static void main(String[] args) {
  41. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  42. new TemplateMatch().run("lena.png", "template.png", "templatematch.png", Imgproc.TM_CCOEFF);
  43. }
  44.  
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement