简体   繁体   English

将RGB热图图像转换为灰度热图的正确方法

[英]Correct way for converting RGB heatmap image to Grayscale heatmap

I am trying to convert a RGB heatmap image to grayscale heatmap image. 我正在尝试将RGB热图图像转换为灰度热图图像。 First I thought It was a simple rgb to grayscale conversion. 首先,我认为这是将rgb转换为灰度的简单方法。 But it isn't. 但事实并非如此。

For example, blue color may represent soft things and red color may represent hard things. 例如,蓝色可以表示柔软的东西,红色可以表示硬的东西。

RGB热图

Using commonly used simple rgb to grayscale conversion method, I found red and blue color has converted to save gray color although they had very different nature of representation. 使用常用的简单的rgb到灰度转换方法,我发现红色和蓝色已转换为保存灰色,尽管它们的表示性质非常不同。

在此处输入图片说明

But What I want something like this where blue is deep gray, and red is bright gray . 但是我想要这样的东西,其中蓝色是深灰色,红色是亮灰色

在此处输入图片说明

I had searched a lot. 我搜了很多。 Unfortunately I did't find (or maybe I couldn't understand). 不幸的是我找不到(或者我听不懂)。 After reading some article on rgb color model, I have found a way to generate grayscale image. 阅读有关rgb颜色模型的文章后,我找到了一种生成灰度图像的方法。 My code is 我的代码是

import colorsys
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

img = mpimg.imread('input_image/abnormal_hi_res.png')
img = img[ : , : , 0:3] # keep only r, g, b channels  

new_image = np.zeros((img.shape[0], img.shape[1]))

for y_pos in range(img.shape[0]):
    for x_pos in range (img.shape[1]):

        color = img[y_pos, x_pos]
        r,g,b = color
        h, _, _ = colorsys.rgb_to_hls(r, g, b) 
        new_image[y_pos, x_pos] = 1.0 - h

plt.imshow(new_image, cmap='gray')
plt.show()

But I believe there should exists a good method backed by proven mathematics. 但是我相信应该存在一个由可靠的数学支持的好方法。
Please help me to find out the correct one for this problem. 请帮助我找出解决此问题的正确方法。

You can follow these links. 您可以点击这些链接。 They have got some good notes on heatmaps and grayscale 他们在热图和灰度上有一些很好的注释

https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html https://matplotlib.org/users/colormaps.html https://docs.opencv.org/3.1.0/de/d25/imgproc_color_conversions.html https://matplotlib.org/users/colormaps.html

*UPDATE *更新

First, you need to convert your BGR image to LUV then convert it to a grayscale image. 首先,您需要将BGR图像转换为LUV,然后将其转换为灰度图像。 Use opencv. 使用opencv。

Code for converting BGR to LUV in opencv. 在opencv中将BGR转换为LUV的代码。

gray = cv2.cvtColor(img, cv2.COLOR_BGR2LUV)

I think it what you are looking for 我认为这是您想要的

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

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