我正在尝试检测一张图片是否与另一张图片完全匹配,并在此情况下将一个变量设置为True
。但是,我所读的所有内容都没有找到太多结果,除了一个特定的线程给出了以下代码。
import cv2
method = cv2.TM_SQDIFF_NORMED
# Read the images from the file
small_image = cv2.imread('ran_away.png')
large_image = cv2.imread('pokemon_card.png')
result = cv2.matchTemplate(small_image, large_image, method)
# We want the minimum squared difference
mn,_,mnLoc,_ = cv2.minMaxLoc(result)
# Draw the rectangle:
# Extract the coordinates of our best match
MPx,MPy = mnLoc
# Step 2: Get the size of the template. This is the same size as the match.
trows,tcols = small_image.shape[:2]
# Step 3: Draw the rectangle on large_image
cv2.rectangle(large_image, (MPx,MPy),(MPx+tcols,MPy+trows),(0,0,255),2)
# Display the original image with the rectangle around the match.
cv2.imshow('output',large_image)
# The image is only displayed if we call this
cv2.waitKey(0)
然而这会打开一个输出,执行我不想要的操作。我只想检测图像是否在图像中,并且如果是,则将其打印到控制台。在我的特定情况下,我正在尝试检测此图像
![Ran_away.png](https://istack.dev59.com/M7Oq6.webp)
![Pokemon_card.png](https://istack.dev59.com/pcSCR.webp)