回答編集履歴

3

fix

2023/05/09 13:35

投稿

退会済みユーザー
test CHANGED
@@ -7,7 +7,7 @@
7
7
  import numpy as np
8
8
 
9
9
 
10
- def nms(rects, scores, overlap):
10
+ def nms(rects, scores, overlapth):
11
11
  if len(rects) <= 1:
12
12
  return rects
13
13
  rects = rects.astype("float")
@@ -31,7 +31,7 @@
31
31
  i_h = np.maximum(0, i_y2 - i_y1 + 1)
32
32
  overlap = (i_w * i_h) / area[remaining_indices]
33
33
  indices = np.delete(
34
- indices, np.concatenate(([last], np.where(overlap > overlap)[0]))
34
+ indices, np.concatenate(([last], np.where(overlap > overlapth)[0]))
35
35
  )
36
36
 
37
37
  return rects[selected].astype("int")

2

nms

2023/05/05 07:55

投稿

退会済みユーザー
test CHANGED
@@ -5,6 +5,38 @@
5
5
  import pyautogui
6
6
  import cv2
7
7
  import numpy as np
8
+
9
+
10
+ def nms(rects, scores, overlap):
11
+ if len(rects) <= 1:
12
+ return rects
13
+ rects = rects.astype("float")
14
+ x1, y1, x2, y2 = np.squeeze(np.split(rects, 4, axis=1))
15
+
16
+ area = (x2 - x1 + 1) * (y2 - y1 + 1)
17
+ indices = np.argsort(scores)
18
+ selected = []
19
+
20
+ while len(indices) > 0:
21
+ last = len(indices) - 1
22
+ selected_index = indices[last]
23
+ remaining_indices = indices[:last]
24
+ selected.append(selected_index)
25
+ i_x1 = np.maximum(x1[selected_index], x1[remaining_indices])
26
+ i_y1 = np.maximum(y1[selected_index], y1[remaining_indices])
27
+ i_x2 = np.minimum(x2[selected_index], x2[remaining_indices])
28
+ i_y2 = np.minimum(y2[selected_index], y2[remaining_indices])
29
+
30
+ i_w = np.maximum(0, i_x2 - i_x1 + 1)
31
+ i_h = np.maximum(0, i_y2 - i_y1 + 1)
32
+ overlap = (i_w * i_h) / area[remaining_indices]
33
+ indices = np.delete(
34
+ indices, np.concatenate(([last], np.where(overlap > overlap)[0]))
35
+ )
36
+
37
+ return rects[selected].astype("int")
38
+
39
+
8
40
 
9
41
  # 画面キャプチャーの実行
10
42
  image = pyautogui.screenshot()
@@ -16,35 +48,34 @@
16
48
  img = cv2.imread('screenshot.png')
17
49
 
18
50
  # テンプレート画像のファイルパスをリストで用意
51
+ # テンプレート画像のファイルパスをリストで用意
19
52
  template_file_paths = ['5m.png', '6m.png', '7m.png', '8m.png', '4p.png', '5p.png', '5s.png', '4s.png', '5s.png', '6s.png', '7s.png', '8s.png', '9s.png']
20
-
21
53
 
22
54
  # 各テンプレート画像について処理
23
55
  detected_templates: list[str] = []
56
+
24
57
  for template_file_path in template_file_paths:
25
58
  # テンプレート画像の読み込み
26
59
  template = cv2.imread(template_file_path)
27
60
  mime_data = 0
28
61
  # テンプレートマッチングの実行
29
62
  result = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
30
-
63
+
31
64
  # 最大のマッチング位置を検出
32
65
  threshold = 0.8 # 類似度の閾値
33
66
  locations = np.where(result >= threshold)
67
+ scores = result[locations]
68
+ rects = []
69
+ h, w = template.shape[:2]
34
- locations = list(zip(*locations[::-1]))
70
+ for y, x in zip(*locations):
71
+ rects.append([x, y, x + w - 1, y + h - 1])
72
+ rects = nms(rects, scores, overlap=0.6)
73
+ for x1, y1, x2, y2 in rects:
74
+ cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
75
+ for rect in rects:
76
+ template_fime_name = template_file_path.split('/')[-1]
77
+ detected_templates.append((tuple(rect), template_fime_name[:-4]))
35
78
 
36
- # マッチング位置を描画
37
- for loc in locations:
38
- h, w = template.shape[:2]
39
- top_left = loc
40
- bottom_right = (top_left[0] + w, top_left[1] + h)
41
- cv2.rectangle(img, top_left, bottom_right, (0, 0, 255), 2)
42
- # マッチングした画像のテンプレート画像のファイル名を取得
43
- template_file_name = template_file_path.split('/')[-1] # ファイル名のみ抽出
44
- detected_templates.append((loc, template_file_name[:-4]))
45
-
46
-
47
- # 結果を表示
48
79
  print(''.join([item[1] for item in sorted(detected_templates)]))
49
80
  cv2.imshow('result', img)
50
81
  cv2.waitKey(0)
@@ -52,6 +83,6 @@
52
83
  # ウィンドウを閉じる
53
84
  cv2.destroyAllWindows()
54
85
  ```
55
- 最適化とか細かいとこ気になるとこの修正その他はmelianがやってくれると思う。
86
+ 参考:ttps://pystyle.info/opencv-non-maximum-suppression/
56
87
  AIはつかってません
57
88
 

1

fix

2023/05/03 05:46

投稿

退会済みユーザー
test CHANGED
@@ -45,7 +45,6 @@
45
45
 
46
46
 
47
47
  # 結果を表示
48
- print(detected_templates)
49
48
  print(''.join([item[1] for item in sorted(detected_templates)]))
50
49
  cv2.imshow('result', img)
51
50
  cv2.waitKey(0)