image.py 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. # This Python file uses the following encoding: utf-8
  2. # @author runhey
  3. # github https://github.com/runhey
  4. import cv2
  5. import numpy as np
  6. from numpy import float32, int32, uint8, fromfile
  7. from pathlib import Path
  8. from module.atom.RuleImageMallResourceMixin import RuleImageMallResourceMixin
  9. from module.base.decorator import cached_property
  10. from module.logger import logger
  11. from module.base.utils import is_approx_rectangle
  12. from module.base.utils.utils import random_normal_distribution_int
  13. class RuleImage(RuleImageMallResourceMixin):
  14. debug_mode: bool = False
  15. def __init__(self, roi_front: tuple, roi_back: tuple, method: str, threshold: float, file: str) -> None:
  16. """
  17. 初始化
  18. :param roi_front: 前置roi
  19. :param roi_back: 后置roi 用于匹配的区域
  20. :param method: 匹配方法 "Template matching"
  21. :param threshold: 阈值 0.8
  22. :param file: 相对路径, 带后缀
  23. """
  24. self._match_init = False # 这个是给后面的 等待图片稳定
  25. self._image = None # 这个是匹配的目标
  26. self._kp = None #
  27. self._des = None
  28. self.method = method
  29. self.roi_front: list = list(roi_front)
  30. self.roi_back = roi_back
  31. self.threshold = threshold
  32. self.file = file
  33. @cached_property
  34. def name(self) -> str:
  35. """
  36. :return:
  37. """
  38. return Path(self.file).stem.upper()
  39. def __str__(self):
  40. return self.name
  41. __repr__ = __str__
  42. def __eq__(self, other):
  43. return str(self) == str(other)
  44. def __hash__(self):
  45. return hash(self.name)
  46. def __bool__(self):
  47. return True
  48. def load_image(self) -> None:
  49. """
  50. 加载图片
  51. :return:
  52. """
  53. if self._image is not None:
  54. return
  55. img = cv2.imdecode(fromfile(self.file, dtype=uint8), -1)
  56. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  57. self._image = img
  58. height, width, channels = self._image.shape
  59. if height != self.roi_front[3] or width != self.roi_front[2]:
  60. self.roi_front[2] = width
  61. self.roi_front[3] = height
  62. logger.debug(f"{self.name} roi_front size changed to {width}x{height}")
  63. def load_kp_des(self) -> None:
  64. if self._kp is not None and self._des is not None:
  65. return
  66. self._kp, self._des = self.sift.detectAndCompute(self.image, None)
  67. @property
  68. def image(self):
  69. """
  70. 获取图片
  71. :return:
  72. """
  73. if self._image is None:
  74. self.load_image()
  75. return self._image
  76. @cached_property
  77. def is_template_match(self) -> bool:
  78. """
  79. 是否是模板匹配
  80. :return:
  81. """
  82. return self.method == "Template matching"
  83. @cached_property
  84. def is_sift_flann(self) -> bool:
  85. return self.method == "Sift Flann"
  86. @cached_property
  87. def sift(self):
  88. return cv2.SIFT_create()
  89. @cached_property
  90. def kp(self):
  91. if self._kp is None:
  92. self.load_kp_des()
  93. return self._kp
  94. @cached_property
  95. def des(self):
  96. if self._des is None:
  97. self.load_kp_des()
  98. return self._des
  99. def corp(self, image: np.array, roi: list = None) -> np.array:
  100. """
  101. 截取图片
  102. :param image:
  103. :param roi
  104. :return:
  105. """
  106. if roi is None:
  107. x, y, w, h = self.roi_back
  108. else:
  109. x, y, w, h = roi
  110. x, y, w, h = int(x), int(y), int(w), int(h)
  111. return image[y:y + h, x:x + w]
  112. def match(self, image: np.array, threshold: float = None) -> bool:
  113. """
  114. :param threshold:
  115. :param image:
  116. :return:
  117. """
  118. if threshold is None:
  119. threshold = self.threshold
  120. if not self.is_template_match:
  121. return self.sift_match(image)
  122. # raise Exception(f"unknown method {self.method}")
  123. source = self.corp(image)
  124. mat = self.image
  125. if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
  126. logger.error(f"Template image is invalid: {mat.shape}")
  127. return False # 模板无效, 匹配失败
  128. if mat.shape[0] > source.shape[0] or mat.shape[1] > source.shape[1]:
  129. # 模板大于源图, 视为无效匹配(避免 matchTemplate 的异常行为)
  130. return False
  131. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  132. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
  133. if self.debug_mode:
  134. logger.attr(self.name, f'matching score {max_val:.5f}')
  135. if max_val > threshold:
  136. self.roi_front[0] = max_loc[0] + self.roi_back[0]
  137. self.roi_front[1] = max_loc[1] + self.roi_back[1]
  138. return True
  139. else:
  140. return False
  141. def match_multi_scale(self, image: np.array, threshold: float = None,
  142. scales: list = None, scale_range: tuple = None) -> bool:
  143. """
  144. 多尺度模板匹配,自动尝试多个缩放比例以适应图片大小的变化
  145. :param image: 原始截图
  146. :param threshold: 匹配阈值
  147. :param scales: 缩放比例列表
  148. :param scale_range: 缩放范围 (start, end, step),例如 (0.8, 1.2, 0.1),step 默认为 0.1
  149. :return: 匹配是否成功
  150. """
  151. if threshold is None:
  152. threshold = self.threshold
  153. # 如果指定了 scale_range,自动生成 scales 列表
  154. if scale_range is not None:
  155. start, end = scale_range[:2]
  156. step = scale_range[2] if len(scale_range) > 2 else 0.1
  157. scales = sorted(set(round(x, 1) for x in np.arange(start, end + step, step)))
  158. if scales is None:
  159. scales = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
  160. else:
  161. scales = sorted(set(round(x, 1) for x in scales))
  162. source = self.corp(image)
  163. mat = self.image
  164. if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
  165. logger.error(f"Template image is invalid: {mat.shape}")
  166. return False
  167. # 预计算模板尺寸
  168. mat_h, mat_w = mat.shape[:2]
  169. source_h, source_w = source.shape[:2]
  170. best_score = 0
  171. best_loc = None
  172. best_scale = 1.0
  173. for scale in scales:
  174. scaled_w = int(mat_w * scale)
  175. scaled_h = int(mat_h * scale)
  176. # 跳过无效缩放
  177. if scaled_w < 10 or scaled_h < 10:
  178. continue
  179. # 缩放后仍大于源图则跳过该尺度(避免 matchTemplate 契约问题)
  180. if scaled_w > source_w or scaled_h > source_h:
  181. continue
  182. try:
  183. scaled_mat = cv2.resize(mat, (scaled_w, scaled_h))
  184. res = cv2.matchTemplate(source, scaled_mat, cv2.TM_CCOEFF_NORMED)
  185. _, max_val, _, max_loc = cv2.minMaxLoc(res)
  186. if max_val > best_score:
  187. best_score = max_val
  188. best_loc = max_loc
  189. best_scale = scale
  190. except Exception as e:
  191. continue
  192. if self.debug_mode:
  193. logger.attr(self.name, f'best scale: {best_scale:.2f}, best score: {best_score:.5f}')
  194. if best_score > threshold and best_loc is not None:
  195. self.roi_front[0] = best_loc[0] + self.roi_back[0]
  196. self.roi_front[1] = best_loc[1] + self.roi_back[1]
  197. self.roi_front[2] = scaled_w
  198. self.roi_front[3] = scaled_h
  199. return True
  200. else:
  201. return False
  202. def match_all(self, image: np.array, threshold: float = None, roi: list = None) -> list[tuple]:
  203. """
  204. 区别于match,这个是返回所有的匹配结果
  205. :param roi:
  206. :param image:
  207. :param threshold:
  208. :return:
  209. """
  210. if roi is not None:
  211. self.roi_back = roi
  212. if threshold is None:
  213. threshold = self.threshold
  214. if not self.is_template_match:
  215. raise Exception(f"unknown method {self.method}")
  216. source = self.corp(image)
  217. mat = self.image
  218. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  219. locations = np.where(results >= threshold)
  220. matches = []
  221. for pt in zip(*locations[::-1]): # (x, y) coordinates
  222. score = results[pt[1], pt[0]]
  223. # 得分, x, y, w, h
  224. x = self.roi_back[0] + pt[0]
  225. y = self.roi_back[1] + pt[1]
  226. matches.append((score, x, y, mat.shape[1], mat.shape[0]))
  227. return matches
  228. def match_all_any(self, image: np.array, threshold: float = None, roi: list = None, nms_threshold: float = 0.3) -> list[tuple]:
  229. """
  230. 区别于match,这个是返回所有的匹配结果,去除冗余匹配项(例如:多个框选区域重叠的情况)时使用。
  231. :param roi:
  232. :param image:
  233. :param threshold:
  234. :return:
  235. """
  236. if roi is not None:
  237. self.roi_back = roi
  238. if threshold is None:
  239. threshold = self.threshold
  240. if not self.is_template_match:
  241. raise Exception(f"unknown method {self.method}")
  242. source = self.corp(image)
  243. mat = self.image
  244. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  245. locations = np.where(results >= threshold)
  246. matches = []
  247. for pt in zip(*locations[::-1]): # (x, y) coordinates
  248. score = results[pt[1], pt[0]]
  249. # 得分, x, y, w, h
  250. x = self.roi_back[0] + pt[0]
  251. y = self.roi_back[1] + pt[1]
  252. matches.append((score, x, y, mat.shape[1], mat.shape[0]))
  253. if len(matches) > 0:
  254. scores = np.array([m[0] for m in matches])
  255. boxes = np.array([[m[1], m[2], m[3], m[4]] for m in matches])
  256. # 使用OpenCV的NMSBoxes
  257. indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), score_threshold=threshold, nms_threshold=nms_threshold)
  258. filtered_matches = [matches[i] for i in indices]
  259. return filtered_matches
  260. return matches
  261. def coord(self) -> tuple:
  262. """
  263. 获取roi_front的随机的点击的坐标
  264. :return:
  265. """
  266. x, y, w, h = self.roi_front
  267. return x + random_normal_distribution_int(0, w), y + random_normal_distribution_int(0, h)
  268. def coord_more(self) -> tuple:
  269. """
  270. 获取roi_back的随机的点击的坐标
  271. :return:
  272. """
  273. x, y, w, h = self.roi_back
  274. return x + random_normal_distribution_int(0, w), y + random_normal_distribution_int(0, h)
  275. def front_center(self) -> tuple:
  276. """
  277. 获取roi_front的中心坐标
  278. :return:
  279. """
  280. x, y, w, h = self.roi_front
  281. return int(x + w//2), int(y + h//2)
  282. def test_match(self, image: np.array):
  283. self.debug_mode = True
  284. if self.is_template_match:
  285. return self.match(image)
  286. if self.is_sift_flann:
  287. return self.sift_match(image, show=True)
  288. def sift_match(self, image: np.array, show=False) -> bool:
  289. """
  290. 特征匹配,同样会修改 roi_front
  291. :param image: 是游戏的截图,就是转通道后的截图
  292. :param show: 测试用的
  293. :return:
  294. """
  295. source = self.corp(image)
  296. kp, des = self.sift.detectAndCompute(source, None)
  297. # 参数1:index_params
  298. # 对于SIFT和SURF,可以传入参数index_params=dict(algorithm=FLANN_INDEX_KDTREE, trees=5)。
  299. # 对于ORB,可以传入参数index_params=dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12)。
  300. index_params = dict(algorithm=1, trees=5)
  301. # 参数2:search_params 指定递归遍历的次数,值越高结果越准确,但是消耗的时间也越多。
  302. search_params = dict(checks=50)
  303. # 根据设置的参数创建特征匹配器 指定匹配的算法和kd树的层数,指定返回的个数
  304. flann = cv2.FlannBasedMatcher(index_params, search_params)
  305. # 利用创建好的特征匹配器利用k近邻算法来用模板的特征描述符去匹配图像的特征描述符,k指的是返回前k个最匹配的特征区域
  306. # 返回的是最匹配的两个特征点的信息,返回的类型是一个列表,列表元素的类型是Dmatch数据类型,具体是什么我也不知道
  307. # 第一个参数是小图的des, 第二个参数是大图的des
  308. matches = flann.knnMatch(self.des, des, k=2)
  309. good = []
  310. result = True
  311. for i, (m, n) in enumerate(matches):
  312. # 设定阈值, 距离小于对方的距离的0.7倍我们认为是好的匹配点.
  313. if m.distance < 0.6 * n.distance:
  314. good.append(m)
  315. if len(good) >= 10:
  316. src_pts = float32([self.kp[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
  317. dst_pts = float32([kp[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
  318. # 计算透视变换矩阵m, 要求点的数量>=4
  319. m, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  320. # 创建一个包含模板图像四个角坐标的数组
  321. w, h = self.roi_front[2], self.roi_front[3]
  322. pts = float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
  323. if m is None:
  324. result = False
  325. else:
  326. dst = int32(cv2.perspectiveTransform(pts, m))
  327. self.roi_front[0] = dst[0, 0, 0] + self.roi_back[0]
  328. self.roi_front[1] = dst[0, 0, 1] + self.roi_back[1]
  329. if show:
  330. cv2.polylines(source, [dst], isClosed=True, color=(0, 0, 255), thickness=2)
  331. if not is_approx_rectangle(np.array([pos[0] for pos in dst])):
  332. result = False
  333. else:
  334. result = False
  335. # https://blog.csdn.net/cungudafa/article/details/105399278
  336. # https://blog.csdn.net/qq_45832961/article/details/122776322
  337. if show:
  338. # 准备一个空的掩膜来绘制好的匹配
  339. mask_matches = [[0, 0] for i in range(len(matches))]
  340. # 向掩膜中添加数据
  341. for i, (m, n) in enumerate(matches):
  342. if m.distance < 0.6 * n.distance: # 理论上0.7最好
  343. mask_matches[i] = [1, 0]
  344. img_matches = cv2.drawMatchesKnn(self.image, self.kp, source, kp, matches, None,
  345. matchColor=(0, 255, 0), singlePointColor=(255, 0, 0),
  346. matchesMask=mask_matches, flags=0)
  347. cv2.imshow(f'Sift Flann: {self.name}', img_matches)
  348. cv2.waitKey(0)
  349. cv2.destroyAllWindows()
  350. return result
  351. def match_mean_color(self, image, color: tuple, bias=10) -> bool:
  352. """
  353. :param image:
  354. :param color: rgb
  355. :param bias:
  356. :return:
  357. """
  358. image = self.corp(image)
  359. average_color = cv2.mean(image)
  360. if self.debug_mode:
  361. logger.info(f'{self.name} average_color: {average_color}')
  362. for i in range(3):
  363. if abs(average_color[i] - color[i]) > bias:
  364. return False
  365. return True
  366. def match_brightness(
  367. self, image: np.array,
  368. threshold: float = 0.9,
  369. roi: list = None,
  370. gray: bool = False) -> bool:
  371. source = self.corp(image, roi) if roi is not None else self.corp(image)
  372. template = self.image
  373. if len(source.shape) != 3:
  374. raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
  375. source_gray = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY)
  376. source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
  377. if len(template.shape) != 3:
  378. raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
  379. template_gray = cv2.cvtColor(template, cv2.COLOR_RGB2GRAY)
  380. template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
  381. source_value = float(source_gray.mean()) if gray else float(source_hsv[:, :, 2].mean())
  382. template_value = float(template_gray.mean()) if gray else float(template_hsv[:, :, 2].mean())
  383. score = 1.0 - abs(source_value - template_value) / 255.0
  384. score = max(0.0, min(1.0, score))
  385. if self.debug_mode:
  386. logger.attr(self.name, f'brightness similarity {score:.5f}')
  387. logger.info(f'Template value: {template_value}, Source value: {source_value}')
  388. return score >= threshold
  389. def match_saturation(self, image: np.array, threshold: float = 0.9, roi: list = None) -> bool:
  390. source = self.corp(image, roi) if roi is not None else self.corp(image)
  391. template = self.image
  392. if len(source.shape) != 3:
  393. raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
  394. source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
  395. if len(template.shape) != 3:
  396. raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
  397. template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
  398. source_value = float(source_hsv[:, :, 1].mean())
  399. template_value = float(template_hsv[:, :, 1].mean())
  400. score = 1.0 - abs(source_value - template_value) / 255.0
  401. score = max(0.0, min(1.0, score))
  402. if self.debug_mode:
  403. logger.attr(self.name, f'saturation similarity {score:.5f}')
  404. logger.info(f'Template value: {template_value}, Source value: {source_value}')
  405. return score >= threshold
  406. if __name__ == "__main__":
  407. from dev_tools.assets_test import detect_image
  408. IMAGE_FILE = './log/test/QQ截图20240223151924.png'
  409. from tasks.Restart.assets import RestartAssets
  410. jade = RestartAssets.I_HARVEST_JADE
  411. jade.method = 'Sift Flann'
  412. sign = RestartAssets.I_HARVEST_SIGN
  413. sign.method = 'Sift Flann'
  414. print(jade.roi_front)
  415. detect_image(IMAGE_FILE, jade)
  416. detect_image(IMAGE_FILE, sign)
  417. print(jade.roi_front)