dataAnalysis.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #import the required packages
  2. import matplotlib.pyplot as plt
  3. from matplotlib.colors import LogNorm
  4. import cv2,glob
  5. import numpy as np
  6. #specify the color for which histogram is to be plotted
  7. color = 'pieces/yellow'
  8. # whether the plot should be on full scale or zoomed
  9. zoom = 1
  10. # load all the files in the folder
  11. files = glob.glob(color + '*.jpg')
  12. files.sort()
  13. # empty arrays for separating the channels for plotting
  14. B = np.array([])
  15. G = np.array([])
  16. R = np.array([])
  17. H = np.array([])
  18. S = np.array([])
  19. V = np.array([])
  20. Y = np.array([])
  21. Cr = np.array([])
  22. Cb = np.array([])
  23. LL = np.array([])
  24. LA = np.array([])
  25. LB = np.array([])
  26. # Data creation
  27. # append the values from each file to the respective channel
  28. for fi in files[:]:
  29. # BGR
  30. im = cv2.imread(fi)
  31. b = im[:,:,0]
  32. b = b.reshape(b.shape[0]*b.shape[1])
  33. g = im[:,:,1]
  34. g = g.reshape(g.shape[0]*g.shape[1])
  35. r = im[:,:,2]
  36. r = r.reshape(r.shape[0]*r.shape[1])
  37. B = np.append(B,b)
  38. G = np.append(G,g)
  39. R = np.append(R,r)
  40. # HSV
  41. hsv = cv2.cvtColor(im,cv2.COLOR_BGR2HSV)
  42. h = hsv[:,:,0]
  43. h = h.reshape(h.shape[0]*h.shape[1])
  44. s = hsv[:,:,1]
  45. s = s.reshape(s.shape[0]*s.shape[1])
  46. v = hsv[:,:,2]
  47. v = v.reshape(v.shape[0]*v.shape[1])
  48. H = np.append(H,h)
  49. S = np.append(S,s)
  50. V = np.append(V,v)
  51. # YCrCb
  52. ycb = cv2.cvtColor(im,cv2.COLOR_BGR2YCrCb)
  53. y = ycb[:,:,0]
  54. y = y.reshape(y.shape[0]*y.shape[1])
  55. cr = ycb[:,:,1]
  56. cr = cr.reshape(cr.shape[0]*cr.shape[1])
  57. cb = ycb[:,:,2]
  58. cb = cb.reshape(cb.shape[0]*cb.shape[1])
  59. Y = np.append(Y,y)
  60. Cr = np.append(Cr,cr)
  61. Cb = np.append(Cb,cb)
  62. # Lab
  63. lab = cv2.cvtColor(im,cv2.COLOR_BGR2LAB)
  64. ll = lab[:,:,0]
  65. ll = ll.reshape(ll.shape[0]*ll.shape[1])
  66. la = lab[:,:,1]
  67. la = la.reshape(la.shape[0]*la.shape[1])
  68. lb = lab[:,:,2]
  69. lb = lb.reshape(lb.shape[0]*lb.shape[1])
  70. LL = np.append(LL,ll)
  71. LA = np.append(LA,la)
  72. LB = np.append(LB,lb)
  73. # Plotting the histogram
  74. nbins = 10
  75. plt.figure(figsize=[20,10])
  76. plt.subplot(2,3,1)
  77. plt.hist2d(B, G, bins=nbins, norm=LogNorm())
  78. plt.xlabel('B')
  79. plt.ylabel('G')
  80. plt.title('RGB')
  81. if not zoom:
  82. plt.xlim([0,255])
  83. plt.ylim([0,255])
  84. plt.colorbar()
  85. plt.subplot(2,3,2)
  86. plt.hist2d(B, R, bins=nbins, norm=LogNorm())
  87. plt.colorbar()
  88. plt.xlabel('B')
  89. plt.ylabel('R')
  90. plt.title('RGB')
  91. if not zoom:
  92. plt.xlim([0,255])
  93. plt.ylim([0,255])
  94. plt.subplot(2,3,3)
  95. plt.hist2d(R, G, bins=nbins, norm=LogNorm())
  96. plt.colorbar()
  97. plt.xlabel('R')
  98. plt.ylabel('G')
  99. plt.title('RGB')
  100. if not zoom:
  101. plt.xlim([0,255])
  102. plt.ylim([0,255])
  103. plt.subplot(2,3,4)
  104. plt.hist2d(H, S, bins=nbins, norm=LogNorm())
  105. plt.colorbar()
  106. plt.xlabel('H')
  107. plt.ylabel('S')
  108. plt.title('HSV')
  109. if not zoom:
  110. plt.xlim([0,180])
  111. plt.ylim([0,255])
  112. plt.subplot(2,3,5)
  113. plt.hist2d(Cr, Cb, bins=nbins, norm=LogNorm())
  114. plt.colorbar()
  115. plt.xlabel('Cr')
  116. plt.ylabel('Cb')
  117. plt.title('YCrCb')
  118. if not zoom:
  119. plt.xlim([0,255])
  120. plt.ylim([0,255])
  121. plt.subplot(2,3,6)
  122. plt.hist2d(LA, LB, bins=nbins, norm=LogNorm())
  123. plt.colorbar()
  124. plt.xlabel('A')
  125. plt.ylabel('B')
  126. plt.title('LAB')
  127. if not zoom:
  128. plt.xlim([0,255])
  129. plt.ylim([0,255])
  130. plt.savefig(color + '.png',bbox_inches='tight')
  131. else:
  132. plt.savefig(color + '-zoom.png',bbox_inches='tight')
  133. plt.show()