参考文献:
https://blog.csdn.net/zziahgf/article/details/79704788
https://zhuanlan.zhihu.com/p/37933909
https://blog.csdn.net/u013841196/article/details/85643310#comments
是真实标签的的坐标。控制该函数的左右移动情况
是热图的feature map 中每一个像素位置
:控制着该函数的胖瘦,也就是宽窄,涉及的范围多大,即径向范围
代码实现:
import time
import numpy as np
import cv2
import matplotlib.pyplot as plt
def CenterLabelHeatMap(img_width, img_height, c_x, c_y, sigma):
X1 = np.linspace(1, img_width, img_width)
Y1 = np.linspace(1, img_height, img_height)
[X, Y] = np.meshgrid(X1, Y1)
X = X - c_x
Y = Y - c_y
D2 = X * X + Y * Y
E2 = 2.0 * sigma * sigma
Exponent = D2 / E2
heatmap = np.exp(-Exponent)
return heatmap
#################另一种生成方法############
# Compute gaussian kernel
def CenterGaussianHeatMap(img_height, img_width, c_x, c_y, variance):
gaussian_map = np.zeros((img_height, img_width))
for x_p in range(img_width):
for y_p in range(img_height):
dist_sq = (x_p - c_x) * (x_p - c_x) + \
(y_p - c_y) * (y_p - c_y)
exponent = dist_sq / 2.0 / variance / variance
gaussian_map[y_p, x_p] = np.exp(-exponent)
return gaussian_map
image_file = 'test.jpg'
img = cv2.imread(image_file)
img = img[:,:,::-1]
height, width,_ = np.shape(img)
cy, cx = height/2.0, width/2.0
start = time.time()
heatmap1 = CenterLabelHeatMap(width, height, cx, cy, 21)
t1 = time.time() - start
start = time.time()
heatmap2 = CenterGaussianHeatMap(height, width, cx, cy, 21)
t2 = time.time() - start
print(t1, t2)
plt.subplot(1,2,1)
plt.imshow(heatmap1)
plt.subplot(1,2,2)
plt.imshow(heatmap2)
plt.show()
print('End.')