Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public void 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());
- Point matchLoc;
- Point maxLoc;
- Point minLoc;
- MinMaxLocResult mmr;
- boolean iterate = true;
- while(iterate){
- // / Localizing the best match with minMaxLoc
- mmr = Core.minMaxLoc(result);
- matchLoc = mmr.maxLoc;
- if(mmr.maxVal < 0.8)
- {
- iterate = false;
- }
- // / 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);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement