Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2018
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.04 KB | None | 0 0
  1. import numpy as np
  2. import matplotlib.pyplot as plt
  3. from scipy import misc as mc
  4.  
  5. def getHist(img, n):
  6.     H = np.zeros(n)
  7.     width, height = img[:,:].shape
  8.     space = np.linspace(0, 255, n+1);
  9.     #print space
  10.     for i in range(0, n):
  11.         boolM =  ( ((img[:,:] >= space[i]) & (img[:,:] < space[i+1])) | ((space[i+1]==255) & (img[:,:] >= space[i]) & (img[:,:] <= space[i+1])) )
  12.         H[i] = np.sum(boolM)
  13.     return H
  14.  
  15. def getProperties(img):
  16.     width, height = img[:,:,0].shape;
  17.     brightness =  float(np.sum(img[:,:,0]))/(width*height)
  18.     contrastMat = img[:,:,0]-brightness
  19.     contrastMat = contrastMat * contrastMat
  20.     contrast = np.sqrt(float(np.sum(contrastMat))/(width*height))
  21.     return brightness, contrast
  22.    
  23. #
  24.  
  25. A = np.array([[1, 0, 0.25], [1, 0.75, 0.5], [1, 0.5, 0.25]]) * 255.0
  26. print getHist( A, 3 ) # [3 2 4]
  27. print getHist( A, 5 ) # [1 2 2 1 3]
  28.  
  29. img = mc.imread('lab2.png')
  30. img = img.astype("float64")
  31. H = getHist(img[:,:,0],11)
  32.  
  33.  
  34. plt.subplot(1,2,1)
  35. plt.bar(range(len(H)), H);
  36.  
  37. img = img/255
  38. plt.subplot(1,2,2)
  39. plt.imshow(img)
  40.  
  41. plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement