Skip to content

Commit f7512c6

Browse files
authored
Merge pull request #1 from dzhoshkun/master
Added support for Lab color space
2 parents 7762546 + 18ff7e5 commit f7512c6

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

real_time_histogram/real_time_histogram.py

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
parser.add_argument('-f', '--file',
1818
help='Path to video file (if not using camera)')
1919
parser.add_argument('-c', '--color', type=str, default='gray',
20-
help='Color space: "gray" (default) or "rgb"')
20+
help='Color space: "gray" (default), "rgb", or "lab"')
2121
parser.add_argument('-b', '--bins', type=int, default=16,
2222
help='Number of bins per channel (default 16)')
2323
parser.add_argument('-w', '--width', type=int, default=0,
@@ -38,6 +38,8 @@
3838
fig, ax = plt.subplots()
3939
if color == 'rgb':
4040
ax.set_title('Histogram (RGB)')
41+
elif color == 'lab':
42+
ax.set_title('Histogram (L*a*b*)')
4143
else:
4244
ax.set_title('Histogram (grayscale)')
4345
ax.set_xlabel('Bin')
@@ -47,13 +49,18 @@
4749
lw = 3
4850
alpha = 0.5
4951
if color == 'rgb':
50-
lineR, = ax.plot(np.arange(bins), np.zeros((bins,)), c='r', lw=lw, alpha=alpha)
51-
lineG, = ax.plot(np.arange(bins), np.zeros((bins,)), c='g', lw=lw, alpha=alpha)
52-
lineB, = ax.plot(np.arange(bins), np.zeros((bins,)), c='b', lw=lw, alpha=alpha)
52+
lineR, = ax.plot(np.arange(bins), np.zeros((bins,)), c='r', lw=lw, alpha=alpha, label='Red')
53+
lineG, = ax.plot(np.arange(bins), np.zeros((bins,)), c='g', lw=lw, alpha=alpha, label='Green')
54+
lineB, = ax.plot(np.arange(bins), np.zeros((bins,)), c='b', lw=lw, alpha=alpha, label='Blue')
55+
elif color == 'lab':
56+
lineL, = ax.plot(np.arange(bins), np.zeros((bins,)), c='k', lw=lw, alpha=alpha, label='L*')
57+
lineA, = ax.plot(np.arange(bins), np.zeros((bins,)), c='b', lw=lw, alpha=alpha, label='a*')
58+
lineB, = ax.plot(np.arange(bins), np.zeros((bins,)), c='y', lw=lw, alpha=alpha, label='b*')
5359
else:
54-
lineGray, = ax.plot(np.arange(bins), np.zeros((bins,1)), c='k', lw=lw)
60+
lineGray, = ax.plot(np.arange(bins), np.zeros((bins,1)), c='k', lw=lw, label='intensity')
5561
ax.set_xlim(0, bins-1)
5662
ax.set_ylim(0, 1)
63+
ax.legend()
5764
plt.ion()
5865
plt.show()
5966

@@ -82,6 +89,16 @@
8289
lineR.set_ydata(histogramR)
8390
lineG.set_ydata(histogramG)
8491
lineB.set_ydata(histogramB)
92+
elif color == 'lab':
93+
cv2.imshow('L*a*b*', frame)
94+
lab = cv2.cvtColor(frame, cv2.COLOR_BGR2LAB)
95+
(l, a, b) = cv2.split(lab)
96+
histogramL = cv2.calcHist([l], [0], None, [bins], [0, 255]) / numPixels
97+
histogramA = cv2.calcHist([a], [0], None, [bins], [0, 255]) / numPixels
98+
histogramB = cv2.calcHist([b], [0], None, [bins], [0, 255]) / numPixels
99+
lineL.set_ydata(histogramL)
100+
lineA.set_ydata(histogramA)
101+
lineB.set_ydata(histogramB)
85102
else:
86103
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
87104
cv2.imshow('Grayscale', gray)

0 commit comments

Comments
 (0)