Advertisement
menna_elshahawy

Untitled

Nov 24th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.52 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import cv2
  3. import numpy as np
  4. import time
  5.  
  6. cameraman = cv2.imread('cameraman.png')
  7. fognoise = cv2.imread('fognoise.png')
  8. fog = cv2.imread('fog.png')
  9. frostfog = cv2.imread('frostfog.png')
  10. bat = cv2.imread('bat.png')
  11. tree = cv2.imread('tree.png', 0)
  12. treeM = cv2.imread('treeM.png', 0)
  13.  
  14.  
  15. def draw_histograms_cameraman():
  16. my_dpi = 240
  17. plt.figure(figsize=(1024 / my_dpi, 512 / my_dpi), dpi=my_dpi)
  18.  
  19. hist, bin_edges = np.histogram(cameraman.ravel(), bins=256, range=[0, 256], normed=True)
  20. F1 = np.cumsum(hist)
  21. width = 1 / 1.5
  22. plt.bar(np.arange(0, 256), hist, width, color='red', alpha=0.5)
  23. plt.bar(np.arange(0, 256) + width, F1 * max(hist), width, color='blue', alpha=0.5)
  24. plt.title('Cameraman Histogram')
  25. plt.savefig('Cameraman_histograms.png', dpi=my_dpi)
  26. plt.show()
  27.  
  28.  
  29. def draw_histograms_bat():
  30. my_dpi = 240
  31. plt.figure(figsize=(1024 / my_dpi, 512 / my_dpi), dpi=my_dpi)
  32. histbat, bin_edges = np.histogram(bat.ravel(), bins=256, range=[0, 256], normed=True)
  33.  
  34. F1 = np.cumsum(histbat)
  35. width = 1 / 1.5
  36. plt.bar(np.arange(0, 256), histbat, width, color='red', alpha=0.5)
  37. plt.bar(np.arange(0, 256) + width, F1 * max(histbat), width, color='blue', alpha=0.5)
  38. plt.title('Bat Histogram')
  39. plt.savefig('Bat_histograms.png', dpi=my_dpi)
  40.  
  41. plt.show()
  42.  
  43.  
  44. def draw_histograms_fog():
  45. my_dpi = 240
  46. plt.figure(figsize=(1024 / my_dpi, 512 / my_dpi), dpi=my_dpi)
  47. hist, bin_edges = np.histogram(fog.ravel(), bins=256, range=[0, 256], normed=True)
  48. F1 = np.cumsum(hist)
  49. width = 1 / 1.5
  50. plt.bar(np.arange(0, 256), hist, width, color='red', alpha=0.5)
  51. plt.bar(np.arange(0, 256) + width, F1 * max(hist), width, color='blue', alpha=0.5)
  52. plt.title('Fog Histogram')
  53. plt.savefig('Fog_histograms.png', dpi=my_dpi)
  54.  
  55. plt.show()
  56.  
  57.  
  58. def draw_histograms_fogNoise():
  59. my_dpi = 240
  60. plt.figure(figsize=(1024 / my_dpi, 512 / my_dpi), dpi=my_dpi)
  61. bins = len(fognoise)
  62. print(bins)
  63. hist, bin_edges = np.histogram(fognoise.ravel(), bins=256, range=[0, 256], normed=True)
  64. F1 = np.cumsum(hist)
  65. width = 1 / 1.5
  66. plt.bar(np.arange(0, 256), hist, width, color='red', alpha=0.5)
  67. plt.bar(np.arange(0, 256) + width, F1 * max(hist), width, color='blue', alpha=0.5)
  68. plt.title('FogNoise Histogram')
  69. plt.savefig('FogNoise_histograms.png', dpi=my_dpi)
  70. plt.show()
  71.  
  72.  
  73. # Mean Verses Gaussian
  74.  
  75. def mean_vs_gaussian():
  76. # preparing kernel
  77. kernel = np.ones((5, 5), np.float32) / 25
  78. # applying 5x5 mean filter
  79. dst = cv2.filter2D(cameraman, -1, kernel)
  80.  
  81. # applying 5x5 gaussian filter
  82. blur = cv2.GaussianBlur(cameraman, (5, 5), 0)
  83. # plotting images after applying both filters
  84. plt.subplot(221), plt.imshow(blur), plt.title('Gaussian')
  85. plt.xticks([]), plt.yticks([])
  86. plt.subplot(222), plt.imshow(dst), plt.title('Mean Filter')
  87. plt.xticks([]), plt.yticks([])
  88. # Plotting histograms
  89. plt.subplot(223), plt.hist(blur.flatten(), 256, range=(0, 255), fc='w', ec='k', normed=True), plt.title(
  90. 'Gaussian Histogram')
  91. plt.subplot(224), plt.hist(dst.flatten(), 256, range=(0, 255), fc='w', ec='k', normed=True), plt.title(
  92. 'Mean Histogram')
  93. # for the image to be full screen
  94. figManager = plt.get_current_fig_manager()
  95. figManager.window.showMaximized()
  96. plt.show()
  97.  
  98.  
  99. def selective_Median_Filter():
  100. # measuring runtime for median filter on all pixels
  101. start_time = time.clock()
  102. median = median_filter()
  103. print('Pre-enhancement runtime : ', "--- %s seconds ---" % (time.clock() - start_time))
  104. plt.imshow(median)
  105. plt.show()
  106. # measuring runtime for median filter on too black and woo white pixels only(noise)
  107. start_time2 = time.clock()
  108. median2 = selective_median_filter()
  109. print('Post-enhancement runtime : ', "--- %s seconds ---" % (time.clock() - start_time2))
  110. plt.imshow(median2)
  111. plt.show()
  112.  
  113.  
  114. def median_filter():
  115. img = cv2.cvtColor(fognoise, cv2.COLOR_BGR2GRAY)
  116. # neglecting corners and edges of image
  117. for i in range(2, img.shape[0] - 2):
  118. for j in range(2, img.shape[1] - 2):
  119. # apply filter on all pixels
  120. sorted = np.sort(img[i - 2:i + 3, j - 2:j + 3].flatten())
  121. idx = (len(sorted) / 2) + 1 if len(sorted) % 2 == 0 else (len(sorted)) / 2
  122. img[i][j] = sorted[int(idx)]
  123. img2 = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  124. return img2
  125.  
  126.  
  127. def selective_median_filter():
  128. img = cv2.cvtColor(fognoise, cv2.COLOR_BGR2GRAY)
  129. # neglecting corners and edges of image
  130. for i in range(2, img.shape[0] - 2):
  131. for j in range(2, img.shape[1] - 2):
  132. p = img[i][j] # apply it on too black and too white only
  133. if 0 <= p <= 30 or 225 <= p <= 255:
  134. sorted = np.sort(img[i - 2:i + 3, j - 2:j + 3].flatten())
  135. idx = (len(sorted) / 2) + 1 if len(sorted) % 2 == 0 else (len(sorted)) / 2
  136. img[i][j] = sorted[int(idx)]
  137. img2 = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
  138. return img2
  139.  
  140.  
  141. def contrastStretching_histogramEqualization():
  142. # contrast stretching
  143. # normalize the image with type NORM_MINMAX
  144. img_rescale = frostfog
  145. cv2.normalize(img_rescale, dst=img_rescale, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX)
  146.  
  147. # histogram equalization
  148. # convert the image back to grey scale format
  149. img_gray = cv2.cvtColor(frostfog, cv2.COLOR_BGR2GRAY)
  150. img_gray = cv2.equalizeHist(img_gray)
  151. # convert the image back to RGB format
  152. img_output = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR)
  153.  
  154. # drawing images after contrast stretching and histogram equalization
  155. plt.subplot(221), plt.imshow(img_rescale), plt.title('Contrast stretching')
  156. plt.xticks([]), plt.yticks([])
  157. plt.subplot(222), plt.imshow(img_output), plt.title('Histogram equalization')
  158. plt.xticks([]), plt.yticks([])
  159.  
  160. # calculating and plotting histograms for both
  161. plt.subplot(223), plt.hist(img_rescale.flatten(), 256, range=[0, 256], fc='r', ec='k', normed=True), plt.title(
  162. 'Contrast stretching Histogram')
  163. # plt.xticks([]), plt.yticks([])
  164.  
  165. # check the cumulative if it is linear
  166. plt.subplot(224), plt.hist(img_output.flatten(), 256, range=[0, 256], fc='b', ec='k', normed=True), plt.title(
  167. 'Histogram equalization Histogram')
  168. # plt.xticks([]), plt.yticks([])
  169.  
  170. # for the image to be full screen
  171. figManager = plt.get_current_fig_manager()
  172. figManager.window.showMaximized()
  173. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement