image.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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.base.decorator import cached_property
  9. from module.logger import logger
  10. class RuleImage:
  11. def __init__(self, roi_front: tuple, roi_back: tuple, method: str, threshold: float, file: str) -> None:
  12. """
  13. 初始化
  14. :param roi_front: 前置roi
  15. :param roi_back: 后置roi 用于匹配的区域
  16. :param method: 匹配方法 "Template matching"
  17. :param threshold: 阈值 0.8
  18. :param file: 相对路径, 带后缀
  19. """
  20. self.roi_front: list = list(roi_front)
  21. self.roi_back = roi_back
  22. self.method = method
  23. self.threshold = threshold
  24. self.file = file
  25. self._match_init = False # 这个是给后面的 等待图片稳定
  26. self._image = None # 这个是匹配的目标
  27. @cached_property
  28. def name(self) -> str:
  29. """
  30. :return:
  31. """
  32. return Path(self.file).stem
  33. def __str__(self):
  34. return self.name
  35. __repr__ = __str__
  36. def __eq__(self, other):
  37. return str(self) == str(other)
  38. def __hash__(self):
  39. return hash(self.name)
  40. def __bool__(self):
  41. return True
  42. def load_image(self) -> None:
  43. """
  44. 加载图片
  45. :return:
  46. """
  47. if self._image is not None:
  48. return
  49. img = cv2.imdecode(fromfile(self.file, dtype=uint8), -1)
  50. img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  51. self._image = img
  52. height, width, channels = self._image.shape
  53. if height != self.roi_front[3] or width != self.roi_front[2]:
  54. self.roi_front[2] = width
  55. self.roi_front[3] = height
  56. logger.info(f"roi_front size changed to {width}x{height}")
  57. @property
  58. def image(self):
  59. """
  60. 获取图片
  61. :return:
  62. """
  63. if self._image is None:
  64. self.load_image()
  65. return self._image
  66. @cached_property
  67. def is_template_match(self) -> bool:
  68. """
  69. 是否是模板匹配
  70. :return:
  71. """
  72. return self.method == "Template matching"
  73. def corp(self, image: np.array) -> np.array:
  74. """
  75. 截取图片
  76. :param image:
  77. :return:
  78. """
  79. x, y, w, h = self.roi_back
  80. return image[y:y + h, x:x + w]
  81. def match(self, image: np.array, threshold: float = None) -> bool:
  82. """
  83. :param threshold:
  84. :param image:
  85. :return:
  86. """
  87. if threshold is None:
  88. threshold = self.threshold
  89. if not self.is_template_match:
  90. raise Exception(f"unknown method {self.method}")
  91. source = self.corp(image)
  92. mat = self.image
  93. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  94. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
  95. if max_val > threshold:
  96. self.roi_front[0] = max_loc[0] + self.roi_back[0]
  97. self.roi_front[1] = max_loc[1] + self.roi_back[1]
  98. return True
  99. else:
  100. return False
  101. def coord(self) -> tuple:
  102. """
  103. 获取roi_front的随机的点击的坐标
  104. :return:
  105. """
  106. x, y, w, h = self.roi_front
  107. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  108. def coord_more(self) -> tuple:
  109. """
  110. 获取roi_back的随机的点击的坐标
  111. :return:
  112. """
  113. x, y, w, h = self.roi_back
  114. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  115. if __name__ == "__main__":
  116. image = cv2.imread("D:/4713.jpg")
  117. mat = RuleImage(roi_front=(758,122,66,77), roi_back=(339,104,836,120), threshold=0.8, method="Template matching", file="./tasks/AreaBoss/res/res_explore.png")
  118. print(mat.match(image))