ret*_*wer 5 python numpy image python-2.7
我想转换 numpy.ndarray:
out = [[12 12 12 ..., 12 12 12]
[12 12 12 ..., 12 12 12]
[12 12 12 ..., 12 12 12]
...,
[11 11 11 ..., 10 10 10]
[11 11 11 ..., 10 10 10]
[11 11 11 ..., 10 10 10]]
Run Code Online (Sandbox Code Playgroud)
到 RGB 图像。
颜色取自数组:
colors_pal = np.array(
[0,0,128], #ind 0
[0,0,0],
....
[255,255,255]], #ind 12
dtype=np.float32)
Run Code Online (Sandbox Code Playgroud)
例如,索引为 12 的所有像素都将为白色 (255,255,255)。
我现在的做法非常慢(大约 1.5 秒/img):
data = np.zeros((out.shape[0],out.shape[1],3), dtype=np.uint8 )
for x in range(0,out.shape[0]):
for y in range(0,out.shape[1]):
data[x,y] = colors_pal[out[x][y]]
img = Image.fromarray(data)
img.save(...)
Run Code Online (Sandbox Code Playgroud)
有什么有效的方法可以更快地做到这一点?
您可以使用完整图像作为查找表的索引。
就像是data = colors_pal[out]
import numpy as np
import matplotlib.pyplot as plt
import skimage.data
import skimage.color
# sample image, grayscale 8 bits
img = skimage.color.rgb2gray(skimage.data.astronaut())
img = (img * 255).astype(np.uint8)
# sample LUT from matplotlib
lut = (plt.cm.jet(np.arange(256)) * 255).astype(np.uint8)
# apply LUT and display
plt.imshow(lut[img])
plt.show()
Run Code Online (Sandbox Code Playgroud)