utils.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import cv2
  2. # import pytesseract
  3. def find_image(image, pattern, threshold: float = 0.9, top1_only: bool = True, draw_result: bool = False):
  4. '''
  5. 以图搜图
  6. 使用全图进行模板匹配,来匹配当前画面中有没有输入的pattern
  7. :return 匹配到的数量
  8. '''
  9. results = cv2.matchTemplate(image, pattern, cv2.TM_CCOEFF_NORMED)
  10. count = 0
  11. img_disp = None
  12. pos = []
  13. if draw_result:
  14. img_disp = image.copy()
  15. if top1_only:
  16. _, max_val, _, max_loc = cv2.minMaxLoc(results)
  17. # 找到最佳匹配位置的坐标
  18. if max_val >= threshold:
  19. top_left = max_loc
  20. bottom_right = (top_left[0] + pattern.shape[1], top_left[1] + pattern.shape[0])
  21. count = 1
  22. # print(f'top_left={top_left}, similiar={max_val}')
  23. pos.append((top_left[0], top_left[1], bottom_right[0], bottom_right[1]))
  24. if draw_result:
  25. # 在原始图像上绘制矩形框来突出显示匹配出的图案
  26. cv2.rectangle(img_disp, top_left, bottom_right, (0, 255, 0), 2)
  27. else:
  28. count = 0
  29. else:
  30. #筛选大于一定匹配值的点
  31. val,result = cv2.threshold(results,threshold,1.0,cv2.THRESH_BINARY)
  32. match_locs = cv2.findNonZero(result)
  33. # print('match_locs.shape:',match_locs.shape)
  34. # print('match_locs:\n',match_locs)
  35. count = len(match_locs)
  36. # 找到最佳匹配位置的坐标
  37. for match_loc_t in match_locs:
  38. #match_locs是一个3维数组,第2维固定长度为1,取其下标0对应数组
  39. top_left = match_loc_t[0]
  40. bottom_right = (top_left[0] + pattern.shape[1], top_left[1] + pattern.shape[0])
  41. # print(f'top_left={top_left}, similar={results[top_left[1],top_left[0]]}')
  42. pos.append((top_left[0], top_left[1], bottom_right[0], bottom_right[1]))
  43. if draw_result:
  44. # 在原始图像上绘制矩形框来突出显示匹配出的图案
  45. cv2.rectangle(img_disp, top_left, bottom_right, (0,255,0), 5, 8, 0 )
  46. cv2.circle(result, top_left, 10, (255,0,0), 3 )
  47. # TODO 去除重叠的区域,只留该区域里相似度最高的
  48. if draw_result:
  49. cv2.imshow('Matched Result', img_disp)
  50. cv2.waitKey(0)
  51. cv2.destroyAllWindows()
  52. return count, pos
  53. # def find_text(image, text) -> int:
  54. # '''
  55. # 以文搜图
  56. # 使用图像识别库进行图像识别和文字检测
  57. # 使用全图进行模板匹配,来匹配当前画面中有没有输入的文字
  58. # :return 匹配到的数量
  59. # '''
  60. # #
  61. # extracted_text = pytesseract.image_to_string(image)
  62. # match_indices = [i for i, item in enumerate(extracted_text) if text in item]
  63. # count = len(match_indices)
  64. # if count > 0:
  65. # print(f'Found {count} locations matches the input pattern.')
  66. # # 可以根据需求输出或显示匹配的结果
  67. # else:
  68. # print('Pattern not found.')
  69. # return count
  70. if __name__ == '__main__':
  71. # 读取待搜索的图像和目标图像
  72. image_path = "tests/toppa_scrolls/test.png"
  73. pattern_path = "tests/toppa_scrolls/res_toppa_scroll.png"
  74. image = cv2.imread(image_path)
  75. pattern = cv2.imread(pattern_path)
  76. if image is None or pattern is None:
  77. print(f"image file not exists? {image_path}, patter image file: {pattern_path}")
  78. else:
  79. # find_text(image, "突破")
  80. count, pos = find_image(image, pattern, 0.9, False, False)
  81. print(f"count={count}, pos={pos}")