python 大图找小图

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)
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Windows下使用Python进行大并返回位置的最快方法是使用OpenCV库。OpenCV是一个强大的计算机视觉库,可以处理像和视频的各种任务。 要使用OpenCV进行大的过程,可以按照以下步骤进行: 1. 导入所需的库和模块,包括cv2和numpy。 2. 加载大和小。使用cv2模块的imread函数读取大和小,存储在相应的变量中。 ```python large_image = cv2.imread('large_image.jpg') small_image = cv2.imread('small_image.jpg') ``` 3. 使用cv2模块的matchTemplate函数在大中寻。matchTemplate函数会在大上滑动给定大小的窗口,并计算给定窗口和小之间的匹配程度。 ```python result = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED) ``` 4. 到匹配程度最高的位置。使用cv2模块的minMaxLoc函数到result矩阵中的最小和最大值以及它们的位置。 ```python min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result) ``` 5. 返回小在大中的位置。可以使用max_loc变量得到小在大中的位置。 ```python small_image_width, small_image_height = small_image.shape[:2] top_left = max_loc bottom_right = (top_left[0] + small_image_width, top_left[1] + small_image_height) ``` 这是一个简单的使用OpenCV在Windows下进行大并返回位置的方法。通过利用OpenCV库的快速像处理和计算能力,可以在Windows环境下快速完成这个任务。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值