要在大图片中识别一张小图片的位置,可以使用Python的图像处理库OpenCV来实现。下面是一个简单的代码示例:

import cv2

def find_image_location(small_image_path, large_image_path):
    # 读取小图片和大图片
    small_image = cv2.imread(small_image_path, cv2.IMREAD_GRAYSCALE)
    large_image = cv2.imread(large_image_path, cv2.IMREAD_GRAYSCALE)

    # 使用模板匹配方法进行图像匹配
    result = cv2.matchTemplate(large_image, small_image, cv2.TM_CCOEFF_NORMED)
    min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)

    # 获取小图片在大图片中的位置
    top_left = max_loc
    bottom_right = (top_left[0] + small_image.shape[1], top_left[1] + small_image.shape[0])

    return top_left, bottom_right

# 调用函数查找小图片在大图片中的位置
small_image_path = 'small_image.jpg'
large_image_path = 'large_image.jpg'
top_left, bottom_right = find_image_location(small_image_path, large_image_path)

# 在大图片上画出小图片的边框
large_image = cv2.imread(large_image_path)
cv2.rectangle(large_image, top_left, bottom_right, (0, 255, 0), 2)

# 展示结果
cv2.imshow('Result', large_image)
cv2.waitKey(0)
cv2.destroyAllWindows()

请确保将代码中的small_image.jpglarge_image.jpg替换为实际的小图片和大图片的路径。此代码将在大图片上绘制出小图片的边框,并显示结果。

Python OpenCV: 在大图片中查找小图片位置 - 代码示例

原文地址: https://www.cveoy.top/t/topic/pp4Z 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录