-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor_histograms.py
66 lines (51 loc) · 1.71 KB
/
color_histograms.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
from matplotlib import pyplot as plt
import numpy as np
import cv2
image_path = "dino.jpg"
cv2_image = cv2.imread(image_path)
print(cv2_image.shape)
cv2.imshow("Original", cv2_image)
cv2.waitKey(0)
# histogram = cv2.calcHist(cv2.split(cv2_image)[0], [0], None, [256], [0, 256])
# plt.figure()
# plt.title("Grayscale Histogram")
# plt.xlabel("Bins")
# plt.ylabel("# of Pixels")
# plt.plot(histogram)
# plt.xlim([0, 256])
# plt.show()
# cv2.waitKey(0)
chans = cv2.split(cv2_image)
# colors = ("b", "g", "r")
# plt.figure()
# plt.title("Color historgram")
# plt.xlabel("Bins")
# plt.ylabel("# of Pixels")
#
# for (chan, color) in zip(chans, colors):
# hist = cv2.calcHist([chan], [0], None, [256], [0, 256])
# plt.plot(hist, color=color)
# plt.xlim([0, 256])
# plt.show()
# cv2.waitKey(0)
fig = plt.figure()
ax = fig.add_subplot(131)
hist = cv2.calcHist([chans[1], chans[0]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation="nearest")
ax.set_title("2D Color Histogram for G and B")
plt.colorbar(p)
ax = fig.add_subplot(132)
hist = cv2.calcHist([chans[1], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation="nearest")
ax.set_title("2D Color Histogram for G and R")
plt.colorbar(p)
ax = fig.add_subplot(133)
hist = cv2.calcHist([chans[0], chans[2]], [0, 1], None, [32, 32], [0, 256, 0, 256])
p = ax.imshow(hist, interpolation="nearest")
ax.set_title("2D Color Histogram for B and R")
plt.colorbar(p)
print(f"2d histogram shape: {hist.shape}, with {hist.flatten().shape[0]} values")
plt.show()
hist = cv2.calcHist([cv2_image], [0, 1, 2], None, [8, 8, 8], [0, 256, 0, 256, 0, 256])
print(f"3D histogram shape: {hist.shape}, with {hist.flatten().shape[0]}")
plt.show()