image.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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._match_init = False # 这个是给后面的 等待图片稳定
  21. self._image = None # 这个是匹配的目标
  22. self.method = method
  23. self.roi_front: list = list(roi_front)
  24. self.roi_back = roi_back
  25. self.threshold = threshold
  26. self.file = file
  27. @cached_property
  28. def name(self) -> str:
  29. """
  30. :return:
  31. """
  32. return Path(self.file).stem.upper()
  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. x, y, w, h = int(x), int(y), int(w), int(h)
  81. return image[y:y + h, x:x + w]
  82. def match(self, image: np.array, threshold: float = None) -> bool:
  83. """
  84. :param threshold:
  85. :param image:
  86. :return:
  87. """
  88. if threshold is None:
  89. threshold = self.threshold
  90. if not self.is_template_match:
  91. raise Exception(f"unknown method {self.method}")
  92. source = self.corp(image)
  93. mat = self.image
  94. res = cv2.matchTemplate(source, mat, cv2.TM_CCOEFF_NORMED)
  95. min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res) # 最小匹配度,最大匹配度,最小匹配度的坐标,最大匹配度的坐标
  96. # logger.attr(self.name, max_val)
  97. if max_val > threshold:
  98. self.roi_front[0] = max_loc[0] + self.roi_back[0]
  99. self.roi_front[1] = max_loc[1] + self.roi_back[1]
  100. return True
  101. else:
  102. return False
  103. def coord(self) -> tuple:
  104. """
  105. 获取roi_front的随机的点击的坐标
  106. :return:
  107. """
  108. x, y, w, h = self.roi_front
  109. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  110. def coord_more(self) -> tuple:
  111. """
  112. 获取roi_back的随机的点击的坐标
  113. :return:
  114. """
  115. x, y, w, h = self.roi_back
  116. return x + np.random.randint(0, w), y + np.random.randint(0, h)
  117. def front_center(self) -> tuple:
  118. """
  119. 获取roi_front的中心坐标
  120. :return:
  121. """
  122. x, y, w, h = self.roi_front
  123. return int(x + w//2), int(y + h//2)
  124. if __name__ == "__main__":
  125. image = cv2.imread("D:/4713.jpg")
  126. 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")
  127. print(mat.match(image))