从灰度图片创建彩色图片
Create colour picture from greyscale picture
我有一个值从 0 到 1000 的矩阵,我可以轻松地将值缩放到 0~255 范围,如果我在 opencv 中显示来自 Python 的矩阵,那是一张灰度图。 =11=]
问题是,如何将矩阵{Dimensions = (m, n)}转换为三层矩阵数组{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
>>> 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)
我有一个值从 0 到 1000 的矩阵,我可以轻松地将值缩放到 0~255 范围,如果我在 opencv 中显示来自 Python 的矩阵,那是一张灰度图。 =11=]
问题是,如何将矩阵{Dimensions = (m, n)}转换为三层矩阵数组{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
>>> 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)