TypeError: an integer is required (got type list)的一种解决方案 - Cache One

我的问题:
图1在这里插入图片描述
图1
我的代码:

import numpy as np
import cv2 as cv

def template_image_demo():
    target = cv.imread("D:\wenjian\GraduateCourse\DigitalImageProcessing\code-python\image\cobe1.jpg")
    tpl = cv.imread("D:\wenjian\GraduateCourse\DigitalImageProcessing\code-python\image\cobe3Cut.jpg")
    cv.imshow("template", tpl)
    cv.imshow("target", target)
    methods = [cv.TM_SQDIFF_NORMED, cv.TM_CCORR_NORMED, cv.TM_CCOEFF_NORMED]
    th, tw = tpl.shape[:2]
    print("th = ", th)
    print("tw = ", tw)
    for md in methods:
        result = cv.matchTemplate(target, tpl, md)
        min_val, max_val, min_loc, max_loc = cv.minMaxLoc(result)
        print("min_val = ", min_val)
        print("max_val = ", max_val)
        print("min_loc = ", min_loc)
        print("max_loc = ", max_loc)
        if md == cv.TM_SQDIFF_NORMED:
            if min_val > 0.5:
                print("SQDIFF图像不匹配")
                tl = [0, 0]
            else:
                tl = min_loc
        else:
            if max_val < 0.5:
                print("图像不匹配")
                tl = [0, 0]
            else:
                tl = max_loc

	br = (tl[0] + tw, tl[1] + th)
        cv.rectangle(target, tl, br, [0, 0, 255], 3)
        cv.imshow("match"+np.str(md), target)
        template_image_demo()
	cv.waitKey(0)
	cv.destroyAllWindows()

``解决:
		tl = (0, 0)
从图一可以看出tl = min_loc 的值都是圆括号中放值,即()
而我代码中的tl = [0, 0]却是用中括号,因此犯错。






为您推荐