我在邮件列表中回答了这个问题,但我将在这里复制解决方案,以便更容易找到(并且格式更漂亮)。
基本上,在色调的表示方式(0-1而不是0-180)、uint8与浮点数据类型以及灰度图像如何转换为RGB方面存在一些差异。一个快速的用法示例可能如下:
import numpy as np
import matplotlib.pyplot as plt
from skimage import color
from skimage import data
def colorize(image, hue):
"""Return image tinted by the given hue based on a grayscale image."""
hsv = color.rgb2hsv(color.gray2rgb(image))
hsv[:, :, 0] = hue
hsv[:, :, 1] = 1 # Turn up the saturation; we want the color to pop!
return color.hsv2rgb(hsv)
image = data.camera()[::2, ::2]
hue_rotations = np.linspace(0, 1, 6) # 0--1 is equivalent to 0--180
colorful_images = [colorize(image, hue) for hue in hue_rotations]
fig, axes = plt.subplots(nrows=2, ncols=3)
for ax, array in zip(axes.flat, colorful_images):
ax.imshow(array, vmin=0, vmax=1)
ax.set_axis_off()
plt.show()
其给出: