多尺度模板匹配不正确

Multi-Scale template Matching doesn't work right

我通过查看 this article 实时完成了多尺度模板匹配。当模板出现在框架中时,它会检测到它并在其周围绘制一个边界框,这意味着它可以正常工作。 但是当框架中也没有模板时,它会检测某处并绘制边界框。我会提到我发现的代码和错误。

import cv2 as cv2
import numpy as np
import imutils


def main():

    template1 = cv2.imread("C:\Users\Manthika\Desktop\opencvtest\template.jpg")
    template1 = cv2.cvtColor(template1, cv2.COLOR_BGR2GRAY)
    template1 = cv2.Canny(template1, 50, 200)
    template = imutils.resize(template1, width=60)
    (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:
                print("frame is smaller than the template")
                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)

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

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

我认为问题出在这两行,

if found is None or maxVal > found[0]:
                found = (maxVal, maxLoc, r)

找到的变量总是更新为一个值,即使它是 none。我是计算机视觉的新手,所以请善待并帮助我解决这个问题。如果我需要提及任何其他内容,也请告诉我。谢谢。

参考How do I use OpenCV MatchTemplate?:

在您的代码中,您有 (_, maxVal, _, maxLoc) = cv2.minMaxLoc(result),它应该是 minVal,maxVal,minLoc,maxLoc = cv.MinMaxLoc(result),您需要设置 minVal 的阈值来过滤不匹配的结果。

示例:

# 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)
    (minVal, maxVal, _, maxLoc) = cv2.minMaxLoc(result)

    # if we have found a new maximum correlation value, then ipdate
    # the bookkeeping variable
    if found is None or maxVal > found[0]:
        found = (maxVal, maxLoc, r)

# unpack the bookkeeping varaible and compute the (x, y) coordinates
# of the bounding box based on the resized ratio
(maxVal, maxLoc, r) = found
# Threshold setting, this 11195548 value is tested by some random images
threshold = 11195548
if maxVal > threshold:
    print("match 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)
else:
    print("no match found")