【图像处理】模板匹配的学习笔记

OpenCV的模板匹配算法

  • cv.TM_CCOEFF
  • cv.TM_CCOEFF_NORMED
  • cv.TM_CCORR
  • cv.TM_CCORR_NORMED
  • cv.TM_SQDIFF
  • cv.TM_SQDIFF_NORMED

匹配代码模板

python 复制代码
image = cv2.imread(r"scene.png", cv2.IMREAD_GRAYSCALE)
template = cv2.imread(r"element.png", cv2.IMREAD_GRAYSCALE)
res = cv2.matchTemplate(img, template, cv2.TM_CCOEFF_NORMED)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(res)

if max_val > 0.8:
    print("Detected")
else:
    print("Not detected")

Note:

  1. cv2.TM_CCOEFF_NORMED:相较于其它方法,通常被认为具有较好的鲁棒性
相关推荐
master cat7 个月前
子串匹配--------c++
c++·匹配·子串