Guest User

Untitled

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