plots.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. #!/bin/ipython
  2. import argparse
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. import sys
  6. ## general defines
  7. linecolor = "#%x%x%x" % ( 217, 234, 211 )
  8. markercolor = "#%x%x%x" % ( 217/2, 234/2, 211/2 )
  9. # Draw pretty plot
  10. def doc_plot(fig, x, y):
  11. plt.figure(fig.number)
  12. fig.clear()
  13. lines, = plt.plot(x,y)
  14. lines.set_color(linecolor)
  15. lines.set_linewidth(4)
  16. lines.set_marker('o')
  17. lines.set_markeredgecolor(markercolor)
  18. lines.set_markersize(6)
  19. lines.set_markeredgewidth(2)
  20. axes = fig.get_axes()[0]
  21. axes.set_aspect(1)
  22. axes.set_ybound(0,1)
  23. axes.set_xbound(0,1)
  24. axes.grid(True)
  25. axes.xaxis.label.set_text(r'$P_{IN}$')
  26. axes.xaxis.label.set_fontsize(14)
  27. axes.yaxis.label.set_text(r'$P_{OUT}$')
  28. axes.yaxis.label.set_fontsize(14)
  29. # Print out interleaved coefficients for HAL3 tonemap curve tags
  30. def doc_coeff(x,y):
  31. coeffs = np.vstack((x, y)).reshape(-1,order='F')
  32. coeff_str = "[ "
  33. for val in coeffs[:-1]:
  34. coeff_str += "%0.4f, " % val
  35. coeff_str += "%0.4f ]" % coeffs[-1]
  36. print coeff_str
  37. def doc_map(fig, imgMap, index):
  38. plt.figure(fig.number)
  39. fig.clear()
  40. plt.imshow(imgMap - 1, interpolation='nearest')
  41. for x in range(0, np.size(imgMap, 1)):
  42. for y in range(0, np.size(imgMap, 0)):
  43. plt.text(x,y, imgMap[y,x,index], color='white')
  44. axes = fig.get_axes()[0]
  45. axes.set_xticks(range(0, np.size(imgMap, 1)))
  46. axes.set_yticks(range(0, np.size(imgMap, 0)))
  47. ## Check arguments
  48. parser = argparse.ArgumentParser(description='Draw plots for camera HAL3.x implementation spec doc')
  49. parser.add_argument('--save_figures', default=False, action='store_true',
  50. help='Save figures as pngs')
  51. args = parser.parse_args()
  52. ## Linear mapping
  53. x_lin = np.linspace(0,1,2)
  54. y_lin = x_lin
  55. lin_fig = plt.figure(1)
  56. doc_plot(lin_fig, x_lin, y_lin)
  57. lin_title = 'Linear tonemapping curve'
  58. plt.title(lin_title)
  59. print lin_title
  60. doc_coeff(x_lin, y_lin)
  61. if args.save_figures:
  62. plt.savefig('linear_tonemap.png',bbox_inches='tight')
  63. ## Inverse mapping
  64. x_inv = x_lin
  65. y_inv = 1 - x_lin
  66. inv_fig = plt.figure(2)
  67. doc_plot(inv_fig, x_inv, y_inv)
  68. inv_title = 'Inverting tonemapping curve'
  69. plt.title(inv_title)
  70. print inv_title
  71. doc_coeff(x_inv, y_inv)
  72. if args.save_figures:
  73. plt.savefig('inverse_tonemap.png',bbox_inches='tight')
  74. ## Gamma 1/2.2
  75. x_gamma = np.linspace(0, 1, 16);
  76. y_gamma = x_gamma**(1/2.2)
  77. gamma_fig = plt.figure(3)
  78. doc_plot(gamma_fig, x_gamma, y_gamma)
  79. gamma_title = r'$\gamma=1/2.2$ tonemapping curve'
  80. plt.title(gamma_title)
  81. print gamma_title
  82. doc_coeff(x_gamma, y_gamma)
  83. if args.save_figures:
  84. plt.savefig('gamma_tonemap.png',bbox_inches='tight')
  85. ## sRGB curve
  86. x_srgb = x_gamma
  87. y_srgb = np.where(x_srgb <= 0.0031308, x_srgb * 12.92, 1.055*x_srgb**(1/2.4)-0.055)
  88. srgb_fig = plt.figure(4)
  89. doc_plot(srgb_fig, x_srgb, y_srgb)
  90. srgb_title = 'sRGB tonemapping curve'
  91. plt.title(srgb_title)
  92. print srgb_title
  93. doc_coeff(x_srgb, y_srgb)
  94. if args.save_figures:
  95. plt.savefig('srgb_tonemap.png',bbox_inches='tight')
  96. ## Sample lens shading map
  97. shadingMapSize = np.array([3, 4])
  98. shadingMap1 = np.array(
  99. [ 1.3, 1.2, 1.15, 1.2, 1.2, 1.2, 1.15, 1.2, 1.1, 1.2, 1.2, 1.2, 1.3, 1.2, 1.3, 1.3,
  100. 1.2, 1.2, 1.25, 1.1, 1.1, 1.1, 1.1, 1.0, 1.0, 1.0, 1.0, 1.0, 1.2, 1.3, 1.25, 1.2,
  101. 1.3, 1.2, 1.2, 1.3, 1.2, 1.15, 1.1, 1.2, 1.2, 1.1, 1.0, 1.2, 1.3, 1.15, 1.2, 1.3 ])
  102. redMap = shadingMap1[0::4].reshape(shadingMapSize)
  103. greenEMap = shadingMap1[1::4].reshape(shadingMapSize)
  104. greenOMap = shadingMap1[2::4].reshape(shadingMapSize)
  105. blueMap = shadingMap1[3::4].reshape(shadingMapSize)
  106. rgbMap = np.dstack( (redMap, (greenEMap + greenOMap) / 2, blueMap) )
  107. redMap = np.dstack( (redMap, np.zeros(shadingMapSize), np.zeros(shadingMapSize) ) )
  108. greenEMap = np.dstack( (np.zeros(shadingMapSize), greenEMap, np.zeros(shadingMapSize) ) )
  109. greenOMap = np.dstack( (np.zeros(shadingMapSize), greenOMap, np.zeros(shadingMapSize) ) )
  110. blueMap = np.dstack( (np.zeros(shadingMapSize), np.zeros(shadingMapSize), blueMap ) )
  111. redImg = plt.figure(5)
  112. doc_map(redImg, redMap, 0)
  113. plt.title('Red lens shading map')
  114. if args.save_figures:
  115. plt.savefig('red_shading.png',bbox_inches='tight')
  116. greenEImg = plt.figure(6)
  117. doc_map(greenEImg, greenEMap, 1)
  118. plt.title('Green (even rows) lens shading map')
  119. if args.save_figures:
  120. plt.savefig('green_e_shading.png',bbox_inches='tight')
  121. greenOImg = plt.figure(7)
  122. doc_map(greenOImg, greenOMap, 1)
  123. plt.title('Green (odd rows) lens shading map')
  124. if args.save_figures:
  125. plt.savefig('green_o_shading.png',bbox_inches='tight')
  126. blueImg = plt.figure(8)
  127. doc_map(blueImg, blueMap, 2)
  128. plt.title('Blue lens shading map')
  129. if args.save_figures:
  130. plt.savefig('blue_shading.png',bbox_inches='tight')
  131. rgbImg = plt.figure(9)
  132. rgbImg.clear()
  133. plt.imshow(1/rgbMap,interpolation='bicubic')
  134. axes = rgbImg.get_axes()[0]
  135. axes.set_xticks(range(0, np.size(rgbMap, 1)))
  136. axes.set_yticks(range(0, np.size(rgbMap, 0)))
  137. plt.title('Image of uniform white wall (inverse shading map)')
  138. if args.save_figures:
  139. plt.savefig('inv_shading.png',bbox_inches='tight')
  140. # Rec. 709
  141. x_rec709 = x_gamma
  142. y_rec709 = np.where(x_rec709 <= 0.018, x_rec709 * 4.500, 1.099*x_rec709**0.45-0.099)
  143. rec709_fig = plt.figure(10)
  144. doc_plot(rec709_fig, x_rec709, y_rec709)
  145. rec709_title = 'Rec. 709 tonemapping curve'
  146. plt.title(rec709_title)
  147. print rec709_title
  148. doc_coeff(x_rec709, y_rec709)
  149. if args.save_figures:
  150. plt.savefig('rec709_tonemap.png',bbox_inches='tight')
  151. # Show figures
  152. plt.show()