在这里我们使用零均值归一化交叉相关进行模式匹配。将imori_part.jpg
在imori.jpg
中匹配的图像使用红框框出来。
零均值归一化交叉相关(Zero-mean Normalization Cross Correlation,简称ZNCC)求出两个图像的相似度,匹配S最大处的图像。
图像I的平均值记为m_i,图像T的平均值记为m_t。使用下式计算S:
S=\frac{\sum\limits_{x=0}^w\ \sum\limits_{y=0}^h\ |[I(i+x,j+y)-m_i]\ [T(x,y)-m_t]}{\sqrt{\sum\limits_{x=0}^w\ \sum\limits_{y=0}^h\ [I(i+x,j+y)-m_i]^2}\ \sqrt{\sum\limits_{x=0}^w\ \sum\limits_{y=0}^h\ [T(x,y)-m_t]^2}}
S最后的范围在-1\leq S\leq 1。零均值归一化积相关去掉平均值的话就是归一化交叉相关,据说这比归一化交叉相关对变换更加敏感。
python实现:
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Read image
img = cv2.imread("imori.jpg").astype(np.float32)
H, W, C = img.shape
mi = np.mean(img)
# Read templete image
temp = cv2.imread("imori_part.jpg").astype(np.float32)
Ht, Wt, Ct = temp.shape
mt = np.mean(temp)
# Templete matching
i, j = -1, -1
v = -1
for y in range(H-Ht):
for x in range(W-Wt):
_v = np.sum((img[y:y+Ht, x:x+Wt]-mi) * (temp-mt))
_v /= (np.sqrt(np.sum((img[y:y+Ht, x:x+Wt]-mi)**2)) * np.sqrt(np.sum((temp-mt)**2)))
if _v > v:
v = _v
i, j = x, y
out = img.copy()
cv2.rectangle(out, pt1=(i, j), pt2=(i+Wt, j+Ht), color=(0,0,255), thickness=1)
out = out.astype(np.uint8)
# Save result
cv2.imwrite("out.jpg", out)
cv2.imshow("result", out)
cv2.waitKey(0)
cv2.destroyAllWindows()
输入(imori.jpg):
template图像(imori_part.jpg):
输出: