Because of this need, so, the template search for the relevant information, but the algorithm is very difficult to see things, especially opencv involved in a lot of mathematical methods.
So just to achieve this function, because the demand is relatively simple, in the Internet also search for the relevant code, directly to use, here is equivalent to reprint:
Code, the pro-test can be used, the effect is good, indeed the mouth to find out.
Original: http://www.itstrike.cn/Question/645ffff0-2862-46b6-a421-b76a37dfc660.html
Class Matchingdemo {public void Run (string inFile, String templatefile, string outFile, int match_method) {Sys Tem.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 Got Core.rectangle (IMG, Matchloc, new Point (matchloc.x + templ.cols (), Matchloc.y + templ.ro WS ()), new Scalar (0, 255, 0)); Save the visualized detection. System.out.println ("Writing" + outFile); Highgui.imwrite (OutFile, IMG); }}public class Templatematching {public static void main (string[] args) {system.loadlibrary ("opencv_java246"); New Matchingdemo (). Run (Args[0], args[1], args[2], Imgproc.tm_ccoeff); }}
Target Image:
Template Image:
Post-Search Image:
Opencv for Android template matching