将RGB热图图像转换为灰度热图的正确方法 [英] Correct way for converting RGB heatmap image to Grayscale heatmap

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

问题描述

我正在尝试将RGB热图图像转换为灰度热图图像.首先,我认为这是一个简单的 rgb到灰度转换.但事实并非如此.

I am trying to convert a RGB heatmap image to grayscale heatmap image. First I thought It was a simple rgb to grayscale conversion. 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.

但是我想要这样的东西,其中蓝色是深灰色,红色是亮灰色.

But What I want something like this where blue is deep gray, and red is bright gray.

我搜索了很多东西.不幸的是我没有找到(或者也许我听不懂).阅读有关rgb颜色模型的文章后,我找到了一种生成灰度图像的方法.我的代码是

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. 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

* UPDATE

首先,您需要将BGR图像转换为LUV,然后将其转换为灰度图像.使用opencv.

First, you need to convert your BGR image to LUV then convert it to a grayscale image. Use opencv.

在opencv中将BGR转换为LUV的代码.

Code for converting BGR to LUV in opencv.

灰色= cv2.cvtColor(img,cv2.COLOR_BGR2LUV)

我认为这是您想要的

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

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