【问题标题】:How to see if an image is contained in another image image and get an output of 1 if there is or 0 if there isn't?如何查看一个图像是否包含在另一个图像图像中,如果有则输出 1,如果没有则输出 0?
【发布时间】:2021-07-24 15:47:51
【问题描述】:

我有一个截取屏幕截图的脚本,我想看看其中是否有较小的图像。我目前正在使用 pyAutoGui 截屏。我知道我可以使用 open-cv 但我不知道如何将其转换为布尔值,例如当检测到较小的图像时给出 1 或类似 True 而不是给出 0 或 False 时。谢谢!

【问题讨论】:

  • 请发布示例图片的链接。

标签: python opencv pyautogui


【解决方案1】:

您可以使用cv2.matchTemplate 方法:

import cv2
import numpy as np

img1 = cv2.imread("image1.png")
img2 = cv2.imread("image2.png")

img1_gray = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
img2_gray = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)

res = cv2.matchTemplate(img1_gray, img2_gray, cv2.TM_CCOEFF_NORMED)
if np.any(res > 0.9):
    print("Image2 is in Image1.")

0.9 是否表示将图像分类为其他图像内部所需的置信度。为了更准确,您可以这样做:

if np.any(res > 0.99):

【讨论】:

    猜你喜欢
    • 2014-03-15
    • 2021-09-27
    • 1970-01-01
    • 2015-03-16
    • 1970-01-01
    • 2010-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    相关资源
    最近更新 更多