实时多尺度模板匹配

时间:2019-04-27 21:00:12

标签: python opencv computer-vision nonetype

我能够从图片from this中进行模板匹配,并将其应用于实时,这意味着我遍历了帧。但这似乎与模板与框架不匹配,而且我意识到found(簿记变量)始终为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()

请帮助我解决这个问题。

模板为:

enter image description here

1 个答案:

答案 0 :(得分:1)

这可能是您的问题:

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

您使用的模板为743x887像素,比宽高。这种形状不同于大多数相机输出的形状,后者的宽度大于高度。因此,如果您未使用1080p全高清摄像头,则您使用的摄像头的输出可能已经小于模板(高度)。这会中断循环,并使foundNone。休息之前,您应该使用打印语句进行检查。

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

更新
评论中的其他问题。遍历所有模板,执行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)