使用时
gocv
例如,可以
template matching
MinMaxLoc
函数检索矩阵中最小值和最大值的位置。
但是,在下面的python示例中,作者使用
numpy.Where
对矩阵进行阈值化,得到多个极大值的位置。巨蟒
zip
函数用于将值粘合在一起,使它们像切片一样
[][2]int
,内部切片是找到的匹配项的xs和ys。
loc[::-1]
reverses
阵列。
zip(*loc..)
用于解包给zip的切片。
https://docs.opencv.org/master/d4/dc6/tutorial_py_template_matching.html
import cv2 as cv
import numpy as np
from matplotlib import pyplot as plt
img_rgb = cv.imread('mario.png')
img_gray = cv.cvtColor(img_rgb, cv.COLOR_BGR2GRAY)
template = cv.imread('mario_coin.png',0)
w, h = template.shape[::-1]
res = cv.matchTemplate(img_gray,template,cv.TM_CCOEFF_NORMED)
threshold = 0.8
loc = np.where( res >= threshold)
for pt in zip(*loc[::-1]):
cv.rectangle(img_rgb, pt, (pt[0] + w, pt[1] + h), (0,0,255), 2)
cv.imwrite('res.png',img_rgb)
np.where
Go中的算法在应用阈值后获取多个位置?