首页 > 编程语言 >python 大图找小图

python 大图找小图

时间:2023-02-01 12:02:58浏览次数:53  
标签:return img python 大图 self list 小图 template image


from pathlib import Path
import numpy
import cv2


class Image:
def __init__(self, image):
self.image = cv2.imread(image, cv2.IMREAD_UNCHANGED)

@property
def width(self):
return self.image.shape[1]

@property
def height(self):
return self.image.shape[0]


class MatchImg(object):
def __init__(self, source, template, threshod=0.95):
"""
匹配一个图片,是否是另一个图片的局部图。source是大图,template是小图。即判断小图是否是大图的一部分。
:param source:
:param template:
:param threshod: 匹配程度,值越大,匹配程度要求就越高,最好不要太小
"""
self.source_img = source
self.template_img = template
self.threshod = threshod

def match_template(self, method=cv2.TM_CCOEFF_NORMED):
"""
返回小图左上角的点,在大图中的坐标。
:param method:
:return: list[tuple(x,y),...]
"""
try:
result = cv2.matchTemplate(self.source_img.image, self.template_img.image, method)
locations = numpy.where(result >= self.threshod)
res = list(zip(locations[1], locations[0])) # 返回的是匹配到的template左上角那个坐标点在image中的位置,可能有多个值
return res
except cv2.error as e:
print(e)

def get_template_position(self):
"""
获取小图在大图中,左上角和右下角的坐标
:return: List[list[x,y,x,y],...]
"""
res = self.match_template()
new_pos = []
for r in res:
r = list(r)
r.append(r[0] + self.template_img.width)
r.append(r[1] + self.template_img.height)
new_pos.append(r)
return new_pos

def get_img_center(self):
"""
获取大图中,每个小图中心点所在的坐标
:return:
"""
pos = self.match_template()
points = []
for p in pos:
x, y = p[0] + int(self.template_img.width / 2), p[1] + int(self.template_img.height / 2)
points.append((x, y))
return points


def load_image_file(path):
path = Path(path)
if not path.exists():
print('not exist file')
try:
image = Image(str(path))
return image
except cv2.error as e:
print(e)






big ='1.png'
small ='3.png'
img1 = load_image_file(big)
img2 = load_image_file(small)

process = MatchImg(img1, img2, 0.95)
points = process.get_img_center()
_list = []
for i in points:
_list.append(str(i[0])+','+str(i[1]))

file_handle=open('1.txt',mode='w')
for i in _list:
file_handle.write('%sn' % i)


标签:return,img,python,大图,self,list,小图,template,image
From: https://blog.51cto.com/u_10780206/6031181

相关文章

  • python pyqt5简单界面
    ​​https://doc.qt.io/qtforpython/PySide6/QtWidgets/QTableWidget.html​​importsysfromPyQt5.QtWidgetsimportQApplication,QWidget,QDesktopWidget,QHBoxLayou......
  • python json to txt
    defread(self):file_path=os.path.join("db","alert.json")ifos.path.exists(file_path):file_object=open(file_path,mode='r',encodin......
  • python pyinstaller 打包方式介绍
    '''pipinstallpyinstaller单个pyinstaller-Fv3.py单个隐藏黑框pyinstaller-Fv3.py-w多个带很多文件pyinstaller-Dv3.py多个带很多文件隐藏黑框pyinstaller-Dv3......
  • Python经典题:找出1-9中有那些组合相加等于一个特定值,例如说20,一个列表中元素进行组合,
     找出1-9中有那些组合相加等于一个特定值,例如说20num=[1,2,3,4,5,6,7,8,9]defcount(num,n):#num=list(sorted(filter(lambdax:x<=n,num)))#pri......
  • python mongo查询
    importpymongo#连接数据库环境myclient1=pymongo.MongoClient('mongodb://账号:密码@ip:端口/')mydb1=myclient1["slot"]//dbmycol1=mydb1["ota.versions"]//表x=my......
  • python excel操作读取,写入
    importxlrd,xlwtfromxlutilsimportcopy#读取excel表格某个数据data=xlrd.open_workbook("select125.xls")tablerd=data.sheet_by_name("Sheet1")rowNum=tablerd......
  • 跟着廖雪峰学 python 002
    ​ ​编辑 #表示注释:表示缩进的语句是代码块(缩进一般是四个空格)数据类型整数:        在程序中的表示方法和数学上的写法一模一样(正整数和负整数) ......
  • 详解如何用 C 为 Python 实现扩展模块
    当Python代码的执行效率不高时,我们会选择将性能相关的部分交给C来实现,但这要求开发者必须熟悉Python提供的CAPI。为此我专门写了一个系列,介绍如何用C给Python......
  • Python发送邮件脚本
    目的:将Python执行脚本结果发送到指定邮箱 1、以下163邮箱为例,设置发件人是163邮箱,接收人是qq邮箱。由于163邮箱的安全机制,Python登陆163邮件客户端不是使用邮箱密码而......
  • 【Python】生成 gif图片
    draw_gif.pyimportosimportioimportimghdrimportimageio.v2asimageiofromPILimportImage,ImageDraw,ImageFontimportnumpyasnpfromPILimportImag......