实时多尺度模板匹配

问题描述 投票:1回答:1

我能够从图片from this进行模板匹配,并将其应用于实时,这意味着我循环遍历框架。但它似乎没有将模板与框架匹配,而且我意识到找到(簿记变量)总是无。

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()

请帮我解决这个问题。

模板是:

enter image description here

python opencv computer-vision nonetype
1个回答
1
投票

这可能是你的问题:

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

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

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

更新: 评论中的额外问题。遍历所有模板,执行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)
© www.soinside.com 2019 - 2024. All rights reserved.