当前位置:   article > 正文

python调用OpenCV进行找图并返回坐标示例_python找图返回坐标

python找图返回坐标
  1. import threading
  2. import cv2 # opencv库
  3. import numpy as np # 将图片转换成数组,opencv无法直接处理mss的截图,用它进行转换
  4. import mss # 截屏库
  5. from concurrent.futures import ThreadPoolExecutor
  6. import os
  7. def findpic(x1, y1, x2, y2, lujin, xiangsidu):
  8. chang = x2 - x1
  9. kuan = y2 - y1
  10. quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
  11. with mss.mss() as sct:
  12. # 获取大图片
  13. quyujieping = sct.grab(quyu) # 对屏幕的左上角800*600进行截图
  14. quyujieping = np.array(quyujieping) # 调用numpy库将mss库的截屏转换一下,并赋值给变量quyujieping
  15. datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
  16. xiaotupian = cv2.imread(lujin)
  17. xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
  18. # 在大图片中查找小图片
  19. result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
  20. # 筛选结果
  21. locations = np.where(result >= xiangsidu) # 用numpy筛选一下结果高于0.85的
  22. locations = list(zip(*locations[::-1])) # 将结果再次处理一下,仅保留坐标
  23. if len(locations) > 0:
  24. return (locations[0][0] + x1, locations[0][1] + y1)
  25. return (-1, -1)
  26. def zuijin_zuobiao(coordinates_list, given_coordinate):
  27. # 确保给定的坐标包含两个元素
  28. if len(given_coordinate) != 2:
  29. raise ValueError("给定的坐标必须包含两个元素 (x, y)")
  30. # 初始化最小距离和对应坐标的坐标
  31. min_distance = float('inf') # 使用正无穷大作为初始值
  32. zuijin_zuobiao = None
  33. # 计算曼哈顿距离并找到最近坐标
  34. for coord in coordinates_list:
  35. # 确保列表中的每个坐标也包含两个元素
  36. if len(coord) != 2:
  37. raise ValueError("坐标列表中的每个坐标必须包含两个元素 (x, y)")
  38. x1, y1 = given_coordinate
  39. x2, y2 = coord
  40. distance = abs(x1 - x2) + abs(y1 - y2)
  41. # 如果是第一个坐标或者距离更近,则更新最小距离和最近距离的坐标
  42. if distance < min_distance:
  43. min_distance = distance
  44. zuijin_zuobiao = coord
  45. if zuijin_zuobiao==None:
  46. return -1,-1
  47. else:
  48. return zuijin_zuobiao
  49. def chazhao_tupian(datupian_huidu, xiaotupian_path, xiangsidu):
  50. xiaotupian = cv2.imread(xiaotupian_path)
  51. if xiaotupian is None:
  52. return [] # 返回空列表表示未找到图片或路径错误
  53. xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
  54. result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
  55. locations = np.where(result >= xiangsidu)
  56. locations = list(zip(*locations[::-1]))
  57. return [(loc[0] + int(os.path.splitext(os.path.basename(xiaotupian_path))[0].split("_")[1]),
  58. loc[1] + int(os.path.splitext(os.path.basename(xiaotupian_path))[0].split("_")[2]))
  59. for loc in locations]
  60. def findpicE(x1, y1, x2, y2, liebiao_dir, xiangsidu):
  61. chang = x2 - x1
  62. kuan = y2 - y1
  63. quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
  64. with mss.mss() as sct:
  65. quyujieping = sct.grab(quyu)
  66. quyujieping = np.array(quyujieping)
  67. datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
  68. # 使用线程池并行处理图片匹配
  69. with ThreadPoolExecutor() as executor:
  70. futures = []
  71. for xiaotupian_name in os.listdir(liebiao_dir):
  72. xiaotupian_path = os.path.join(liebiao_dir, xiaotupian_name)
  73. future = executor.submit(chazhao_tupian, datupian_huidu, xiaotupian_path, xiangsidu)
  74. futures.append(future)
  75. # 收集所有线程的结果
  76. zuobiao_liebiao = []
  77. for future in futures:
  78. zuobiao_liebiao.extend(future.result())
  79. juese_zuobiao = (0,0) # 假设这个函数已定义并返回角色坐标
  80. weiyi_zuobiao = zuijin_zuobiao(zuobiao_liebiao, juese_zuobiao) # 假设这个函数已定义并返回最近坐标
  81. if weiyi_zuobiao[0]>0:
  82. return weiyi_zuobiao
  83. return -1,-1
  84. def findpicEX(x1, y1, x2, y2, liebiao_dir, xiangsidu):
  85. chang = x2 - x1
  86. kuan = y2 - y1
  87. quyu = {"left": x1, "top": y1, "width": chang, "height": kuan}
  88. with mss.mss() as sct:
  89. quyujieping = sct.grab(quyu)
  90. quyujieping = np.array(quyujieping)
  91. datupian_huidu = cv2.cvtColor(quyujieping, cv2.COLOR_BGR2GRAY)
  92. for xiaotupian_mingcheng in os.listdir(liebiao_dir):
  93. if not xiaotupian_mingcheng.lower().endswith(('.png', '.jpg', '.jpeg', '.bmp', '.gif')):
  94. continue # 跳过非图片文件
  95. xiaotupian_path = os.path.join(liebiao_dir, xiaotupian_mingcheng)
  96. xiaotupian = cv2.imread(xiaotupian_path)
  97. if xiaotupian is None:
  98. continue # 跳过无法读取的图片
  99. xiaotupian_huidu = cv2.cvtColor(xiaotupian, cv2.COLOR_BGR2GRAY)
  100. result = cv2.matchTemplate(datupian_huidu, xiaotupian_huidu, cv2.TM_CCOEFF_NORMED)
  101. locations = np.where(result >= xiangsidu)
  102. if locations is not None and len(locations[0]) > 0:
  103. return (locations[0][0] + x1, locations[1][0] + y1) # 注意这里要分别取locations[0]和locations[1]
  104. return (-1, -1)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/在线问答5/article/detail/888127
推荐阅读
相关标签
  

闽ICP备14008679号