将彩色图喷射到灰度图

发布于 2024-12-05 04:13:07 字数 368 浏览 1 评论 0原文

我有一个喷射色彩图:

jet colormap

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

我在 Google 上找不到转换它的函数。 MATLAB 使用一些名为 rgb2ind 的东西,但我想知道公式。

I have a jet colormap:

jet colormap

and I would like to know if there's some way to convert to a grayscale. I can't use average because maximum and minimum value goes to the same gray color. Or if there's some way to convert to another color palette.

I can't find on Google a function to convert it. MATLAB uses some thing called rgb2ind but I would like to know the formula.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

变身佩奇 2024-12-12 04:13:07

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

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 获得的色调值重新排序功能。将以下内容与原始颜色图进行比较:

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

imshow(img, 'Colormap',C)

hue_sorted

First let me create an indexed image using the Jet colormap:

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

imshow(img, 'Colormap',cmap)

jet

The straightforward conversion using IND2GRAY produces the following:

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

jet_to_gray

As you expressed, the min/max converge to the same value. From what I understood, you are looking to map the jet colormap to linearly go from dark to light shades of gray. For this, we can reorder using the hue value which we get with the RGB2HSV function. Compare the following against the original colormap:

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

imshow(img, 'Colormap',C)

hue_sorted

梦途 2024-12-12 04:13:07

在 MATLAB 中创建图像,

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

将图像保存为 tiff,使用 Paintbrush 裁剪边框并保存为“\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;

情节应该是这样的:

在此处输入图像描述

您可以使用数组 [r,g,b] 作为查找表或以此为基础
找出从数组 [r,g,b] 获取“公式”的方法

create the image in MATLAB

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

save the image as tiff, crop out the borders with Paintbrush and save as '\directorypath\jetmage.tiff'.

load the image in MATLAB

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

get image size

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

get a row of image for each color, red, green and blue.

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 the intensity profile for each color for that row.

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

The plot should be something like so:

enter image description here

you can use array [r,g,b] as the look-up-table or base on that
figure out a way to get the 'formula' from array [r,g,b]

不奢求什么 2024-12-12 04:13:07

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因为它可能会过度拉伸图像的动态范围。

rgb2ind converts the RGB values for each pixel into indices within a color map. If you use the 2 input version with a color map input, then it will look-up the closest color in the color map that matches each pixel. This will basically give you a single number for each pixel, rather than an RGB value.

For example, if you load your image

RGB = imread(imagefilename);

then, since the Jet color map is returned by jet, then you can get the indexed data using

mapsize = 256;
map = jet(mapsize);
ind = rgb2ind(RGB, map);

You can then display the image using any color map

colormap(map)
image(ind)
colormap('gray')

Don't use imagesc because it may stretch the dynamic range of your image unacceptably.

泅渡 2024-12-12 04:13:07

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()

Python implementation of the answer given by teng (assuming a default 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()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文