Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. # bivariate colormaps in matplotlib
  2. import numpy as np
  3. import matplotlib as mpl
  4. import matplotlib.pyplot as plt
  5.  
  6.  
  7. ## Bivariate colormapping
  8. from scipy.interpolate import RegularGridInterpolator
  9.  
  10.  
  11. def bivariate_colormap(img, colormap):
  12. r = np.linspace(0, 1, colormap.shape[0])
  13. c = np.linspace(0, 1, colormap.shape[1])
  14. splines = [RegularGridInterpolator((r,c), colormap[:,:,i]) for i in range(3)]
  15. def remap(img, spline):
  16. pts = np.dstack([img[:,:,0].ravel(), img[:,:,1].ravel()])
  17. return spline(pts).reshape(img.shape[0:2])
  18.  
  19. return np.dstack([remap(img, splines[i]) for i in range(3)])
  20.  
  21.  
  22. ### Demo colormap
  23. def make_colormap(n, cyclic_x=False, cyclic_y=False):
  24. import skimage.color
  25. # simple pretty colormap
  26. spacing = np.linspace(-100,100,n)
  27. mx, my = np.meshgrid(spacing, spacing)
  28. if cyclic_x:
  29. mx = np.cos(mx/100*np.pi)*50.0
  30. if cyclic_y:
  31. my = np.cos(my/100*np.pi)*50.0
  32.  
  33. c = np.ones_like(mx)*75 + my*0.15 - mx *0.3
  34. t = np.dstack([c,mx,my])
  35. t[:,:,1] = np.tanh(t[:,:,1]/130)*100
  36. t[:,:,2] = np.tanh(t[:,:,2]/190)*100
  37.  
  38. rgb_img = skimage.color.lab2rgb(t)
  39. return rgb_img
  40.  
  41. rgb_img = make_colormap(256)
  42. plt.imshow(rgb_img)
  43.  
  44.  
  45.  
  46. ### Demo
  47. import scipy.special
  48.  
  49. x = np.linspace(-3, 3, 100)
  50. mx, my = np.meshgrid(x, x)
  51. z = mx + 1j*my
  52.  
  53. zf = (1+1j)**z
  54.  
  55. zfc = np.dstack([np.real(zf), np.imag(zf)])
  56. # zfc = np.dstack([np.abs(zf), np.angle(zf)]) (use a cyclic color map)
  57.  
  58. def normalize(x):
  59. return (x-np.min(x)) / (np.max(x)-np.min(x))
  60. zfc[:,:,0] = normalize(zfc[:,:,0])
  61. zfc[:,:,1] = normalize(zfc[:,:,1])
  62. plt.imshow(bivariate_colormap(zfc, rgb_img))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement