Jet colormap为灰度

时间:2011-09-16 05:01:43

标签: matlab colors rgb grayscale

我有一个喷墨色彩图:

jet colormap

我想知道是否有某种方法可以转换为灰度。我无法使用平均值,因为最大值和最小值会变为相同的灰色。或者,如果有某种方法可以转换为另一种调色板。

我无法在Google上找到转换它的功能。 MATLAB使用了一些名为rgb2ind的东西,但我想知道公式。

4 个答案:

答案 0 :(得分:9)

首先让我使用Jet colormap创建一个索引图像:

img = repmat(uint8(0:255), 100, 1);
cmap = jet(256);

imshow(img, 'Colormap',cmap)

jet

使用IND2GRAY进行直接转换会产生以下结果:

J = ind2gray(img,cmap);
imshow(J)

jet_to_gray

如您所述,最小值/最大值收敛到相同的值。根据我的理解,您希望将喷射色图映射为从深色到浅色的灰色线性。为此,我们可以使用RGB2HSV函数获得reorder using the hue value。将以下内容与原始色彩映射进行比较:

[~,idx] = sortrows(rgb2hsv(cmap), -1);  %# sort by Hue
C = gray(256);
C = C(idx,:);

imshow(img, 'Colormap',C)

hue_sorted

答案 1 :(得分:3)

在MATLAB中创建图像

image(1:64);axis off;colormap(jet);

将图像保存为tiff,使用画笔裁剪边框并另存为'\ directorypath \ jetmage.tiff'。

在MATLAB中加载图像

jetmage=imread('\\directorypath\jetmage.tiff');

获取图像尺寸

[szX,szY,szZ]=size(jetmage);

为每种颜色(红色,绿色和蓝色)获取一行图像。

r=reshape(jetmage(uint8(szX/2),:,1),[szY,1]);
g=reshape(jetmage(uint8(szX/2),:,2),[szY,1]);
b=reshape(jetmage(uint8(szX/2),:,3),[szY,1]);

绘制该行每种颜色的强度分布图。

plot(r,'r-');hold on;
plot(g,'g-');hold on;
plot(b,'b-');hold on;

情节应该是这样的:

enter image description here

您可以使用数组[r,g,b]作为查找表或基础 找出一种从数组[r,g,b]

获取'公式'的方法

答案 2 :(得分:0)

rgb2ind将每个像素的RGB值转换为颜色映射中的索引。如果您使用带有颜色映射输入的2输入版本,则它将查找颜色映射中与每个像素匹配的最接近颜色。这基本上会为每个像素提供一个数字,而不是RGB值。

例如,如果您加载图片

RGB = imread(imagefilename);

然后,由于jet返回了Jet颜色映射,因此您可以使用

获取索引数据
mapsize = 256;
map = jet(mapsize);
ind = rgb2ind(RGB, map);

然后,您可以使用任何颜色映射

显示图像
colormap(map)
image(ind)
colormap('gray')

不要使用imagesc,因为它可能无法接受地延长图像的动态范围。

答案 3 :(得分:0)

teng给出的答案的Python实现(假设默认的matplotlib jetmap)。

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


def PIL2array(img):
    return numpy.array(img.getdata(),
                numpy.uint8).reshape(img.size[1], img.size[0], 3)
def array2PIL(arr, size):
    mode = 'RGBA'
    arr = arr.reshape(arr.shape[0]*arr.shape[1], arr.shape[2])
    if len(arr[0]) == 3:
       arr = numpy.c_[arr, 255*numpy.ones((len(arr),1), numpy.uint8)]
    return Image.frombuffer(mode, size, arr.tostring(), 'raw', mode, 0, 1)

def return_x(y,color,direction):

    if color == "blue" and direction == "up":
       m = (4.0 + 6.0/11.0)
       c = 0.5
    elif color == "blue" and direction == "down":
       m = -3.226
       c = 2.097
    elif color == "green" and direction == "up":
       m = 4.0
       c = -0.5
    elif color == "green" and direction == "down":
       m = -3.704
       c = 3.370
    elif color == "red" and direction == "up":
       m = 3.223
       c = -1.129
    elif color == "red" and direction == "down":
       m = -4.545
       c = 5.041
    else:
       print "Returning y:: INCORRECT OPTIONS"
       m = 1
       c = 0

    return (y-c)/m 

# x >= y
def big_equal(x,y):
    return x > y or np.allclose(x,y)

# x <= y
def less_equal(x,y):
    return x < y or np.allclose(x,y)

def convert_jet_to_grey(img_array,n):
    new_image = np.zeros((img_array.shape[0],img_array.shape[1]))
    for i in range(img_array.shape[0]):
        for j in range(img_array.shape[1]):
            pixel_blue = img_array[i,j,2]
            pixel_green = img_array[i,j,1]
            pixel_red = img_array[i,j,0]
            if (pixel_blue < 1) and big_equal(pixel_blue,0.5) and less_equal(pixel_green,0.5) :
               #print "a1"
               #print "i,j = ",i,",",j
               new_image[i,j] = return_x(pixel_blue,"blue","up")**n
            elif np.allclose(pixel_blue,1.0) and big_equal(pixel_green,0):
                 #print "b1"
                 #print "i,j = ",i,",",j
                 new_image[i,j] = return_x(pixel_green,"green","up")**n
            elif (pixel_blue < 1) and big_equal(pixel_blue,0.4) and big_equal(pixel_green,0.5):
                 #print "c1"
                 #print "i,j = ",i,",",j
                 new_image[i,j] = return_x(pixel_green,"blue","down")**n
            elif (pixel_red < 1) and big_equal(pixel_red,0.4) and big_equal(pixel_green,0.5):
                 #print "c2"
                 #print "i,j = ",i,",",j
                 new_image[i,j] = return_x(pixel_green,"red","up")**n
            elif np.allclose(pixel_red,1.0) and big_equal(pixel_green,0):
                 #print "b2"
                 #print "i,j = ",i,",",j
                 new_image[i,j] = return_x(pixel_green,"green","down")**n
            elif (pixel_red < 1) and big_equal(pixel_red,0.5) and less_equal(pixel_green,0.5):
           #print "a2"
           #print "i,j = ",i,",",j
           new_image[i,j] = return_x(pixel_blue,"red","down")**n
        else:
           print "Leaving 0:: NOT A JET IMAGE"

return new_image

def main():


   img = Image.open('test.jpg')
   arr = PIL2array(img)

   img_new = convert_jet_to_grey(arr/255.0,1)
   imgplot = plt.imshow(img_new)
   plt.show()

if __name__ == '__main__':
   main()