utils.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. import cv2
  2. import numpy as np
  3. from module.logger import logger
  4. def detect_safe_area(image, target_gray_value=255, tolerance=0):
  5. """
  6. 在灰度图片中查找与目标灰度值在容差范围内的纯色区块,并返回这些区块的坐标。
  7. :param image_path: 图片路径
  8. :param target_gray_value: 目标灰度值,默认为255(最亮)
  9. :param tolerance: 灰度差异容忍度,默认为0(即完全匹配)
  10. :return: 包含找到的纯色区块坐标元组的列表
  11. """
  12. # 加载图片并转换为灰度图
  13. # img = Image.open(image_path).convert('L') # 'L' 表示转换为灰度图
  14. # img_arr = np.array(img) # 转换为numpy数组以便高效处理
  15. # 将图片转换为灰度图
  16. gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
  17. _, img_arr = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
  18. # 初始化结果列表
  19. solid_regions = []
  20. # 遍历图片的每个像素
  21. for y in range(img_arr.shape[0]): # 高度
  22. for x in range(img_arr.shape[1]): # 宽度
  23. pixel_gray_value = img_arr[y, x]
  24. # 检查当前像素灰度值是否与目标灰度值在容差范围内匹配
  25. if abs(pixel_gray_value - target_gray_value) <= tolerance:
  26. # 检查周围是否也是同样的灰度值,以确定这是一个区块而非单个像素
  27. is_solid_region = True
  28. for dy in range(-1, 2): # 检查3x3区域
  29. for dx in range(-1, 2):
  30. if dy == 0 and dx == 0:
  31. continue # 跳过中心点
  32. ny, nx = y + dy, x + dx
  33. if 0 <= ny < img_arr.shape[0] and 0 <= nx < img_arr.shape[1]:
  34. if abs(img_arr[ny, nx] - target_gray_value) > tolerance:
  35. is_solid_region = False
  36. break
  37. if not is_solid_region:
  38. break
  39. if is_solid_region:
  40. # 记录纯色区块的左上角坐标和右下角坐标
  41. start_x, start_y = x, y
  42. while start_y > 0 and abs(img_arr[start_y - 1, x] - target_gray_value) <= tolerance:
  43. start_y -= 1
  44. while start_x > 0 and abs(img_arr[y, start_x - 1] - target_gray_value) <= tolerance:
  45. start_x -= 1
  46. end_x, end_y = x, y
  47. while end_y < img_arr.shape[0] - 1 and abs(img_arr[end_y + 1, x] - target_gray_value) <= tolerance:
  48. end_y += 1
  49. while end_x < img_arr.shape[1] - 1 and abs(img_arr[y, end_x + 1] - target_gray_value) <= tolerance:
  50. end_x += 1
  51. solid_regions.append(((start_x, start_y), (end_x + 1, end_y + 1)))
  52. return solid_regions
  53. def detect_safe_area2(image, safe_color_lower, safe_color_upper, num_areas=3, debug=False):
  54. """
  55. 找出当前截图里最大的纯色区域,找出来的这个区域将被用于随机点击的区域。
  56. :note: 注意拿到这个区域后要马上点击,不要sleep!
  57. :param image: 截图图像
  58. :return: 最大纯色区域的坐标
  59. """
  60. # 将图片从BGR转换到HSV色彩空间
  61. hsv = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
  62. # 定义颜色范围
  63. # 注意:这里的颜色范围需要根据实际图片中的安全区域颜色进行调整
  64. mask = cv2.inRange(hsv, safe_color_lower, safe_color_upper)
  65. # 应用形态学操作来消除噪点和填补小的孔洞
  66. kernel = np.ones((5, 5), np.uint8)
  67. mask = cv2.dilate(mask, kernel, iterations=1)
  68. mask = cv2.erode(mask, kernel, iterations=1)
  69. # 查找轮廓
  70. contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  71. # 计算每个轮廓的面积并排序
  72. # areas = [(cv2.contourArea(contour), contour) for contour in contours]
  73. areas = []
  74. for contour in contours:
  75. area = cv2.contourArea(contour)
  76. print(f"area={area}")
  77. areas.append((area, contour))
  78. # areas.sort(reverse=True)
  79. # 按面积降序排序
  80. areas_sorted = sorted(areas, key=lambda x: x[0], reverse=True)
  81. if debug:
  82. # 绘制面积排名前3的区域
  83. for area, contour in areas_sorted[:num_areas]:
  84. x1, y1, w1, h1 = cv2.boundingRect(contour)
  85. # 使用蓝色边框,线宽为4
  86. cv2.rectangle(image, (x1, y1), (x1 + w1, y1 + h1), (255, 0, 0), 4)
  87. # 使用红色边框,线宽为3
  88. cv2.drawContours(image, [contour], -1, (0, 0, 255), 3)
  89. # 假设最大的轮廓是安全区域
  90. if contours:
  91. safe_area = max(contours, key=cv2.contourArea)
  92. x, y, w, h = cv2.boundingRect(safe_area)
  93. if debug:
  94. # 绘制安全区域
  95. cv2.drawContours(image, [safe_area], -1, (0, 255, 0), 3)
  96. return (x, y, w, h)
  97. return (0, 0, 0, 0)
  98. def test_ocr_locate_dokan_target():
  99. import cv2
  100. from module.atom.ocr import RuleOcr
  101. logger.info("========================================>>test 2")
  102. O_DOKAN_MAP = RuleOcr(roi=(270, 130, 740, 460), area=(270, 130, 740, 460), mode="Full", method="Default",
  103. keyword="万", name="dokan_map")
  104. image = cv2.imread("g:/yys/oas/tests/1.png")
  105. pos = O_DOKAN_MAP.ocr_full(image)
  106. print(f"image1: {pos}")
  107. image = cv2.imread("g:/yys/oas/tests/2.png")
  108. pos = O_DOKAN_MAP.ocr_full(image)
  109. print(f"image2: {pos}")
  110. image = cv2.imread("g:/yys/oas/tests/3.png")
  111. pos = O_DOKAN_MAP.ocr_full(image)
  112. print(f"image3: {pos}")
  113. image = cv2.imread("g:/yys/oas/tests/4.png")
  114. pos = O_DOKAN_MAP.ocr_full(image)
  115. print(f"image4: {pos}")
  116. if pos == (0, 0, 0, 0):
  117. logger.info("failed to find {self.O_DOKAN_MAP.keyword}")
  118. else:
  119. # 取中间
  120. x = pos[0] + pos[2] / 2
  121. # 往上偏移20
  122. y = pos[1] - 20
  123. logger.info(f"ocr detect result pos={pos}, try click pos, x={x}, y={y}")
  124. def test_anti_detect_random_click():
  125. import cv2
  126. # image_list = ['g:/yys/oas/tests/1.png', 'g:/yys/oas/tests/2.png', 'g:/yys/oas/tests/3.png', 'g:/yys/oas/tests/4.png', 'g:/yys/oas/tests/5.png']
  127. image_list = ['g:/yys/oas/tests/a.png', 'g:/yys/oas/tests/b.png', 'g:/yys/oas/tests/c.png']
  128. for item in image_list:
  129. print(f"item: {item}")
  130. image = cv2.imread(item)
  131. # 注意:这里的颜色范围需要根据实际图片进行调整
  132. # http://kb.rg4.net/docs/omassistant/omassistant-1fnirpstkhdnh
  133. # 0<=h<20, 红色
  134. # 30<=h<45,黄色
  135. # 45<=h<90,绿色
  136. # 90<=h<125,青色
  137. # 125<=h<150,蓝色
  138. # 150<=h<175,紫色
  139. # 175<=h<200,粉红色
  140. # 200<=h<220,砖红色
  141. # 220<=h<255,品红色
  142. # 示例使用:假设安全区域是绿色的
  143. safe_color_lower = np.array([45, 25, 25]) # HSV颜色空间的绿色下界
  144. safe_color_upper = np.array([90, 255, 255]) # HSV颜色空间的绿色上界
  145. # 读取图片
  146. image = cv2.imread('c.png')
  147. if image is None:
  148. print("Error: Image not found.")
  149. else:
  150. # 调用函数
  151. pos = detect_safe_area2(image, safe_color_lower, safe_color_upper, 3, True)
  152. print(f"pos={pos}")
  153. # 显示结果
  154. cv2.imshow('Safe Area', image)
  155. cv2.waitKey(0)
  156. cv2.destroyAllWindows()