我正在尝试匹配扑克牌。我认为由于卡片都是独一无二的,模板匹配可能是正确的方法。
我的文件夹中有模板
(图像),这些只是卡片。
现在,当我尝试将它们与图片中的几张卡片和表格进行匹配时,我在 threshold = 0.8
处得到 0 个匹配。
我查了一下,这似乎是一个规模问题。也就是说,如果我理解正确,如果卡片图片(模板)与我想要检测卡片的比例不同,那么它将不会被检测到。
我不知道如何从这里继续。
这是我正在使用的代码。
mport pyautogui
import cv2
import numpy as np
import time
import pyscreenshot as grabimage
import os
img_de = cv2.imread('/media/xxx/cards/match2.jpg')
img_gray = cv2.cvtColor(img_de,cv2.COLOR_BGR2GRAY)
os.chdir('/media/xxx/cards/template-for-matching/')
templates = os.listdir()
# templates = ['9s.jpg']
for template in templates:
print('checking: ' + str(template))
t = cv2.imread(template,0)
w,h = t.shape[::-1]
res = cv2.matchTemplate(img_gray,t,cv2.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where(res >= threshold)
for pt in zip(*loc[::-1]):
cv2.rectangle(img_de, pt, (pt[0]+w, pt[1]+h),(0,255,255),1)
cv2.imshow('detected',img_de)
cv2.waitKey(0)
input('Wait')
cv2.destroyAllWindows()
编辑:
接受的答案可以完成这项工作。
我采用了不同的方法,因为我的用例是特定的,我可以更改获取模板
图像和测试图像
的比例
我正在使用以下命令来确保比例保持不变。 (Ubuntu,终端命令)
# Install wmctrl
sudo apt-get install wmctrl
# Command to resize the window
wmctrl -r string -e 0,left,up,width,height
这来自一个答案:here
最佳答案
您应该创建引用图像的金字塔,请参阅 this official opencv tutorial 。然后,您可以在代码中添加一个外循环,以循环遍历所有图像尺寸。在此金字塔中,您采用最强匹配的模板并设置该匹配的阈值。
查看代码取自this tutorial :
# loop over the images to find the template in
for imagePath in glob.glob(args["images"] + "/*.jpg"):
# load the image, convert it to grayscale, and initialize the
# bookkeeping variable to keep track of the matched region
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
found = None
# loop over the scales of the image
for scale in np.linspace(0.2, 1.0, 20)[::-1]:
# resize the image according to the scale, and keep track
# of the ratio of the resizing
resized = imutils.resize(gray, width = int(gray.shape[1] * scale))
r = gray.shape[1] / float(resized.shape[1])
# if the resized image is smaller than the template, then break
# from the loop
if resized.shape[0] < tH or resized.shape[1] < tW:
break
# detect edges in the resized, grayscale image and apply template
# matching to find the template in the image
edged = cv2.Canny(resized, 50, 200)
result = cv2.matchTemplate(edged, template, cv2.TM_CCOEFF)
(_, maxVal, _, maxLoc) = cv2.minMaxLoc(result)
# check to see if the iteration should be visualized
if args.get("visualize", False):
# draw a bounding box around the detected region
clone = np.dstack([edged, edged, edged])
cv2.rectangle(clone, (maxLoc[0], maxLoc[1]),
(maxLoc[0] + tW, maxLoc[1] + tH), (0, 0, 255), 2)
cv2.imshow("Visualize", clone)
cv2.waitKey(0)
# if we have found a new maximum correlation value, then update
# the bookkeeping variable
if found is None or maxVal > found[0]:
found = (maxVal, maxLoc, r)
# unpack the bookkeeping variable and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
(_, maxLoc, r) = found
(startX, startY) = (int(maxLoc[0] * r), int(maxLoc[1] * r))
(endX, endY) = (int((maxLoc[0] + tW) * r), int((maxLoc[1] + tH) * r))
# draw a bounding box around the detected result and display the image
cv2.rectangle(image, (startX, startY), (endX, endY), (0, 0, 255), 2)
cv2.imshow("Image", image)
cv2.waitKey(0)
关于python - opencv - 如何在没有比例的情况下进行模板匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57136024/