我有一个值为0到1000的矩阵,我可以轻松地将值缩放到0~255范围,如果我在Python中用opencv显示矩阵,那就是灰度图片。
问题是,如何将矩阵{Dimensions =(m,n)}转换为3层矩阵数组{Dimensions =(m,n,3)}?
这是,如何将灰度图片转换为彩色图片?
我已经完成了这个功能,但它无法正常工作
import matplotlib.pyplot as plt
from itertools import product
def convertPicturetoColor(self, image, cmap=plt.get_cmap('rainbow')):
'''
Converts a greyscale [0~255] picture to a color picture
'''
a, b = np.shape(image)
m = np.zeros((a, b, 3))
for i, j in product(xrange(a), xrange(b)):
m[i,j,:] = np.array(cmap(image[i,j]))[0:3]
return m
答案 0 :(得分:2)
>>> help(cv2.applyColorMap)
Help on built-in function applyColorMap:
applyColorMap(...)
applyColorMap(src, colormap[, dst]) -> dst
以下是地图枚举:
COLORMAP_AUTUMN = 0
COLORMAP_BONE = 1
COLORMAP_COOL = 8
COLORMAP_HOT = 11
COLORMAP_HSV = 9
COLORMAP_JET = 2
COLORMAP_OCEAN = 5
COLORMAP_PINK = 10
COLORMAP_RAINBOW = 4
COLORMAP_SPRING = 7
COLORMAP_SUMMER = 6
COLORMAP_WINTER = 3
所以,简单地说:
dst = cv2.applyColorMap(src, cv2.COLORMAP_RAINBOW)