【发布时间】:2021-06-04 13:14:40
【问题描述】:
祝大家今天好, 我正在尝试创建一个热图,给定一组从 CSV 文件中提取的 x、y 坐标对。我正在使用 numpy.histogram2d 来绘制它,热图给了我正确的,但我需要将它覆盖在另一个图像上。我分享我的代码
Here 是 CSV
csv = pd.read_csv("../test2.csv")
#Gets the centroid of the coords
csv["x"] = (combined_csv["x_min"]+(combined_csv["x_max"]-combined_csv["x_min"])/2).astype(int)
csv["y"] = (combined_csv["y_min"]+(combined_csv["y_max"]-combined_csv["y_min"])/2).astype(int)
am = csv[(csv["tracking_id"] != 7) &(csv["tracking_id"] != 28)] #Some filters
#Taken from another question
def myplot(x, y, s, bins=1000):
heatmap, xedges, yedges = np.histogram2d(x, y, bins=bins, range=[[0, 960], [0, 480]])
heatmap = gaussian_filter(heatmap, sigma=s)
extent = [xedges[0], xedges[-1], yedges[0], yedges[-1]]
return heatmap.T, extent
img, extent = myplot(am["x"], am["y_max"], 16,200)
plt.imshow(img, extent=extent, origin='upper', cmap=cm.jet)
前面的代码给了我这张图片
我将 x 的范围设置为 0 到 480,y 设置为 0 到 960,因为目标图像具有该大小,但是这样做,histogram2d 会返回直方图矩阵 h,即 200x200,然后我使用 matplotlib 进行绘图它和这将我正确地绘制在 x 的 0-480 和 y 的 0-960 范围内,我需要保存所述图像,在 480x960 像素大小中保留尽可能多的信息,然后将其与目标图像相加使用本教程how to superimpose heatmap on a base image?
final_img = cv2.addWeighted (heatmap_img, 0.5, image, 0.5, 0)
【问题讨论】:
标签: python opencv matplotlib histogram2d