forked from kennethinfante/Practical-Python-and-OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path17multi-dimensional_hist_2D.py
executable file
·42 lines (32 loc) · 1.24 KB
/
17multi-dimensional_hist_2D.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
from matplotlib import pyplot as plt
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True, help="Path to the image")
args = vars(ap.parse_args())
image = cv2.imread(args["image"])
cv2.imshow("Original", image)
chans = cv2.split(image)
colors = ("b", "g", "r")
fig = plt.figure()
plt.xlabel("Bins")
plt.ylabel("# of Pixels")
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, ticks=np.arange(0, 1200+1, 150))
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, ticks=np.arange(0, 1200+1, 150))
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, ticks=np.arange(0, 1200+1, 150))
print "2D histogram shape: %s, with %d values" % (hist.shape, hist.flatten().shape[0])
plt.show()
cv2.waitKey(0)