opencv打卡57: 零均值归一化交叉相关进行模式匹配

1、介绍
opencv打卡57: 零均值归一化交叉相关进行模式匹配_第1张图片

2、代码

import cv2
import numpy as np

img = cv2.imread('imori.jpg').astype(np.float32)
H, W, C = img.shape
mi = np.mean(img)

temp = cv2.imread('imori_part.jpg').astype(np.float32)
Ht, Wt, Ct = temp.shape
mt = np.mean(temp)

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)

# res = np.hstack((img, out))

cv2.imshow('res', out)
cv2.waitKey(0)
cv2.destroyAllWindows()


3、结果
opencv打卡57: 零均值归一化交叉相关进行模式匹配_第2张图片

你可能感兴趣的:(opencv打卡100题,opencv,计算机视觉)