回答編集履歴

1

修正

2016/11/27 02:26

投稿

MasahikoHirata
MasahikoHirata

スコア3747

test CHANGED
@@ -1,10 +1,20 @@
1
1
  ```ここに言語を入力
2
2
 
3
- public void run(Bitmap inFile, Bitmap templateFile, String outFile, int match_method) {
3
+ public class MatchingDemo {
4
4
 
5
- // public Mat run(Bitmap inFile, Bitmap templateFile, String outFile, int match_method) {
5
+ private Mat img;
6
6
 
7
+ private Mat templ;
8
+
9
+ private Mat result;
10
+
7
- // public Bitmap run(Bitmap inFile, Bitmap templateFile, Bitmap mResult, int match_method) {
11
+ public MatchingDemo() {
12
+
13
+
14
+
15
+
16
+
17
+ }
8
18
 
9
19
  // public void run(String inFile, String templateFile, String outFile, int match_method) {
10
20
 
@@ -12,19 +22,71 @@
12
22
 
13
23
 
14
24
 
15
- img = new Mat(inFile.getHeight(), inFile.getWidth(), CvType.CV_8UC1);
25
+ Mat img = Highgui.imread(inFile);
16
26
 
17
- templ = new Mat(templateFile.getHeight(), templateFile.getWidth(), CvType.CV_8UC1);
18
-
19
- Utils.bitmapToMat(inFile, img);
20
-
21
- Utils.bitmapToMat(templateFile, templ);
27
+ Mat templ = Highgui.imread(templateFile);
22
28
 
23
29
 
24
30
 
25
- // Mat img = Highgui.imread(inFile);
26
31
 
32
+
33
+ //結果を作成
34
+
35
+ int result_cols = img.cols() - templ.cols() + 1;
36
+
37
+ int result_rows = img.rows() - templ.rows() + 1;
38
+
39
+ result = new Mat(result_rows, result_cols, CvType.CV_32FC1);
40
+
41
+
42
+
27
- // Mat templ = Highgui.imread(templateFile);
43
+ // / Do the Matching and Normalize
44
+
45
+ Imgproc.matchTemplate(img, templ, result, match_method);
46
+
47
+ Core.normalize(result, result, 0, 1, Core.NORM_MINMAX, -1, new Mat());
48
+
49
+
50
+
51
+ //閾値が最も高いものを選ぶ
52
+
53
+ Core.MinMaxLocResult mmr = Core.minMaxLoc(result);
54
+
55
+ //マッチング時の最大値と最小値を返す
56
+
57
+ Point matchLoc;
58
+
59
+ if (match_method == Imgproc.TM_SQDIFF || match_method == Imgproc.TM_SQDIFF_NORMED) {
60
+
61
+ matchLoc = mmr.minLoc;
62
+
63
+ } else {
64
+
65
+ matchLoc = mmr.maxLoc;
66
+
67
+ }
68
+
69
+
70
+
71
+ //マッチしたところを囲む
72
+
73
+ Core.rectangle(img, matchLoc, new Point(matchLoc.x + templ.cols(),
74
+
75
+ matchLoc.y + templ.rows()), new Scalar(0, 255, 0));
76
+
77
+
78
+
79
+ // 結果を保存
80
+
81
+ System.out.println("Writing "+ outFile);
82
+
83
+ Highgui.imwrite(outFile, img);
84
+
85
+
86
+
87
+ }
88
+
89
+ }
28
90
 
29
91
 
30
92