Advertisement
dan-masek

Untitled

Sep 25th, 2018
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.76 KB | None | 0 0
  1. import cv2
  2. import numpy as np
  3. from matplotlib import pyplot as plt
  4.  
  5.  
  6. def apply_custom_colormap(image_gray, cmap=plt.get_cmap('seismic')):
  7.     assert image_gray.dtype == np.uint8, 'must be np.uint8 image'
  8.     if image_gray.ndim == 3: image_gray = image_gray.squeeze(-1)
  9.  
  10.     # Initialize the matplotlib color map
  11.     sm = plt.cm.ScalarMappable(cmap=cmap)
  12.  
  13.     # Obtain linear color range
  14.     color_range = sm.to_rgba(np.linspace(0, 1, 256), bytes=True)[:,2::-1]
  15.     color_range = color_range.reshape(256,1,3)
  16.  
  17.     return cv2.applyColorMap(image_gray, color_range)
  18.  
  19. image_gray = cv2.imread('cage.png', cv2.IMREAD_GRAYSCALE)
  20. image_bgr = apply_custom_colormap(image_gray, cmap=plt.get_cmap('bwr'))
  21.  
  22. cv2.imshow('image with colormap', image_bgr)
  23. cv2.waitKey(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement