简体   繁体   English

在图像顶部的热图

[英]Heatmap on top of image

I have Images of different objects (Pascal Voc) and I have a probability heatmap. 我有不同对象的图像(Pascal Voc),我有一个概率热图。 I want to visualize it by plotting the image and somehow the heatmap on top of it. 我想通过绘制图像以及在它上面以某种方式绘制热图来可视化它。 What would be the best way to do that? 最好的方法是什么?

I was thinking about using the alpha channel like this: 我正在考虑像这样使用alpha通道:

im_heat = np.zeros((image.shape[0],image.shape[1],4))
im_heat[:,:,:3] = image
im_heat[:,:,3] = np.rint(255/heatmap)
plt.imshow(im_heat, cmap='jet')
plt.colorbar()

How do I customize the colorbar to be from min(heatmap) to max(heatmap)? 如何自定义颜色条从最小(热图)到最大(热图)? Or is there any better way to visualize the probabilities? 或者有没有更好的方法可视化概率?

You can stack images and plots with matplotlib and then choose which handle to use for the colorbar. 您可以使用matplotlib堆叠图像和绘图,然后选择要用于颜色栏的句柄。 Using contourf the colorbar min and max values will be based on your heatmap (or you can pass vmin=min(heatmap) and vmax=max(heatmap) to contourf to be explicit about this range). 使用contourf的最小值和最大值将基于热图(或者您可以将vmin=min(heatmap)vmax=max(heatmap)传递给contourf,以明确该范围)。 The problem with this is the heatmap will cover you image (and setting transparency will make the entire thing transparent). 这个问题是热图将覆盖你的图像(设置透明度将使整个事物透明)。 The best option is to make a colormap which is transparent when near zero, as follows, 最好的选择是制作一个在接近零时透明的色彩图,如下所示,

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcolors
import Image

#2D Gaussian function
def twoD_Gaussian((x, y), xo, yo, sigma_x, sigma_y):
    a = 1./(2*sigma_x**2) + 1./(2*sigma_y**2)
    c = 1./(2*sigma_x**2) + 1./(2*sigma_y**2)
    g = np.exp( - (a*((x-xo)**2) + c*((y-yo)**2)))
    return g.ravel()


def transparent_cmap(cmap, N=255):
    "Copy colormap and set alpha values"

    mycmap = cmap
    mycmap._init()
    mycmap._lut[:,-1] = np.linspace(0, 0.8, N+4)
    return mycmap


#Use base cmap to create transparent
mycmap = transparent_cmap(plt.cm.Reds)


# Import image and get x and y extents
I = Image.open('./deerback.jpg')
p = np.asarray(I).astype('float')
w, h = I.size
y, x = np.mgrid[0:h, 0:w]

#Plot image and overlay colormap
fig, ax = plt.subplots(1, 1)
ax.imshow(I)
Gauss = twoD_Gaussian((x, y), .5*x.max(), .4*y.max(), .1*x.max(), .1*y.max())
cb = ax.contourf(x, y, Gauss.reshape(x.shape[0], y.shape[1]), 15, cmap=mycmap)
plt.colorbar(cb)
plt.show()

which gives, 这使,

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM