【问题标题】:Multi-Scale template Matching doesn't work right多尺度模板匹配无法正常工作
【发布时间】:2019-05-03 18:06:35
【问题描述】:

我通过查看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)

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

【问题讨论】:

    标签: python opencv computer-vision template-matching


    【解决方案1】:

    参考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")
    

    【讨论】:

    • 你能解释一下吗?
    • 函数cv2.MinMaxLoccv2.matchTemplate的相似度图中找到最小和最大元素值及其位置,其中maxLoc告诉你匹配区域的左上角在哪里,maxVal告诉你比赛有多好。您发布的代码将为每个图像绘制一个边界框,因为found 始终会更新。但是,如果您可以在绘制框之前将 maxVal 与阈值进行比较,您将删除误报匹配图像。
    • 刚刚做了。顺便说一句,这种方法计算量很大,因为它需要多次缩放图像或模板。您是否考虑过 SIFT,其中 SI 表示尺度不变?
    • 你的意思是特征匹配对吧?我仍在学习它,这将是我进行相同实施的下一步。非常感谢:)你帮了大忙。如果有任何关于特征匹配的好文章,请分享。干杯!
    猜你喜欢
    • 2019-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-06
    • 2011-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-04
    相关资源
    最近更新 更多