Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.34 KB | None | 0 0
  1. import matplotlib
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4.  
  5. from skimage import data, img_as_float
  6. from skimage import exposure
  7. from skimage.color import rgb2gray
  8.  
  9.  
  10. matplotlib.rcParams['font.size'] = 8
  11.  
  12.  
  13. def plot_img_and_hist(image, axes, bins=256):
  14. """Plot an image along with its histogram and cumulative histogram.
  15.  
  16. """
  17. image = img_as_float(image)
  18. ax_img, ax_hist = axes
  19. ax_cdf = ax_hist.twinx()
  20.  
  21. # Display image
  22. ax_img.imshow(image, cmap=plt.cm.gray)
  23. ax_img.set_axis_off()
  24.  
  25. # Display histogram
  26. ax_hist.hist(image.ravel(), bins=bins, histtype='step', color='black')
  27. ax_hist.ticklabel_format(axis='y', style='scientific', scilimits=(0, 0))
  28. ax_hist.set_xlabel('Pixel intensity')
  29. ax_hist.set_xlim(0, 1)
  30. ax_hist.set_yticks([])
  31.  
  32. # Display cumulative distribution
  33. img_cdf, bins = exposure.cumulative_distribution(image, bins)
  34. ax_cdf.plot(bins, img_cdf, 'r')
  35. ax_cdf.set_yticks([])
  36.  
  37. return ax_img, ax_hist, ax_cdf
  38.  
  39.  
  40. # Load an example image
  41. img = plt.imread('rophophora.jpg')
  42. img = rgb2gray(img)
  43.  
  44. # Contrast stretching
  45. p10, p90 = np.percentile(img, (10, 90))
  46. img_rescale = exposure.rescale_intensity(img, in_range=(p10, p90))
  47.  
  48. # Equalization
  49. img_eq = exposure.equalize_hist(img)
  50.  
  51. # Adaptive Equalization
  52. img_adapteq = exposure.equalize_adapthist(img, clip_limit=0.03)
  53.  
  54. # Display results
  55. fig = plt.figure(figsize=(8, 5))
  56. axes = np.zeros((2, 4), dtype=np.object)
  57. axes[0, 0] = fig.add_subplot(2, 4, 1)
  58. for i in range(1, 4):
  59. axes[0, i] = fig.add_subplot(2, 4, 1+i, sharex=axes[0,0], sharey=axes[0,0])
  60. for i in range(0, 4):
  61. axes[1, i] = fig.add_subplot(2, 4, 5+i)
  62.  
  63. ax_img, ax_hist, ax_cdf = plot_img_and_hist(img, axes[:, 0])
  64. ax_img.set_title('Low contrast image')
  65.  
  66. y_min, y_max = ax_hist.get_ylim()
  67. ax_hist.set_ylabel('Number of pixels')
  68. ax_hist.set_yticks(np.linspace(0, y_max, 5))
  69.  
  70. ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_rescale, axes[:, 1])
  71. ax_img.set_title('Contrast stretching')
  72.  
  73. ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_eq, axes[:, 2])
  74. ax_img.set_title('Histogram equalization')
  75.  
  76. ax_img, ax_hist, ax_cdf = plot_img_and_hist(img_adapteq, axes[:, 3])
  77. ax_img.set_title('Adaptive equalization')
  78.  
  79. ax_cdf.set_ylabel('Fraction of total intensity')
  80. ax_cdf.set_yticks(np.linspace(0, 1, 5))
  81.  
  82. # prevent overlap of y-axis labels
  83. fig.tight_layout()
  84. #plt.savefig('ropho_plot_equalize.jpg',dpi=150)
  85.  
  86. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement