数字图像处理之模板匹配

问题: 找出图一中的缺陷


实验图
图二

使用模板匹配:


图三: template

def template_demo():

    tpl = cv2.imread("./test2.png")

    target = cv2.imread("./original.jpg")

    cv2.imshow("template image",tpl)

    cv2.imshow("target image",target)

    # methods = #[cv2.TM_SQDIFF_NORMED,cv2.TM_CCOEFF_NORMED,cv2.TM_CCORR_NORMED]  #各种#匹配算法

    methods = [cv2.TM_SQDIFF_NORMED]

    th,tw = tpl.shape[:2]

    for mdin methods:

        print(md)

        result = cv2.matchTemplate(target,tpl,md)

        min_val,max_val,min_loc,max_loc = cv2.minMaxLoc(result)

        if md == cv2.TM_SQDIFF_NORMED:

        l = min_loc

    else:

        tl = max_loc

        br = (tl[0]+tw,tl[1]+th)

        # y0 = tl[0]

        # x0 = tl[1]

        # y1 = br[0]

        # x1 = br[1]

        # img_target = target[x0:x1, y0:y1]

        # # img_target = target[10:20, 10:20]

        # cv2.imshow('crop', img_target)

        cv2.rectangle(target,tl,br,(0,0,255),2)

        cv2.imshow('math-'+np.str(md),target)

if __name__ =="__main__":

    template_demo()

    cv2.waitKey(0)

    cv2.destroyAllWindows()


效果图

   

你可能感兴趣的:(数字图像处理之模板匹配)