Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.85 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import misc as mc
  4. import math
  5. import copy
  6.  
  7. def rgb2gray(img):
  8. r=img[:,:,0]
  9. g=img[:,:,1]
  10. b=img[:,:,2]
  11. s1=0.2126*r+0.7152*g+0.0722*b
  12. return s1
  13.  
  14.  
  15. #
  16. def brightness(img):
  17. M,N=img.shape
  18. J=(1/(M*N))*(np.sum(np.sum(img)))
  19. return J
  20.  
  21.  
  22. #
  23. def kontrast(img,J):
  24. M,N=img.shape
  25. pierwsze=np.sum(np.sum(np.power((img-J),2)))
  26. drugie=(1/(M*N))*pierwsze
  27. K=np.power(drugie,(1/2))
  28. return K
  29.  
  30.  
  31. #
  32.  
  33. imgp = mc.imread('kierowca.png')
  34.  
  35. #c=img[:,:,:]
  36. s1=rgb2gray(imgp)
  37.  
  38. #imgp = imgp.astype(float)
  39.  
  40.  
  41.  
  42. M,N,O=imgp.shape
  43. print (M)
  44. print (N)
  45.  
  46. img=copy.copy(imgp)
  47.  
  48. J=brightness(img[:,:,0])
  49. print('Jasnosc: %s' % (J))
  50.  
  51. K=kontrast(img[:,:,0],J)
  52. print('Kontrast: %s' % (K))
  53.  
  54. print (img.dtype)
  55. print (img.shape)
  56.  
  57. #plt.imshow(s1)
  58. #plt.show()
  59.  
  60. img=img[:,:,0]
  61. #const=50
  62. #for x in range(0, M):
  63. #for y in range(0, N):
  64. #if((img[x, y] + const) > 255):
  65. #img[x, y] = 225
  66. #elif((img[x, y] + const) < 0):
  67. #img[x, y] = 0
  68. #else:
  69. #img[x, y] = img[x, y] + const
  70.  
  71. stala=50
  72.  
  73. for i in range(0, M):
  74. for j in range(0, N):
  75. img[i,j]=img[i,j]+stala
  76. if ((img[i,j])>255):
  77. img[i,j]=255
  78. if ((img[i,j])<0):
  79. img[i,j]=0
  80.  
  81. J=brightness(img)
  82. K=kontrast(img, J)
  83. print('Jasnosc: %s' % (J))
  84. print('Kontrast: %s' % (K))
  85.  
  86. img1=copy.copy(imgp)
  87. img1=img1[:,:,0]
  88. stala=50
  89. for i in range(0, M):
  90. for j in range(0, N):
  91. img1[i,j]=img1[i,j]*stala
  92. if ((img1[i,j])>255):
  93. img1[i,j]=255
  94. if ((img1[i,j])<0):
  95. img1[i,j]=0
  96.  
  97. J=brightness(img1)
  98. K=kontrast(img1, J)
  99. print('Jasnosc: %s' % (J))
  100. print('Kontrast: %s' % (K))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement