在Matlab中,有一个函数ind2rgb
可以这样使用:
y = ind2rgb(im2uint8(rescale(cfs)),jet(256));
从Matlab website人中:
RGB = ind2rgb(X,map)
将索引图像X
和相应的 colored颜色 映射map
转换为RGB(truecolor)格式.
Python中有没有等效的方法?
在Matlab中,有一个函数ind2rgb
可以这样使用:
y = ind2rgb(im2uint8(rescale(cfs)),jet(256));
从Matlab website人中:
RGB = ind2rgb(X,map)
将索引图像X
和相应的 colored颜色 映射map
转换为RGB(truecolor)格式.
Python中有没有等效的方法?
我不知道是否有内置的类似功能,但您可以使用numpy
个数组轻松实现这一点:
import numpy as np
# Create a 5x5 array of indices for demo
img = np.random.randint(0, 10, (5, 5))
# Create a fake grayscale colormap for demo
cmap = np.vstack((np.linspace(0, 1, n), np.linspace(0, 1, n), np.linspace(0, 1, n))).T
现在,cmap
的第i
行给出了 colored颜色 ,img
的每个元素告诉你cmap
中的哪个索引是该像素的 colored颜色 ,所以你只需要为img
中的每个元素i
取第i
行.因为numpy的索引与广播一起工作,所以可以将整个img
数组作为行索引器,得到一个(img_rows, img_cols, cmap_cols)
形状的数组
rgb_img = cmap[img, :]
print(rgb_img.shape) # (5, 5, 3)
换句话说,ind2rgb(img, cmap)
相当于cmap[img, :]