关于回归heatmap:根据每个点到中心线的距离:亮度与距离成反比
1.每个点到中心线的距离(dis)
ef getDis(pointX,pointY,lineX1,lineY1,lineX2,lineY2):
a=lineY2-lineY1
b=lineX1-lineX2
c=lineX2*lineY1-lineX1*lineY2
dis=(abs(a*pointX+b*pointY+c))/(pow(a*a+b*b,0.5))
return dis
# # Compute gaussian kernel
def CenterGaussianHeatMap(img_array, x1,x2, variance):
gaussian_map = np.zeros_like(img_array)
for y_p in range(img_array.shape[0]):
for x_p in range(img_array.shape[1]):
dist_sq = getDis(x_p, y_p,x1,0, x2,img_array.shape[1])
exponent = dist_sq / variance / variance * 2.5 # 因子2.5可进行调整
gaussian_map[y_p, x_p] = exp(-exponent)
return gaussian_map
2.由两个点确定直线的表达式,再求出点到直线的具体
点1(c_x1,0),点2(c_x2,img_array.shape[1])
def CenterLabelHeatMap(img_array1, c_x1, c_x2, sigma,path):
img_height = img_array1.shape[0]
img_width = img_array1.shape[1]
cv2.line(img_array1, (c_x1, 0), (c_x2, img_array1.shape[1]), (255, 0, 0), lineType=cv2.LINE_AA)
cv2.imwrite("/data/zmm/Cardiac/data/img1/test_img.png", img_array1) # 原图+回归线
lineY1 = 0
lineY2 = img_width
a = lineY2 - lineY1
b = c_x1 - c_x2
c = -(lineY2 - lineY1) * c_x1
X1 = np.linspace(1, img_width, img_width)
Y1 = np.linspace(1, img_height, img_height)
[X, Y] = np.meshgrid(X1, Y1)
dis = (abs(a * X + b * Y + c)) / (pow(a * a + b * b, 0.5))
exponent = dis / sigma / sigma * 2.5
heatmap = np.exp(-exponent.astype(float))
return heatmap
3.注意
https://blog.csdn.net/Fire_Light_/article/details/84974283
cv2.getGaussianKernel(size,sigma)可以生成一个一维的高斯核kernel,
二维的高斯核可以用kernel与kenel的转置求矩阵积得到。
kernel=cv2.getGaussianKernel(256,50)
kernel=kernel*kernel.T
#scales all the values and make the center vaule of kernel to be 1.0
kernel=kernel/np.max(kernel)
注意将矩阵的dtype转为uint8,不然opencv将无法显示图片。
cv2.applyColorMap()函数依据第二个参数可以将灰度图转化为不同风格的RGB图,这里我们选择cv2.COLORMAP_HOT。
heatmap=kernel*255
heatmap=heatmap.astype(np.uint8)
heatmap=cv2.applyColorMap(heatmap, cv2.COLORMAP_HOT)
cv2.imshow(‘heatmap’,heatmap)
cv2.waitKey(0)