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