Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import misc as mc
  4.  
  5. img = mc.imread('plik.png')
  6.  
  7.  
  8.  
  9.  
  10. def rgb2gray(img):
  11. r = img[:, :, 0]
  12. g = img[:, :, 1]
  13. b = img[:, :, 2]
  14. s1 = 0.2126 * r + 0.7152 * g + 0.0722 * b
  15. return s1
  16.  
  17. def histogram(count,image):
  18. offset=255.0/count
  19. histogramValueArray = []
  20. i = 0
  21. while (i < 255.0):
  22. i=i+offset
  23. histogramValueArray.append(np.sum(((image >= (i-offset)) & (image <= i))))
  24. return histogramValueArray
  25.  
  26.  
  27. def linear_expansion(image):
  28. # linear expansion
  29. minVal = np.amin(image)
  30. maxVal = np.amax(image)
  31. test = 255
  32. height = img.shape[0]
  33. width = img.shape[1]
  34. s = (height,width)
  35. output = np.zeros(s)
  36. for i in range(0,height):
  37. for j in range(0,width):
  38. if (image[i][j] < 0):
  39. output[i][j] = 0
  40. elif (image[i][j] > test):
  41. output[i][j]=test
  42. else:
  43. output[i][j]=((image[i][j]-minVal)*(test/(maxVal-minVal)))
  44. return output
  45.  
  46. def nonlinear_expansion(image):
  47. # nonlinear expansion
  48. height = img.shape[0]
  49. width = img.shape[1]
  50. size = (height,width)
  51. output = np.zeros(size)
  52.  
  53. test1 = image/255
  54. test2 = np.zeros(size)
  55. #power
  56. test1 = np.power(test1,2)
  57. #sqrt
  58. #test1 = np.power(test1,0.5)
  59. for i in range(0,height):
  60. for j in range(0,width):
  61. #log10
  62. #pw.gda.pl -> czynnik normalizujacy
  63. #test1[i][j] = np.log(test1[i][j]+1)*1.4428
  64. #exp
  65. #test1[i][j] = np.exp(test1[i][j]-1)/1.7183
  66. if ((test1[i][j] > 0) & (test1[i][j] < 1)):
  67. test2[i][j]=test1[i][j]
  68. elif (test1[i][j] < 0):
  69. test2[i][j]=0
  70. elif (test1[i][j] > 1):
  71. test2[i][j]=1
  72. test2=test2*255
  73. return test2
  74.  
  75. def balance(image):
  76. # returns balanced image
  77. height = img.shape[0]
  78. width = img.shape[1]
  79. size = (height,width)
  80. output = np.zeros(size)
  81.  
  82. D0 = 0
  83. D = np.zeros(256)
  84. LUT = np.zeros(256)
  85.  
  86. D[0] = (np.sum(image)/(height*width))
  87. if( D[0] != 0 ):
  88. D0 = D[0]
  89. for i in range(0,255):
  90. D[i+1] = D[i] + (np.sum(image)/(height*width))
  91. if (D0.astype('int') != 0 & D[i+1].astype('int') != 0 ):
  92. D0 = D[i+1]
  93. LUT = (D - D0) / (1 - D0)
  94. for i in range(0,height):
  95. for j in range(0,width):
  96. output[i][j] = LUT[image[i][j].astype('int') + 1]
  97. output = output * -1
  98. return output
  99.  
  100. imggray = rgb2gray(img)
  101.  
  102. # first histogram
  103.  
  104. histogramArr = (histogram(1000,imggray))
  105. tab = np.linspace(0,255,len( histogramArr) )
  106. plt.bar(tab, histogramArr)
  107. plt.show();
  108.  
  109. # test the histogram function
  110. #A = np.array([[1, 0, 0.25], [1, 0.75, 0.5], [1, 0.5, 0.25]]) * 255.0
  111. #print (A)
  112. #print (histogram( 3, A )) # [3 2 4]
  113. #print (histogram( 5, A )) # [1 2 2 1 3]
  114.  
  115. # linear_expansion
  116. #linearExpansionArr = histogram(1000,linear_expansion(imggray))
  117. #tab = np.linspace(0,255,len(linearExpansionArr) )
  118. #plt.bar(tab, linearExpansionArr)
  119. #plt.show()
  120.  
  121. #nonlinear_expansion
  122. #nonlinearExpansionArr = histogram(1000,nonlinear_expansion(imggray))
  123. #tab = np.linspace(0,255,len(nonlinearExpansionArr) )
  124. #plt.bar(tab, nonlinearExpansionArr)
  125. #plt.show()
  126. #plt.imshow(nonlinear_expansion(imggray), cmap=plt.cm.gray , vmin=0 , vmax=255)
  127. #plt.show();
  128. #potegowanie przyciemnia
  129. #pierwiastkowanie rozjasnia
  130. #log rozjasnia
  131. #ex przycieminia
  132.  
  133. # balance
  134. #balanceArr = histogram(1000,balance(imggray))
  135. #tab = np.linspace(0,255,len(balanceArr) )
  136. #plt.bar(tab, balanceArr)
  137. #plt.show()
  138. #plt.imshow(balance(imggray), cmap=plt.cm.gray , vmin=0 , vmax=255)
  139. #plt.show();
  140. #rownomierny rozklad swiatla
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement