回答編集履歴
2
あ
test
CHANGED
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
|
50
50
|
|
51
|
-
|
51
|
+
|
52
52
|
|
53
53
|
|
54
54
|
|
@@ -66,10 +66,4 @@
|
|
66
66
|
|
67
67
|
return img
|
68
68
|
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
imshow(erase_img("a.png", "b.png"))
|
74
|
-
|
75
69
|
```
|
1
修正
test
CHANGED
@@ -16,6 +16,60 @@
|
|
16
16
|
|
17
17
|
|
18
18
|
|
19
|
-
それはそうとして、
|
19
|
+
それはそうとして、`if num == 1`、つまり、類似度が厳密に1になるというのは、テンプレート画像と全く同じ画素を持つ領域が検索対象の画像にある場合ですので、まずこの条件には引っかからないでしょう。
|
20
20
|
|
21
|
+
`if maxVal >= 0.8:` のように条件をゆるめてみてはどうでしょうか
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
## 修正後のコード
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
```python
|
30
|
+
|
31
|
+
import cv2
|
32
|
+
|
33
|
+
|
34
|
+
|
35
|
+
def erase_img(img_path, templ_path):
|
36
|
+
|
37
|
+
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
|
38
|
+
|
39
|
+
templ = cv2.imread(templ_path, cv2.IMREAD_GRAYSCALE)
|
40
|
+
|
41
|
+
|
42
|
+
|
21
|
-
|
43
|
+
result = cv2.matchTemplate(img, templ, cv2.TM_CCORR_NORMED)
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
# 最も類似度が高い位置と低い位置を取得
|
48
|
+
|
49
|
+
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(result)
|
50
|
+
|
51
|
+
print(result)
|
52
|
+
|
53
|
+
|
54
|
+
|
55
|
+
if maxVal >= 0.8:
|
56
|
+
|
57
|
+
img[
|
58
|
+
|
59
|
+
maxLoc[1] : maxLoc[1] + templ.shape[0],
|
60
|
+
|
61
|
+
maxLoc[0] : maxLoc[0] + templ.shape[1],
|
62
|
+
|
63
|
+
] = 255
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
return img
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
imshow(erase_img("a.png", "b.png"))
|
74
|
+
|
75
|
+
```
|