1 Star 0 Fork 0

白龟山娃/gametest

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
aa.py 2.61 KB
一键复制 编辑 原始数据 按行查看 历史
白龟山娃 提交于 2024-03-15 14:12 . 11
import cv2
import numpy as np
import matplotlib.pyplot as plt
# 加载图像和模板
# image = cv2.imread('zuoqi.bmp')
# template = cv2.imread('zuoqi3.bmp')
# img = cv2.imread('zuoqi.bmp', 0)
# template = cv2.imread('zuoqi1.bmp', 0)
# h, w = template.shape[:2] # rows->h, cols->w
#
# # 相关系数匹配方法:cv2.TM_CCOEFF
# res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF)
# print("res",res)
#
#
# min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)
#
# left_top = max_loc # 左上角
# right_bottom = (left_top[0] + w, left_top[1] + h) # 右下角
# cv2.rectangle(img, left_top, right_bottom, 255, 2) # 画出矩形位置
# plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.title('Template Matching Result')
# plt.axis('off')
# plt.show()
# # 将输入图像转换为灰度
# gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# gray_template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
#
# # 应用模板匹配
# result = cv2.matchTemplate(gray_image, gray_template, cv2.TM_CCOEFF_NORMED)
#
#
# # 定义一个阈值,用于筛选匹配结果
# threshold = 0.8
#
# # 使用np.where()函数找到满足阈值条件的位置
# locations = np.where(result >= threshold)
#
# print("locations",locations)
#
# # 在原图像中绘制边界框
# w, h = gray_template.shape[::-1]
# print("w",w)
# print("h",h)
# for pt in zip(*locations[::-1]):
# cv2.rectangle(image, pt, (pt[0]+w, pt[1]+h), (0, 255, 0), 2)
#
# # 显示匹配结果
# plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
# plt.title('Template Matching Result')
# plt.axis('off')
# plt.show()
import cv2
import numpy as np
# 读取目标图片和模板图片
img_rgb = cv2.imread('zuoqi.bmp')
template = cv2.imread('zuoqi3.bmp')
h, w = template.shape[:2]
# 将模板图片转换为灰度图
template_gray = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
img_gray = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2GRAY)
# 使用模版匹配
res = cv2.matchTemplate(img_gray, template_gray, cv2.TM_CCOEFF_NORMED)
# 设定阈值
threshold = 0.8
# 取匹配程度大于%80的坐标
loc = np.where(res >= threshold)
# print("loc",loc)
# loc (array([0], dtype=int64), array([0], dtype=int64))
# loc (array([], dtype=int64), array([], dtype=int64))
# print("*loc[::-1]",*loc[::-1])
# 根据坐标画矩形框,表示找到的模板位置
print("list(zip(*loc[::-1]))",list(zip(*loc[::-1])))
if len(list(zip(*loc[::-1]))) > 0:
print("图片匹配")
else:
print("图片不匹配")
# for pt in zip(*loc[::-1]):
# cv2.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0, 255, 0), 2)
#
# # 显示图片
# cv2.imshow('Matched Areas', img_rgb)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wzg_DaVinci/gametest.git
git@gitee.com:wzg_DaVinci/gametest.git
wzg_DaVinci
gametest
gametest
master

搜索帮助