opencv-如何在不缩放的情况下进行模板匹配?

时间:2019-07-21 18:42:04

标签: python opencv

我正在尝试匹配纸牌。我认为由于卡片都是唯一的,因此模板匹配可能是正确的选择。

我的文件夹中有templates(图像),这些只是卡。

现在,当我尝试将它们与图片中的几张卡片和桌子进行匹配时,我在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()

编辑:

已接受的答案完成了任务。

我使用了不同的方法,因为我的用例是特定的,因此我可以更改获取template图像和test image的位置的比例

我正在使用以下命令来确保比例保持不变。 (Ubuntu,终端命令)

# Install wmctrl
sudo apt-get install wmctrl
# Command to resize the window
wmctrl -r string -e 0,left,up,width,height

这来自一个答案:here

1 个答案:

答案 0 :(得分:3)

您应创建参考图像的金字塔,请参见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)