Santi Peñate-Vera
Santi Peñate-Vera

Reputation: 1186

Create colour picture from greyscale picture

I have a matrix with values from 0 to 1000, I can easily scale the values to the 0~255 range, and that is a greyscale picture if I show the matrix in opencv from Python.

The question is, how do I convert the Matrix {Dimensions = (m, n)} to a 3-layer matrix array {Dimensions = (m, n, 3)}?

This is, how to convert a greyscale picture to a colour picture?

I have made this function but it is not working

 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

Upvotes: 2

Views: 5841

Answers (1)

berak
berak

Reputation: 39796

>>> help(cv2.applyColorMap)
Help on built-in function applyColorMap:

applyColorMap(...)
    applyColorMap(src, colormap[, dst]) -> dst

and here are the map enums:

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

so, simply:

dst = cv2.applyColorMap(src, cv2.COLORMAP_RAINBOW)

enter image description here

Upvotes: 2

Related Questions