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