Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. #odpuszczamy czesc 2
  2. import numpy as np
  3. import matplotlib.pyplot as pl
  4. from scipy import misc as mc
  5. from numpy import *
  6.  
  7.  
  8. def histogram(image, N):
  9. [x,y] = image.shape
  10. hist = np.zeros((1,N),dtype=np.int)
  11. step = 255/N
  12. step = np.round(step)
  13. for pixelX in range(0,x):
  14. for pixelY in range(0,y):
  15. value = np.floor(image[pixelX,pixelY]/step)+1
  16. value = np.int8(value)
  17. hist[0][value]=(hist[0][value])+1
  18. return hist[0]
  19.  
  20.  
  21. #
  22. def histMinMax(hist):
  23. min = 0
  24. max = 255
  25. for i in range(0, 255):
  26. if(hist[i]>100):
  27. min = i
  28. break
  29. for j in range(0, 255):
  30. if(hist[-j]>100):
  31. max = 255 -j
  32. break
  33. return min, max
  34.  
  35.  
  36. #
  37. def exten(image, min, max):
  38. img=np.copy(image)
  39. [x,y]=img.shape
  40. vec=np.arange(255)
  41. for i in range(0, 255):
  42. if(vec[i]<min):
  43. vec[i]=0
  44. elif(vec[i]>max):
  45. vec[i]=255
  46. else:
  47. vec[i]=(255/(max-min)) *(i-min)
  48. for pixelX in range(0,x):
  49. for pixelY in range(0,y):
  50. intValue=np.int8(img[pixelX][pixelY])
  51. img[pixelX][pixelY]=vec[intValue]
  52.  
  53. return img
  54.  
  55. def normal(imageMatrix):
  56. h=histogram(imageMatrix,255)
  57. [x,y]=imageMatrix.shape
  58. dystr=zeros((1,255),dtype=np.float)
  59. dystr=dystr[0]
  60. pixels=x*y
  61. a=0.0
  62. for i in range(0, 255):
  63. a=a+h[i]
  64. dystr[i]=a/pixels
  65.  
  66. vec=zeros((1,255),dtype=np.int)
  67. vec=vec[0]
  68. for i in range(0, 255):
  69. value = ((dystr[i]-dystr[0])/(1.0-dystr[0]))*255.0
  70. vec[i]=value.astype(int)
  71.  
  72. im=np.copy(imageMatrix)
  73. for pixelX in range(0, x):
  74. for pixelY in range(0, y):
  75. a=np.int8(im[pixelX][pixelY])
  76. im[pixelX][pixelY]=vec[a]
  77. return im
  78.  
  79.  
  80. img = mc.imread('kierowca.png')
  81. img=np.mean(img,2)
  82.  
  83. size=255
  84. h=histogram(img, size)
  85. [min,max]=histMinMax(h)
  86. print("min: {0}, max: {1}".format(min,max))
  87.  
  88. img2=np.copy(img)
  89. img2=exten(img2,min,max)
  90.  
  91. h2=histogram(img2, size)
  92. size_vec=range(size)
  93.  
  94. img3=normal(img)
  95. h3=histogram(img3, size)
  96.  
  97.  
  98. pl.subplot(3, 2, 1)
  99. pl.ylabel('Normalny')
  100. pl.imshow(img,cmap='gray', vmin=0, vmax=255)
  101. pl.subplot(3, 2, 2)
  102. pl.bar(size_vec,h,1,color='blue')
  103.  
  104. pl.subplot(3, 2, 3)
  105. pl.ylabel('Rozciagniety')
  106. pl.imshow(img2,cmap='gray', vmin=0, vmax=255)
  107. pl.subplot(3, 2, 4)
  108. pl.bar(size_vec,h2,1,color='blue')
  109.  
  110. pl.subplot(3,2,5)
  111. pl.ylabel('Wyrownany')
  112. pl.imshow(img3,cmap='gray',vmin=0,vmax=255)
  113. pl.subplot(3,2,6)
  114. pl.bar(size_vec,h3,1,color='blue')
  115.  
  116. pl.show()
  117.  
  118.  
  119.  
  120.  
  121.  
  122.  
  123. #
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement