数据

图像、热力图
图像和热力图合成_数据

热力图生成以及与图像合成代码

根据自己点击的位置,生成热力图
图像和热力图合成_json_02
图像和热力图合成_数据_03

import cv2
import matplotlib.pyplot as plt
import numpy as np

def make_gaussian(size, sigma=10, center=None):
""" Make a square gaussian kernel.
size: is the dimensions of the output gaussian
sigma: is full-width-half-maximum, which
can be thought of as an effective radius.
"""

x = np.arange(0, size[1], 1, float)
y = np.arange(0, size[0], 1, float)
y = y[:, np.newaxis]

if center is None:
x0 = y0 = size[0] // 2
else:
x0 = center[0]
y0 = center[1]

return np.exp(-4 * np.log(2) * ((x - x0) ** 2 + (y - y0) ** 2) / sigma ** 2)


def make_gt(img, points, outputRes=None, sigma=10):
""" Make the ground-truth for each landmark.
img: the original color image
labels: the json labels with the Gaussian centers {'x': x, 'y': y}
sigma: sigma of the Gaussian.
"""

if outputRes is not None:
h, w = outputRes
else:
h, w = img.shape
# print (h, w, len(labels))
#gt = np.zeros((h, w, len(labels)), np.float32)
gt = np.zeros((h, w, 1), np.float32)

for p in range(0, len(points)):
gt[:,:,0] = gt[:,:,0] + (make_gaussian((h, w), sigma, points[p]))
return np.squeeze(gt*255).astype(np.uint8)


def easy_show(data, heatmap, outPath):
plt.figure()
plt.imshow(data, cmap='bone')
plt.imshow(heatmap, cmap='rainbow', alpha=0.4)
plt.savefig(outPath)


if __name__ == '__main__':
imagePath = r'D:\2022\2\1.png'
img2 = cv2.imread(imagePath)
img = cv2.imread(imagePath, 0)
print(img.shape)

a = []
b = []


def on_EVENT_LBUTTONDOWN(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
xy = "%d,%d" % (x, y)
a.append(int(x))
b.append(int(y))
cv2.circle(img2, (x, y), 1, (0, 0, 255), thickness=-1)
cv2.putText(img2, xy, (x, y), cv2.FONT_HERSHEY_PLAIN,
1.0, (0, 0, 0), thickness=1)
cv2.imshow("image", img2)

cv2.namedWindow("image")
cv2.setMouseCallback("image", on_EVENT_LBUTTONDOWN)
cv2.imshow("image", img2)
cv2.waitKey(0)

points = []
for x, y in zip(a, b):
points.append([x, y])

gt = make_gt(img, points, outputRes=None, sigma=80)

easy_show(img2, gt, 'hm.png')