简体   繁体   中英

How to convert grayscale png with transparency to heatmap?

The heatmap package isn't supported by Python 3, and cv2 doesn't support the following for PNG images with an alpha channel:

cv2.applyColorMap(img, cv2.COLORMAP_JET)

I want to convert a grayscale PNG image into a heatmap; in other words, colorize the darker pixels as blue and the brighter pixels as red.

Every pixel's transparency should remain unaffected.

Try using matplotlib.pyplot.get_cmap.

colormap = plt.get_cmap('plasma')
heatmap = (colormap(image) * 2**16).astype(np.uint16)[:,:,:3]
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_RGB2BGR)

You can choose the color maps according to your desired output.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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