質問編集履歴
1
コードの追加
test
CHANGED
File without changes
|
test
CHANGED
@@ -18,9 +18,15 @@
|
|
18
18
|
|
19
19
|
テンプレートマッチングで実装出来そうなのですが、参考に出来るようなサンプルプログラムが見つからず、お手上げの状態です。
|
20
20
|
|
21
|
+
また、テンプレートマッチングだと認識できる位置が、テンプレート画像内での位置になってしまうかと思うのですがいかがでしょうか。。。
|
21
22
|
|
22
23
|
|
24
|
+
|
25
|
+
現状でのコードを一応貼って置きます。画像によるテンプレートマッチングです。
|
26
|
+
|
27
|
+
|
28
|
+
|
23
|
-
一歩でも動
|
29
|
+
一歩でも動き始められるようなヒントでも良いので、どなたか助言を下されば幸いです。
|
24
30
|
|
25
31
|
|
26
32
|
|
@@ -28,8 +34,104 @@
|
|
28
34
|
|
29
35
|
|
30
36
|
|
31
|
-
##
|
37
|
+
##現状でのコード
|
32
38
|
|
33
|
-
|
39
|
+
```Processing
|
34
40
|
|
41
|
+
import processing.video.*;
|
42
|
+
|
43
|
+
import gab.opencv.*;
|
44
|
+
|
45
|
+
import org.opencv.core.Mat;
|
46
|
+
|
47
|
+
import org.opencv.core.CvType;
|
48
|
+
|
49
|
+
import org.opencv.imgproc.Imgproc;
|
50
|
+
|
51
|
+
import org.opencv.core.Core.MinMaxLocResult;
|
52
|
+
|
53
|
+
import org.opencv.core.Core;
|
54
|
+
|
55
|
+
|
56
|
+
|
57
|
+
//Capture Camera; //本当はこれで読み込むビデオを入力画像として使いたい
|
58
|
+
|
59
|
+
|
60
|
+
|
61
|
+
// 入力画像の準備
|
62
|
+
|
63
|
+
PImage inputImage = loadImage("tiisakusita1.jpg");
|
64
|
+
|
65
|
+
OpenCV inputCV = new OpenCV(this, inputImage);
|
66
|
+
|
67
|
+
Mat inputMat = OpenCV.imitate(inputCV.getGray());
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
// テンプレート画像の準備
|
72
|
+
|
73
|
+
PImage templateImage = loadImage("tiisakusita2.jpg");
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
OpenCV templateCV = new OpenCV(this, templateImage);
|
78
|
+
|
79
|
+
Mat templateMat = OpenCV.imitate(templateCV.getGray());
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// 結果格納用の行列の準備
|
84
|
+
|
85
|
+
int resultCols = inputMat.cols() - templateMat.cols() + 1;
|
86
|
+
|
87
|
+
int resultRows = inputMat.rows() - templateMat.rows() + 1;
|
88
|
+
|
89
|
+
Mat resultMat = new Mat(resultRows, resultCols, CvType.CV_32FC1);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
// テンプレートマッチングを実行
|
94
|
+
|
95
|
+
Imgproc.matchTemplate(inputCV.getColor(), templateCV.getColor(), resultMat, Imgproc.TM_CCOEFF_NORMED);
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// 結果を描画
|
100
|
+
|
101
|
+
println("OK!");
|
102
|
+
|
103
|
+
size(400, 300);
|
104
|
+
|
105
|
+
image(inputImage, 100, 0);
|
106
|
+
|
107
|
+
image(templateImage, 10, 10,80,80);
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
MinMaxLocResult mmlr = Core.minMaxLoc(resultMat);
|
112
|
+
|
113
|
+
|
114
|
+
|
115
|
+
if (mmlr.maxVal > 0.1) {
|
116
|
+
|
117
|
+
println("Val: " + mmlr.maxVal);
|
118
|
+
|
119
|
+
stroke(255, 0, 0);
|
120
|
+
|
121
|
+
strokeWeight(3);
|
122
|
+
|
123
|
+
noFill();
|
124
|
+
|
125
|
+
rect((int)mmlr.maxLoc.x + 100, (int)mmlr.maxLoc.y, templateMat.cols(), templateMat.rows());
|
126
|
+
|
127
|
+
}
|
128
|
+
|
129
|
+
```
|
130
|
+
|
131
|
+
|
132
|
+
|
133
|
+
###参考動画
|
134
|
+
|
135
|
+
[1](https://youtu.be/xbvQ2Z2OeRU)
|
136
|
+
|
35
|
-
https://youtu.be/GgGKUKbdb0s
|
137
|
+
[2](https://youtu.be/GgGKUKbdb0s)
|