2017-02-27 57 views
4

我有不同的對象(Pascal Voc)圖像,我有一個概率熱圖。我想通過繪製圖像並以某種方式將熱圖繪製在其上來對其進行可視化。什麼是最好的方式來做到這一點?在圖像頂部的熱圖

我想使用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() 

如何自定義顏色條從分鐘(熱圖)到最大(熱圖)? 或者有沒有更好的方法來可視化概率?

回答

5

您可以用matplotlib堆疊圖像和圖形,然後選擇用於色條的哪個手柄。使用contourf顏色條的最小值和最大值將基於您的熱圖(或者您可以通過vmin=min(heatmap)vmax=max(heatmap)輪廓線來明確該範圍)。這個問題是熱圖會覆蓋你的形象(並且設置透明度會使整個事物變得透明)。最好的辦法是做一個顏色表這是當接近零透明,如下,

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() 

賦予,

enter image description here