Python:实现大图里找小图

一:依赖库

numpy

CV2

二:源码

import cv2
import numpy


def match_in(big_photo, small_photo):
    # 读取大图和小图
    big_image = cv2.imread(big_photo)
    small_image = cv2.imread(small_photo)
    # 设置匹配阈值
    threshold = 0.8
    # 在大图中查找小图
    result = cv2.matchTemplate(big_image, small_image, cv2.TM_CCOEFF_NORMED)
    # 使用numpy的where函数找到满足阈值条件的匹配结果
    locations = numpy.where(result >= threshold)
    # 遍历所有匹配结果的坐标
    for point in zip(*locations[::-1]):
        print("匹配成功")
    # 释放图像资源
    cv2.destroyAllWindows()


if __name__ == '__main__':
    big_photo = r"E:\report\1.png"
    small_photo = r"E:\report\2.png"
    match_in(big_photo, small_photo)

三:运行结果

D:\2_Python\python.exe D:\3_PythonWorkSpace\test\match_in.py 
匹配成功
匹配成功
匹配成功
匹配成功
匹配成功
匹配成功
匹配成功

Process finished with exit code 0

四:图片展示

Python:实现大图里找小图_第1张图片

你可能感兴趣的:(python,opencv,开发语言)