AndroidstudioでOpenCVを使いテンプレートマッチングを行いたいと考えています。サンプルが無いかと探した所下記のコードを発見しました。
inFIleはマッチングする画像データ、templateFileはテンプレート用画像データだと思いますがoutFileは結果出力用のものでしょうか?
###該当のソースコード
java
1package opencv; 2 3import org.opencv.core.Core; 4import org.opencv.core.Core.MinMaxLocResult; 5import org.opencv.core.CvType; 6import org.opencv.core.Mat; 7import org.opencv.core.Point; 8import org.opencv.core.Scalar; 9import org.opencv.highgui.Highgui; 10import org.opencv.imgproc.Imgproc; 11 12class MatchingDemo { 13 public void run(String inFile, String templateFile, String outFile, int match_method) { 14 System.out.println("\nRunning Template Matching"); 15 16 Mat img = Highgui.imread(inFile); 17 Mat templ = Highgui.imread(templateFile); 18 19 // / Create the result matrix 20 int result_cols = img.cols() - templ.cols() + 1; 21 int result_rows = img.rows() - templ.rows() + 1; 22 Mat result = new Mat(result_rows, result_cols, CvType.CV_32FC1); 23 24 // / Do the Matching and Normalize 25 Imgproc.matchTemplate(img, templ, result, match_method); 26 Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat()); 27 28 // / Localizing the best match with minMaxLoc 29 MinMaxLocResult mmr = Core.minMaxLoc(result); 30 31 Point matchLoc; 32 if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) { 33 matchLoc = mmr.minLoc; 34 } else { 35 matchLoc = mmr.maxLoc; 36 } 37 38 // / Show me what you got 39 Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(), 40 matchLoc.y + templ.rows()), new Scalar(0, 255, 0)); 41 42 // Save the visualized detection. 43 System.out.println("Writing "+ outFile); 44 Highgui.imwrite(outFile, img); 45 46 } 47} 48 49public class TemplateMatching { 50 public static void main(String[] args) { 51 System.loadLibrary("opencv_java246"); 52 new MatchingDemo().run(args[0], args[1], args[2], Imgproc.TM_CCOEFF); 53 } 54}
回答1件
あなたの回答
tips
プレビュー