Guest User

Untitled

a guest
Apr 27th, 2015
285
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import org.opencv.core.Core;
  2. import org.opencv.core.Core.MinMaxLocResult;
  3. import org.opencv.core.CvType;
  4. import org.opencv.core.Mat;
  5. import org.opencv.core.Point;
  6. import org.opencv.core.Scalar;
  7. import org.opencv.highgui.Highgui;
  8. import org.opencv.imgproc.Imgproc;
  9.  
  10. public class TemplateMatch {
  11.  
  12. public void run(String inFile, String templateFile, String outFile, int match_method) {
  13. System.out.println("\nRunning Template Matching");
  14.  
  15.  
  16.  
  17.  
  18. Mat img = Highgui.imread(inFile);
  19. Mat templ = Highgui.imread(templateFile);
  20.  
  21. // / Create the result matrix
  22. int result_cols = img.cols() - templ.cols() + 1;
  23. int result_rows = img.rows() - templ.rows() + 1;
  24. Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
  25.  
  26. // / Do the Matching and Normalize
  27. Imgproc.matchTemplate(img, templ, result, match_method);
  28. Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
  29.  
  30. Point matchLoc;
  31. Point maxLoc;
  32. Point minLoc;
  33.  
  34. MinMaxLocResult mmr;
  35.  
  36. boolean iterate = true;
  37. do{
  38.  
  39. // / Localizing the best match with minMaxLoc
  40. mmr = Core.minMaxLoc(result);
  41.  
  42. matchLoc = mmr.maxLoc;
  43.  
  44.  
  45. if(mmr.maxVal < 0.8)
  46. {
  47. iterate = false;
  48. }
  49.  
  50.  
  51.  
  52. // / Show me what you got
  53. Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
  54. matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
  55.  
  56. }while(mmr.maxVal > 0.9);
  57.  
  58. // Save the visualized detection.
  59. System.out.println("Writing "+ outFile);
  60. Highgui.imwrite(outFile, img);
  61.  
  62. }
  63.  
  64.  
  65.  
  66. public static void main(String[] args) {
  67. System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  68. new TemplateMatch().run("lena.png", "template.png", "templatematch.png", Imgproc.TM_CCORR_NORMED);
  69. }
  70.  
  71. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×