map.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import cv2
  2. import numpy as np
  3. from numpy import uint8, fromfile
  4. from tasks.base_task import BaseTask
  5. from tasks.SixRealms.assets import SixRealmsAssets
  6. from tasks.SixRealms.common import MoonSeaType
  7. def binaryzate(img):
  8. img0 = img[..., 0]
  9. img1 = img[..., 1]
  10. img2 = img[..., 2]
  11. mask0 = cv2.inRange(img0, MoonSeaMap.COLOR_RANGE_0[0], MoonSeaMap.COLOR_RANGE_0[1])
  12. result0 = cv2.bitwise_and(img0, img0, mask=mask0)
  13. mask1 = cv2.inRange(img1, 200, 250)
  14. result1 = cv2.bitwise_and(img1, img1, mask=mask1)
  15. mask2 = cv2.inRange(img2, 180, 230)
  16. result2 = cv2.bitwise_and(img2, img2, mask=mask2)
  17. result = np.zeros(img.shape, dtype=np.uint8)
  18. result[..., 0]= result0
  19. result[..., 1]= result1
  20. result[..., 2]= result2
  21. return result
  22. class MoonSeaMap(BaseTask, SixRealmsAssets):
  23. LOWER_COLOR = np.array([220, 190, 170])
  24. UPPER_COLOR = np.array([260, 250, 230])
  25. COLOR_RANGE_0 = [220, 250]
  26. def detect_current(self) -> list[tuple]:
  27. # self.screenshot()
  28. img = self.device.image
  29. # img = 255 - img
  30. # print(img[403, 384])
  31. # print(img[444, 378])
  32. # print(img[411, 381])
  33. # binary_img = img
  34. # img = cv2.inRange(img, self.LOWER_COLOR, self.UPPER_COLOR)
  35. # cv2.imwrite('test_0.png', img[..., 0])
  36. # cv2.imwrite('test_1.png', img[..., 1])
  37. # cv2.imwrite('test_2.png', img[..., 2])
  38. img = binaryzate(img)
  39. # iii = np.zeros((720, 1280, 3), dtype=np.uint8)
  40. # iii[..., 0] = img
  41. # iii[..., 1] = img
  42. # iii[..., 2] = img
  43. # img = iii
  44. cv2.imshow('Binary Image', img)
  45. cv2.waitKey(0)
  46. cv2.destroyAllWindows()
  47. boxs = self.O_OCR_MAP.detect_and_ocr(img)
  48. for box in boxs:
  49. print(box)
  50. return []
  51. def _test():
  52. from module.config.config import Config
  53. from module.device.device import Device
  54. c = Config('oas1')
  55. d = Device(c)
  56. t = MoonSeaMap(c, d)
  57. import cv2
  58. file_image = r'C:\Users\Ryland\Desktop\Desktop\34.png'
  59. image = cv2.imdecode(fromfile(str(file_image), dtype=uint8), -1)
  60. # image = cv2.imread(file_image)
  61. image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  62. t.device.image = image
  63. t.detect_current()
  64. def _test2():
  65. file_image = r'test_1.png'
  66. image = cv2.imdecode(fromfile(str(file_image), dtype=uint8), -1)
  67. print(image.shape)
  68. result = image
  69. mask = cv2.inRange(image, 210, 250)
  70. # mask = 255 * mask
  71. result = cv2.bitwise_and(image, image, mask=mask)
  72. # 显示结果
  73. cv2.imshow('Result', result)
  74. cv2.waitKey(0)
  75. cv2.destroyAllWindows()
  76. if __name__ == '__main__':
  77. _test()