【问题标题】:Multi-scale Template Matching in real-time实时多尺度模板匹配
【发布时间】:2019-09-16 23:37:16
【问题描述】:

我能够从图片from this 进行模板匹配,并将其应用于实时,这意味着我循环遍历帧。但它似乎没有将模板与框架匹配,我意识到 found(bookkeeping variable) 总是 None。

import cv2 as cv2
import numpy as np
import imutils


def main():

    template = cv2.imread("C:\\Users\\Manthika\\Desktop\\opencvtest\\template.jpg")
    template = cv2.cvtColor(template, cv2.COLOR_BGR2GRAY)
    template = cv2.Canny(template, 50, 200)
    (tH, tW) = template.shape[:2]
    cv2.imshow("Template", template)

    windowName = "Something"
    cv2.namedWindow(windowName)
    cap = cv2.VideoCapture(0)

    if cap.isOpened():
        ret, frame = cap.read()
    else:
        ret = False

    # loop over the frames to find the template
    while ret:
        # load the image, convert it to grayscale, and initialize the
        # bookkeeping variable to keep track of the matched region
        ret, frame = cap.read()
        gray = cv2.cvtColor(frame, 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)

            # 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)
                print(found)

            # unpack the bookkeeping variable and compute the (x, y) coordinates
            # of the bounding box based on the resized ratio
        print(found) # here is the PROBLEM, found is always none.
        if found is None:
            # just show only the frames if the template is not detected
            cv2.imshow(windowName, frame)
        else:
            (_, 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(frame, (startX, startY), (endX, endY), (0, 0, 255), 2)
            cv2.imshow(windowName, frame)

        if cv2.waitKey(1) == 27:
            break

    cv2.destroyAllWindows()
    cap.release()


if __name__ == "__main__":
    main()

请帮我解决这个问题。

模板是:

【问题讨论】:

    标签: python opencv computer-vision nonetype


    【解决方案1】:

    这可能是你的问题:

    if resized.shape[0] < tH or resized.shape[1] < tW:
                    break
    

    您使用的模板为 743x887 像素,高于宽度。这种形状与大多数相机的输出不同,后者宽大于高。因此,如果您不使用 1080p 全高清摄像机,则您使用的摄像机的输出可能已经小于模板(高度)。这打破了循环并导致found 成为None。您应该在休息前使用打印语句检查这一点。

    解决方案是检查相机的分辨率并相应地将模板调整为更小的图像。

    更新:
    cmets中的额外问题。循环遍历所有模板,执行matchTemplate并比较相关值以获得最佳匹配模板。

    # create an array with template images and set variables
    templates = [templ1,templ2,templ3,templ4,templ5]
    curr_max = 0
    index = 0
    
    # find the best match
    for i in range(len(templates)):
        # perform matchtemplate
        res= cv2.matchTemplate(img,templates[i],cv2.TM_CCOEFF)
        # get the highest correlation value of the result
        maxVal = res.max()
        # if the correlation is highest thus far, store the value and index of template
        if maxVal > curr_max:
            curr_max = maxVal
            index = i
    
    # print index of template with highest correlation
    print(index)
    

    【讨论】:

    • 非常感谢@J.D.那是确切的问题,我解决了它:) 再次感谢您节省了我的时间。顺便说一句,我还可以知道如何使用超过 1 个模板,这意味着如何遍历我想要匹配的模板并找到最好的(具有最大相关值的)一个并返回它
    • 当然,我已经用代码更新了答案以找到最佳模板。不过,由于这个问题与你最初发帖的问题无关,所以下次最好再开一个新问题;)祝你好运!
    • 你知道吗?你是个神人。太感谢了。赞一个?
    猜你喜欢
    • 2021-10-06
    • 1970-01-01
    • 2019-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多