image.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  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, del_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. # 模板匹配容差:把搜索区域(roi_back)四边各外扩该像素数,用于吸收非原生渲染
  14. # 环境相对模板截图环境的整体相位差(桌面客户端受 DPI 缩放重采样,约 1px)。
  15. # 仓库里约 1/3 的模板 roi_back 与模板等宽,此时 matchTemplate 结果只剩 1 列、
  16. # 横向一格都平移不了,1px 相位差就足以把「元素存在」判成「不存在」。
  17. # 桌面模式由 Device 置为 2,模拟器保持 0、行为逐位不变。
  18. ROI_MATCH_PAD = 0
  19. DESKTOP_ROI_MATCH_PAD = 2
  20. def set_roi_match_padding(pad: int) -> None:
  21. """设置模板匹配的 roi_back 外扩容差(像素,四边同值)。0 表示不启用。"""
  22. global ROI_MATCH_PAD
  23. ROI_MATCH_PAD = max(0, int(pad))
  24. class RuleImage(RuleImageMallResourceMixin):
  25. debug_mode: bool = False
  26. def __init__(self, roi_front: tuple, roi_back: tuple, method: str, threshold: float, file: str) -> None:
  27. """
  28. 初始化
  29. :param roi_front: 前置roi
  30. :param roi_back: 后置roi 用于匹配的区域
  31. :param method: 匹配方法 "Template matching" / "Masked template matching" / "Multi-scale template matching" / "Sift Flann"
  32. :param threshold: 阈值 0.8
  33. :param file: 相对路径, 带后缀
  34. """
  35. self._match_init = False # 这个是给后面的 等待图片稳定
  36. self._image = None # 这个是匹配的目标
  37. self._kp = None #
  38. self._des = None
  39. self._mask_loaded = False # 掩码懒加载标记: False=未加载, 加载后看 _mask
  40. self._mask = None # 掩码图, None 表示无掩码(回退普通匹配)
  41. self.last_score = -1.0 # 最近一次匹配的真实分数, -1 表示本次未产生分数
  42. self.last_scale = None # 最近一次多尺度匹配命中的缩放倍数, 仅 Multi-scale 方法写入
  43. self.method = method
  44. self.roi_front: list = list(roi_front)
  45. self.roi_back = roi_back
  46. self.threshold = threshold
  47. self.file = file
  48. @cached_property
  49. def name(self) -> str:
  50. """
  51. :return:
  52. """
  53. return Path(self.file).stem.upper()
  54. def __str__(self):
  55. return self.name
  56. __repr__ = __str__
  57. def __eq__(self, other):
  58. return str(self) == str(other)
  59. def __hash__(self):
  60. return hash(self.name)
  61. def __bool__(self):
  62. return True
  63. def load_image(self) -> None:
  64. """
  65. 加载图片
  66. :return:
  67. """
  68. if self._image is not None:
  69. return
  70. img = cv2.imdecode(fromfile(self.file, dtype=uint8), -1)
  71. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  72. self._image = img
  73. height, width, channels = self._image.shape
  74. if height != self.roi_front[3] or width != self.roi_front[2]:
  75. self.roi_front[2] = width
  76. self.roi_front[3] = height
  77. logger.debug(f"{self.name} roi_front size changed to {width}x{height}")
  78. def load_kp_des(self) -> None:
  79. if self._kp is not None and self._des is not None:
  80. return
  81. self._kp, self._des = self.sift.detectAndCompute(self.image, None)
  82. @property
  83. def image(self):
  84. """
  85. 获取图片
  86. :return:
  87. """
  88. if self._image is None:
  89. self.load_image()
  90. return self._image
  91. @cached_property
  92. def is_template_match(self) -> bool:
  93. """
  94. 是否是模板匹配
  95. :return:
  96. """
  97. return self.method == "Template matching"
  98. @cached_property
  99. def is_masked_template_match(self) -> bool:
  100. """
  101. 是否是掩码模板匹配,掩码取自同目录下 `xxx_mask.png`
  102. :return:
  103. """
  104. return self.method == "Masked template matching"
  105. @cached_property
  106. def is_multi_scale_template_match(self) -> bool:
  107. return self.method == "Multi-scale template matching"
  108. @cached_property
  109. def is_sift_flann(self) -> bool:
  110. return self.method == "Sift Flann"
  111. @cached_property
  112. def sift(self):
  113. return cv2.SIFT_create()
  114. @cached_property
  115. def kp(self):
  116. if self._kp is None:
  117. self.load_kp_des()
  118. return self._kp
  119. @cached_property
  120. def des(self):
  121. if self._des is None:
  122. self.load_kp_des()
  123. return self._des
  124. @staticmethod
  125. def _pad_roi(roi) -> tuple:
  126. """
  127. 把 ROI 四边各外扩 ROI_MATCH_PAD 像素,返回 (x0, y0, w, h)。
  128. 左上侧必须夹到 0:numpy 对负索引按「倒数」语义取值,会把裁剪位置算错,
  129. 并连带把回算出的 roi_front 算偏;右/下侧越界由切片自然截断。
  130. 返回的 x0/y0 是外扩后的原点,命中回算必须以它做基准。
  131. """
  132. x, y, w, h = [int(v) for v in roi]
  133. pad = ROI_MATCH_PAD
  134. if pad <= 0:
  135. return x, y, w, h
  136. x0 = max(0, x - pad)
  137. y0 = max(0, y - pad)
  138. return x0, y0, x + w + pad - x0, y + h + pad - y0
  139. @property
  140. def mask(self):
  141. """
  142. 掩码图,取模板同目录下 `xxx_mask.png`(`xxx.png` -> `xxx_mask.png`)。
  143. 语义为「非零参与匹配,零忽略」。文件缺失、读取失败或尺寸与模板不一致时
  144. 返回 None,由调用方回退到普通匹配,避免一张画错尺寸的掩码直接让规则报错。
  145. :return:
  146. """
  147. if not self._mask_loaded:
  148. self._mask_loaded = True
  149. path = Path(self.file)
  150. mask_path = path.with_name(f"{path.stem}_mask{path.suffix}")
  151. try:
  152. mask = cv2.imdecode(fromfile(str(mask_path), dtype=uint8), cv2.IMREAD_GRAYSCALE)
  153. except (OSError, cv2.error):
  154. mask = None
  155. if mask is None:
  156. logger.debug(f"{self.name} template mask unavailable, fallback to plain matching: {mask_path}")
  157. elif mask.shape[:2] != self.image.shape[:2]:
  158. logger.error(
  159. f"{self.name} template mask size mismatch "
  160. f"{mask.shape[:2]} != {self.image.shape[:2]}, fallback to plain matching"
  161. )
  162. else:
  163. self._mask = mask
  164. return self._mask
  165. @staticmethod
  166. def _template_is_degenerate(template: np.ndarray, mask: np.ndarray | None = None) -> bool:
  167. """
  168. 判断模板在参与匹配的区域内是否逐通道恒为常量。
  169. CCOEFF_NORMED 归一化时分母来自各通道的方差,参与区域内所有通道都完全没有起伏时,
  170. 分母才是 0,OpenCV 会走特判:无掩码时整张结果矩阵被填成 1.0(恒假阳性,且落点固定
  171. 在 roi_back 左上角),带掩码时整张变成 0/0 的 nan。两种都让这条规则失去意义,
  172. 这里提前拦掉,而不是把异常数值当成命中。
  173. """
  174. if template is None or template.size == 0:
  175. return True
  176. region = template if mask is None else template[mask > 0]
  177. if region.size == 0:
  178. return True
  179. # 掩码取值后是 `(N, C)`、整体模板是 `(H, W, C)`、灰度图是 `(H, W)` 或 `(N,)`,
  180. # 通道数只认模板本身,再统一摊平成 `(N, C)` 逐通道判断。
  181. channels = 1 if template.ndim == 2 else template.shape[-1]
  182. samples = region.reshape(-1, channels)
  183. return all(float(samples[:, channel].std()) == 0.0 for channel in range(channels))
  184. @staticmethod
  185. def _sanitize_match_result(result: np.ndarray) -> np.ndarray:
  186. """
  187. 压掉匹配结果矩阵里的非有限值。
  188. 源图窗口整块同色时 CCOEFF_NORMED 会算出 nan/inf:nan 会让
  189. `max_val > threshold` 恒为假而静默漏检,inf 则会越过阈值并把命中位置指到随机
  190. 坐标(假阳性,点错位置)。这里统一替换为 -1.0,等价于「该位置判不匹配」。
  191. """
  192. if np.isfinite(result).all():
  193. return result
  194. logger.error("match result contains nan/inf (solid-color region), treated as not matched")
  195. return np.nan_to_num(result, nan=-1.0, posinf=-1.0, neginf=-1.0)
  196. def refresh(self) -> None:
  197. self._image = None
  198. self._kp = None
  199. self._des = None
  200. self._mask = None
  201. self._mask_loaded = False
  202. self._match_init = False
  203. self.last_score = -1.0
  204. self.last_scale = None
  205. for cached_name in ('name', 'kp', 'des', 'is_template_match', 'is_sift_flann',
  206. 'is_masked_template_match', 'is_multi_scale_template_match'):
  207. del_cached_property(self, cached_name)
  208. def corp(self, image: np.array, roi: list = None) -> np.array:
  209. """
  210. 截取图片
  211. :param image:
  212. :param roi
  213. :return:
  214. """
  215. x, y, w, h = self._pad_roi(self.roi_back if roi is None else roi)
  216. return image[y:y + h, x:x + w]
  217. def match(self, image: np.array, threshold: float = None) -> bool:
  218. """
  219. :param threshold:
  220. :param image:
  221. :return:
  222. """
  223. if threshold is None:
  224. threshold = self.threshold
  225. if self.is_multi_scale_template_match:
  226. return self.match_multi_scale(image, threshold, scale_range=(0.6, 1.2))
  227. if not (self.is_template_match or self.is_masked_template_match):
  228. return self.sift_match(image)
  229. # raise Exception(f"unknown method {self.method}")
  230. source = self.corp(image)
  231. mat = self.image
  232. if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
  233. logger.error(f"Template image is invalid: {mat.shape}")
  234. return False # 模板无效, 匹配失败
  235. if mat.shape[0] > source.shape[0] or mat.shape[1] > source.shape[1]:
  236. # 模板大于源图, 视为无效匹配(避免 matchTemplate 的异常行为)
  237. return False
  238. mask = self.mask if self.is_masked_template_match else None
  239. if self._template_is_degenerate(mat, mask):
  240. logger.error(f"{self.name} template is flat (no variance in matching area), treated as not matched")
  241. return False
  242. if mask is None:
  243. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  244. else:
  245. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED, mask=mask)
  246. res = self._sanitize_match_result(res)
  247. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
  248. self.last_score = float(max_val)
  249. if self.debug_mode:
  250. logger.attr(self.name, f'matching score {max_val:.5f}')
  251. if max_val > threshold:
  252. x0, y0, _, _ = self._pad_roi(self.roi_back)
  253. self.roi_front[0] = max_loc[0] + x0
  254. self.roi_front[1] = max_loc[1] + y0
  255. return True
  256. else:
  257. return False
  258. def match_multi_scale(self, image: np.array, threshold: float = None,
  259. scales: list = None, scale_range: tuple = None) -> bool:
  260. """
  261. 多尺度模板匹配,自动尝试多个缩放比例以适应图片大小的变化
  262. :param image: 原始截图
  263. :param threshold: 匹配阈值
  264. :param scales: 缩放比例列表
  265. :param scale_range: 缩放范围 (start, end, step),例如 (0.8, 1.2, 0.1),step 默认为 0.1
  266. :return: 匹配是否成功
  267. """
  268. if threshold is None:
  269. threshold = self.threshold
  270. # 如果指定了 scale_range,自动生成 scales 列表
  271. if scale_range is not None:
  272. start, end = scale_range[:2]
  273. step = scale_range[2] if len(scale_range) > 2 else 0.1
  274. scales = sorted(set(round(x, 1) for x in np.arange(start, end + step, step)))
  275. if scales is None:
  276. scales = [0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2]
  277. else:
  278. scales = sorted(set(round(x, 1) for x in scales))
  279. source = self.corp(image)
  280. mat = self.image
  281. if mat is None or mat.shape[0] == 0 or mat.shape[1] == 0:
  282. logger.error(f"Template image is invalid: {mat.shape}")
  283. return False
  284. if self._template_is_degenerate(mat):
  285. logger.error(f"{self.name} template is flat (no variance), treated as not matched")
  286. return False
  287. # 预计算模板尺寸
  288. mat_h, mat_w = mat.shape[:2]
  289. source_h, source_w = source.shape[:2]
  290. best_score = 0
  291. best_loc = None
  292. best_scale = 1.0
  293. best_shape = None # 最佳得分对应的是哪一档缩放尺寸, 回写 roi_front 只能用它
  294. self.last_scale = None
  295. for scale in scales:
  296. scaled_w = int(mat_w * scale)
  297. scaled_h = int(mat_h * scale)
  298. # 跳过无效缩放
  299. if scaled_w < 10 or scaled_h < 10:
  300. continue
  301. # 缩放后仍大于源图则跳过该尺度(避免 matchTemplate 契约问题)
  302. if scaled_w > source_w or scaled_h > source_h:
  303. continue
  304. try:
  305. scaled_mat = cv2.resize(mat, (scaled_w, scaled_h))
  306. res = cv2.matchTemplate(source, scaled_mat, cv2.TM_CCOEFF_NORMED)
  307. res = self._sanitize_match_result(res)
  308. _, max_val, _, max_loc = cv2.minMaxLoc(res)
  309. if max_val > best_score:
  310. best_score = max_val
  311. best_loc = max_loc
  312. best_scale = scale
  313. best_shape = (scaled_w, scaled_h)
  314. except Exception as e:
  315. continue
  316. if self.debug_mode:
  317. logger.attr(self.name, f'best scale: {best_scale:.2f}, best score: {best_score:.5f}')
  318. self.last_score = float(best_score)
  319. if best_loc is not None:
  320. self.last_scale = float(best_scale)
  321. if best_score > threshold and best_loc is not None and best_shape is not None:
  322. x0, y0, _, _ = self._pad_roi(self.roi_back)
  323. self.roi_front[0] = best_loc[0] + x0
  324. self.roi_front[1] = best_loc[1] + y0
  325. self.roi_front[2] = best_shape[0]
  326. self.roi_front[3] = best_shape[1]
  327. return True
  328. else:
  329. return False
  330. def match_all(self, image: np.array, threshold: float = None, roi: list = None) -> list[tuple]:
  331. """
  332. 区别于match,这个是返回所有的匹配结果
  333. :param roi:
  334. :param image:
  335. :param threshold:
  336. :return:
  337. """
  338. if roi is not None:
  339. self.roi_back = roi
  340. if threshold is None:
  341. threshold = self.threshold
  342. if not (self.is_template_match or self.is_masked_template_match):
  343. raise Exception(f"unknown method {self.method}")
  344. source = self.corp(image)
  345. mat = self.image
  346. mask = self.mask if self.is_masked_template_match else None
  347. if self._template_is_degenerate(mat, mask):
  348. logger.error(f"{self.name} template is flat (no variance in matching area), treated as not matched")
  349. return []
  350. if mask is None:
  351. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  352. else:
  353. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED, mask=mask)
  354. results = self._sanitize_match_result(results)
  355. if results.size:
  356. self.last_score = float(results.max())
  357. locations = np.where(results >= threshold)
  358. matches = []
  359. x0, y0, _, _ = self._pad_roi(self.roi_back)
  360. for pt in zip(*locations[::-1]): # (x, y) coordinates
  361. score = results[pt[1], pt[0]]
  362. # 得分, x, y, w, h
  363. x = x0 + pt[0]
  364. y = y0 + pt[1]
  365. matches.append((score, x, y, mat.shape[1], mat.shape[0]))
  366. return matches
  367. def match_all_any(self, image: np.array, threshold: float = None, roi: list = None, nms_threshold: float = 0.3) -> list[tuple]:
  368. """
  369. 区别于match,这个是返回所有的匹配结果,去除冗余匹配项(例如:多个框选区域重叠的情况)时使用。
  370. :param roi:
  371. :param image:
  372. :param threshold:
  373. :return:
  374. """
  375. if roi is not None:
  376. self.roi_back = roi
  377. if threshold is None:
  378. threshold = self.threshold
  379. if not (self.is_template_match or self.is_masked_template_match):
  380. raise Exception(f"unknown method {self.method}")
  381. source = self.corp(image)
  382. mat = self.image
  383. mask = self.mask if self.is_masked_template_match else None
  384. if self._template_is_degenerate(mat, mask):
  385. logger.error(f"{self.name} template is flat (no variance in matching area), treated as not matched")
  386. return []
  387. if mask is None:
  388. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  389. else:
  390. results = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED, mask=mask)
  391. results = self._sanitize_match_result(results)
  392. if results.size:
  393. self.last_score = float(results.max())
  394. locations = np.where(results >= threshold)
  395. matches = []
  396. x0, y0, _, _ = self._pad_roi(self.roi_back)
  397. for pt in zip(*locations[::-1]): # (x, y) coordinates
  398. score = results[pt[1], pt[0]]
  399. # 得分, x, y, w, h
  400. x = x0 + pt[0]
  401. y = y0 + pt[1]
  402. matches.append((score, x, y, mat.shape[1], mat.shape[0]))
  403. if len(matches) > 0:
  404. scores = np.array([m[0] for m in matches])
  405. boxes = np.array([[m[1], m[2], m[3], m[4]] for m in matches])
  406. # 使用OpenCV的NMSBoxes
  407. indices = cv2.dnn.NMSBoxes(boxes.tolist(), scores.tolist(), score_threshold=threshold, nms_threshold=nms_threshold)
  408. filtered_matches = [matches[i] for i in indices]
  409. return filtered_matches
  410. return matches
  411. def coord(self) -> tuple:
  412. """
  413. 获取roi_front的随机的点击的坐标
  414. :return:
  415. """
  416. x, y, w, h = self.roi_front
  417. return x + random_normal_distribution_int(0, w), y + random_normal_distribution_int(0, h)
  418. def coord_more(self) -> tuple:
  419. """
  420. 获取roi_back的随机的点击的坐标
  421. :return:
  422. """
  423. x, y, w, h = self.roi_back
  424. return x + random_normal_distribution_int(0, w), y + random_normal_distribution_int(0, h)
  425. def front_center(self) -> tuple:
  426. """
  427. 获取roi_front的中心坐标
  428. :return:
  429. """
  430. x, y, w, h = self.roi_front
  431. return int(x + w//2), int(y + h//2)
  432. def test_match(self, image: np.array):
  433. self.debug_mode = True
  434. if self.is_template_match or self.is_masked_template_match or self.is_multi_scale_template_match:
  435. return self.match(image)
  436. if self.is_sift_flann:
  437. return self.sift_match(image, show=True)
  438. def sift_match(self, image: np.array, show=False) -> bool:
  439. """
  440. 特征匹配,同样会修改 roi_front
  441. :param image: 是游戏的截图,就是转通道后的截图
  442. :param show: 测试用的
  443. :return:
  444. """
  445. source = self.corp(image)
  446. kp, des = self.sift.detectAndCompute(source, None)
  447. # 参数1:index_params
  448. # 对于SIFT和SURF,可以传入参数index_params=dict(algorithm=FLANN_INDEX_KDTREE, trees=5)。
  449. # 对于ORB,可以传入参数index_params=dict(algorithm=FLANN_INDEX_LSH, table_number=6, key_size=12)。
  450. index_params = dict(algorithm=1, trees=5)
  451. # 参数2:search_params 指定递归遍历的次数,值越高结果越准确,但是消耗的时间也越多。
  452. search_params = dict(checks=50)
  453. # 根据设置的参数创建特征匹配器 指定匹配的算法和kd树的层数,指定返回的个数
  454. flann = cv2.FlannBasedMatcher(index_params, search_params)
  455. # 利用创建好的特征匹配器利用k近邻算法来用模板的特征描述符去匹配图像的特征描述符,k指的是返回前k个最匹配的特征区域
  456. # 返回的是最匹配的两个特征点的信息,返回的类型是一个列表,列表元素的类型是Dmatch数据类型,具体是什么我也不知道
  457. # 第一个参数是小图的des, 第二个参数是大图的des
  458. matches = flann.knnMatch(self.des, des, k=2)
  459. good = []
  460. result = True
  461. for i, (m, n) in enumerate(matches):
  462. # 设定阈值, 距离小于对方的距离的0.7倍我们认为是好的匹配点.
  463. if m.distance < 0.6 * n.distance:
  464. good.append(m)
  465. if len(good) >= 10:
  466. src_pts = float32([self.kp[m.queryIdx].pt for m in good]).reshape(-1, 1, 2)
  467. dst_pts = float32([kp[m.trainIdx].pt for m in good]).reshape(-1, 1, 2)
  468. # 计算透视变换矩阵m, 要求点的数量>=4
  469. m, mask = cv2.findHomography(src_pts, dst_pts, cv2.RANSAC, 5.0)
  470. # 创建一个包含模板图像四个角坐标的数组
  471. w, h = self.roi_front[2], self.roi_front[3]
  472. pts = float32([[0, 0], [0, h - 1], [w - 1, h - 1], [w - 1, 0]]).reshape(-1, 1, 2)
  473. if m is None:
  474. result = False
  475. else:
  476. dst = int32(cv2.perspectiveTransform(pts, m))
  477. x0, y0, _, _ = self._pad_roi(self.roi_back)
  478. self.roi_front[0] = dst[0, 0, 0] + x0
  479. self.roi_front[1] = dst[0, 0, 1] + y0
  480. if show:
  481. cv2.polylines(source, [dst], isClosed=True, color=(0, 0, 255), thickness=2)
  482. if not is_approx_rectangle(np.array([pos[0] for pos in dst])):
  483. result = False
  484. else:
  485. result = False
  486. # https://blog.csdn.net/cungudafa/article/details/105399278
  487. # https://blog.csdn.net/qq_45832961/article/details/122776322
  488. if show:
  489. # 准备一个空的掩膜来绘制好的匹配
  490. mask_matches = [[0, 0] for i in range(len(matches))]
  491. # 向掩膜中添加数据
  492. for i, (m, n) in enumerate(matches):
  493. if m.distance < 0.6 * n.distance: # 理论上0.7最好
  494. mask_matches[i] = [1, 0]
  495. img_matches = cv2.drawMatchesKnn(self.image, self.kp, source, kp, matches, None,
  496. matchColor=(0, 255, 0), singlePointColor=(255, 0, 0),
  497. matchesMask=mask_matches, flags=0)
  498. cv2.imshow(f'Sift Flann: {self.name}', img_matches)
  499. cv2.waitKey(0)
  500. cv2.destroyAllWindows()
  501. return result
  502. def match_mean_color(self, image, color: tuple, bias=10) -> bool:
  503. """
  504. :param image:
  505. :param color: rgb
  506. :param bias:
  507. :return:
  508. """
  509. image = self.corp(image)
  510. average_color = cv2.mean(image)
  511. if self.debug_mode:
  512. logger.info(f'{self.name} average_color: {average_color}')
  513. for i in range(3):
  514. if abs(average_color[i] - color[i]) > bias:
  515. return False
  516. return True
  517. def match_brightness(
  518. self, image: np.array,
  519. threshold: float = 0.9,
  520. roi: list = None,
  521. gray: bool = False) -> bool:
  522. source = self.corp(image, roi) if roi is not None else self.corp(image)
  523. template = self.image
  524. if len(source.shape) != 3:
  525. raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
  526. source_gray = cv2.cvtColor(source, cv2.COLOR_RGB2GRAY)
  527. source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
  528. if len(template.shape) != 3:
  529. raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
  530. template_gray = cv2.cvtColor(template, cv2.COLOR_RGB2GRAY)
  531. template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
  532. source_value = float(source_gray.mean()) if gray else float(source_hsv[:, :, 2].mean())
  533. template_value = float(template_gray.mean()) if gray else float(template_hsv[:, :, 2].mean())
  534. score = 1.0 - abs(source_value - template_value) / 255.0
  535. score = max(0.0, min(1.0, score))
  536. if self.debug_mode:
  537. logger.attr(self.name, f'brightness similarity {score:.5f}')
  538. logger.info(f'Template value: {template_value}, Source value: {source_value}')
  539. return score >= threshold
  540. def match_saturation(self, image: np.array, threshold: float = 0.9, roi: list = None) -> bool:
  541. source = self.corp(image, roi) if roi is not None else self.corp(image)
  542. template = self.image
  543. if len(source.shape) != 3:
  544. raise Exception(f'{self.name} source image must be 3-channel RGB, got shape={source.shape}')
  545. source_hsv = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)
  546. if len(template.shape) != 3:
  547. raise Exception(f'{self.name} template image must be 3-channel RGB, got shape={template.shape}')
  548. template_hsv = cv2.cvtColor(template, cv2.COLOR_RGB2HSV)
  549. source_value = float(source_hsv[:, :, 1].mean())
  550. template_value = float(template_hsv[:, :, 1].mean())
  551. score = 1.0 - abs(source_value - template_value) / 255.0
  552. score = max(0.0, min(1.0, score))
  553. if self.debug_mode:
  554. logger.attr(self.name, f'saturation similarity {score:.5f}')
  555. logger.info(f'Template value: {template_value}, Source value: {source_value}')
  556. return score >= threshold
  557. if __name__ == "__main__":
  558. from dev_tools.assets_test import detect_image
  559. IMAGE_FILE = './log/test/QQ截图20240223151924.png'
  560. from tasks.Restart.assets import RestartAssets
  561. jade = RestartAssets.I_HARVEST_JADE
  562. jade.method = 'Sift Flann'
  563. sign = RestartAssets.I_HARVEST_SIGN
  564. sign.method = 'Sift Flann'
  565. print(jade.roi_front)
  566. detect_image(IMAGE_FILE, jade)
  567. detect_image(IMAGE_FILE, sign)
  568. print(jade.roi_front)