如何使用 Python OpenCV 将灰度图像转换为热图图像 [英] How to convert a grayscale image to heatmap image with Python OpenCV

查看:205
本文介绍了如何使用 Python OpenCV 将灰度图像转换为热图图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 (540, 960, 1) 形状的图像,其值范围为黑白的 [0..255].我需要将其转换为热图"表示.例如,具有 255 的像素应该是最热的,而具有 0 的像素应该是最不热的.其他介于两者之间.我还需要将热图作为 Numpy 数组返回,以便稍后将它们合并到视频中.有没有办法做到这一点?

解决方案

这里有两种方法,一种使用Matplotlib,一种只使用OpenCV

方法#1: OpenCV +

我们可以使用 OpenCV 内置的热图功能.这是使用 cv2.COLORMAP_HOT 热图

的结果

代码

导入 cv2图像 = cv2.imread('frame.png', 0)热图 = cv2.applyColorMap(图像,cv2.COLORMAP_HOT)cv2.imshow('热图', 热图)cv2.waitKey()


注意:虽然 OpenCV 的内置实现简短快捷,但我建议使用方法 #1,因为有更大的颜色图选择.Matplotlib 有 数百种不同的颜色图并允许您在 OpenCV 时创建自己的自定义颜色图只有12个可供选择.这是内置的 OpenCV 颜色图选择:

I have a (540, 960, 1) shaped image with values ranging from [0..255] which is black and white. I need to convert it to a "heatmap" representation. As an example, pixels with 255 should be of most heat and pixels with 0 should be with least heat. Others in-between. I also need to return the heat maps as Numpy arrays so I can later merge them to a video. Is there a way to achieve this?

解决方案

Here are two methods, one using Matplotlib and one using only OpenCV

Method #1: OpenCV + matplotlib.pyplot.get_cmap

To implement a grayscale (1-channel) -> heatmap (3-channel) conversion, we first load in the image as grayscale. By default, OpenCV reads in an image as 3-channel, 8-bit BGR. We can directly load in an image as grayscale using cv2.imread() with the cv2.IMREAD_GRAYSCALE parameter or use cv2.cvtColor() to convert a BGR image to grayscale with the cv2.COLOR_BGR2GRAY parameter. Once we load in the image, we throw this grayscale image into Matplotlib to obtain our heatmap image. Matplotlib returns a RGB format so we must convert back to Numpy format and switch to BGR colorspace for use with OpenCV. Here's a example using a scientific infrared camera image as input with the inferno colormap. See choosing color maps in Matplotlib for available built-in colormaps depending on your desired use case.

Input image:

Output heatmap image:

Code

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

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

cv2.imshow('image', image)
cv2.imshow('heatmap', heatmap)
cv2.waitKey()

Method #2: cv2.applyColorMap()

We can use OpenCV's built in heatmap function. Here's the result using the cv2.COLORMAP_HOT heatmap

Code

import cv2

image = cv2.imread('frame.png', 0)
heatmap = cv2.applyColorMap(image, cv2.COLORMAP_HOT)

cv2.imshow('heatmap', heatmap)
cv2.waitKey()


Note: Although OpenCV's built-in implementation is short and quick, I recommend using Method #1 since there is a larger colormap selection. Matplotlib has hundreds of various colormaps and allows you to create your own custom color maps while OpenCV only has 12 to choose from. Here's the built in OpenCV colormap selection:

这篇关于如何使用 Python OpenCV 将灰度图像转换为热图图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆