Guest User

Untitled

a guest
Nov 19th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.66 KB | None | 0 0
  1. from PIL import Image
  2. import numpy as np
  3. from IPython import get_ipython
  4. get_ipython().run_line_magic('matplotlib', 'inline')
  5. import matplotlib.pyplot as plt
  6.  
  7. #Load an image
  8. fname = 'picture1.jpg'
  9. image_grey = Image.open(fname).convert("L")
  10. image_rgb = Image.open(fname).convert("RGB")
  11.  
  12. arrey_grey = np.asarray(image_grey)
  13. arrey_rgb = np.asarray(image_rgb)
  14.  
  15. #Get image size
  16. with Image.open('picture1.jpg') as img:
  17. width, height = img.size
  18.  
  19. size = width * height
  20.  
  21. #Plot the uploaded image both grey and rgb
  22. plt.imshow(arrey_grey, cmap='gray')
  23. plt.show()
  24.  
  25. plt.imshow(arrey_rgb)
  26. plt.show()
  27.  
  28. #Define numpy arrey for each color channel
  29. matrix_red = np.zeros((256, 4),dtype = object)
  30. matrix_red[:,1] = int(0)
  31. matrix_red[:,2:] = float(0)
  32.  
  33. matrix_green = np.zeros((256, 4),dtype = object)
  34. matrix_green[:,1] = int(0)
  35. matrix_green[:,2:] = float(0)
  36.  
  37. matrix_blue = np.zeros((256, 4),dtype = object)
  38. matrix_blue[:,1] = int(0)
  39. matrix_blue[:,2:] = float(0)
  40.  
  41. # Completing first column with 0-255
  42. for i in range(256):
  43. matrix_red[i][0] = i
  44. matrix_green[i][0] = i
  45. matrix_blue[i][0] = i
  46.  
  47. # Counting intensity for each color channel
  48. for i in range(width):
  49. Matrix_Width = arrey_rgb[i]
  50. for i in range(height):
  51. Matrix_Height = Matrix_Width[i]
  52. Red_Value = Matrix_Height[0]
  53. Green_Value = Matrix_Height[1]
  54. Blue_Value = Matrix_Height[2]
  55. for i in range(256):
  56. if (matrix_red[i][0] == Red_Value):
  57. matrix_red[i][1] = matrix_red[i][1] + 1
  58. if (matrix_green[i][0] == Green_Value):
  59. matrix_green[i][1] = matrix_green[i][1] + 1
  60. if (matrix_blue[i][0] == Blue_Value):
  61. matrix_blue[i][1] = matrix_blue[i][1] + 1
  62.  
  63. # Data for task ahead
  64. Hx = 0
  65. for i in range(256):
  66. matrix_red[i][2] = matrix_red[i][1] / size
  67. Hx = (matrix_red[i][2] + matrix_red[i][3]) + Hx
  68. matrix_red[i][3] = Hx
  69.  
  70. #Plotting results
  71. Frequencie_Red = np.zeros((256, 1),dtype = object)
  72. Frequencie_Red[:,0] = int(0)
  73.  
  74. Frequencie_Green = np.zeros((256, 1),dtype = object)
  75. Frequencie_Green[:,0] = int(0)
  76.  
  77. Frequencie_Blue = np.zeros((256, 1),dtype = object)
  78. Frequencie_Blue[:,0] = int(0)
  79.  
  80. Intensity = np.zeros((256, 1),dtype = object)
  81. Intensity[:,0] = int(0)
  82.  
  83. for i in range(256):
  84. Frequencie_Red[i] = matrix_red[i][1]
  85. Frequencie_Green[i] = matrix_green[i][1]
  86. Frequencie_Blue[i] = matrix_blue[i][1]
  87.  
  88. for i in range(256):
  89. Intensity[i] = i
  90.  
  91. pos = Intensity
  92. width = 1.0
  93.  
  94. ax = plt.axes()
  95. ax.set_xticks(pos + (width / 2))
  96. ax.set_xticklabels(Intensity)
  97.  
  98. plt.bar(pos, Frequencie_Red, width, color='r')
  99. plt.show()
  100.  
  101. plt.bar(pos, Frequencie_Green, width, color='g')
  102. plt.show()
  103.  
  104. plt.bar(pos, Frequencie_Blue, width, color='b')
  105. plt.show()
Add Comment
Please, Sign In to add comment