Pseudo-color mapping

python version

Excerpted from: https://blog.csdn.net/guduruyu/article/details/60868501

 

 

 

 

 

# -*- coding: utf-8 -*-
"""
Created on Wed Apr 25 10:25:15 2018

@author: Administrator
"""

from matplotlib import cm  
import numpy as np   
from PIL import Image


    def get_jet():    #获取整型和浮点型的jet map
      
        colormap_int = np.zeros((256, 3), np.uint8)  
        colormap_float = np.zeros((256, 3), np.float)  
      
        for i in range(0, 256, 1):  
           colormap_float[i, 0] = cm.jet(i)[0]  
           colormap_float[i, 1] = cm.jet(i)[1]  
           colormap_float[i, 2] = cm.jet(i)[2]  
      
           colormap_int[i, 0] = np.int_(np.round(cm.jet(i)[0] * 255.0))  
           colormap_int[i, 1] = np.int_(np.round(cm.jet(i)[1] * 255.0))  
           colormap_int[i, 2] = np.int_(np.round(cm.jet(i)[2] * 255.0))  
      
        np.savetxt("jet_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')  
        np.savetxt("jet_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')  
      
        print colormap_int  
      
        return  
    
    
    def get_spectral():  #获取其他种类的  如:spectral map
 
    colormap_int = np.zeros((256, 3), np.uint8)  
    colormap_float = np.zeros((256, 3), np.float)  
 
    for i in range(0, 256, 1):  
       colormap_float[i, 0] = cm.spectral(i)[0]  
       colormap_float[i, 1] = cm.spectral(i)[1]  
       colormap_float[i, 2] = cm.spectral(i)[2]  
 
       colormap_int[i, 0] = np.int_(np.round(cm.spectral(i)[0] * 255.0))  
       colormap_int[i, 1] = np.int_(np.round(cm.spectral(i)[1] * 255.0))  
       colormap_int[i, 2] = np.int_(np.round(cm.spectral(i)[2] * 255.0))  
 
    np.savetxt("spectral_float.txt", colormap_float, fmt = "%f", delimiter = ' ', newline = '\n')  
    np.savetxt("spectral_int.txt", colormap_int, fmt = "%d", delimiter = ' ', newline = '\n')  
 
    print colormap_int  
 
    return


    def gray2color(gray_array, color_map):  #伪彩映射代码
          
        rows, cols = gray_array.shape  
        color_array = np.zeros((rows, cols, 3), np.uint8)  
      
        for i in range(0, rows):  
            for j in range(0, cols):  
                color_array[i, j] = color_map[gray_array[i, j]]  
          
        #color_image = Image.fromarray(color_array)  
      
        return color_array  


    def test_gray2color(): #a calling subroutine
        gray_image = Image.open('Image.png'). convert("L") #
      
        gray_array = np.array(gray_image)  
          
        figure()  
        subplot(211)  
        plt.imshow(gray_array, cmap = 'gray')  
      
        jet_map = np.loadtxt('E:\\Development\\Thermal\ \ColorMaps\\jet_int.txt', dtype = np.int)  
        color_jet = gray2color(gray_array, jet_map) #call a function, color map an array,
        subplot(212) #make another canvas
        plt.imshow( color_jet) # to draw
        show() #Display the picture
        return  



 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324897811&siteId=291194637