forked from kennethinfante/Practical-Python-and-OpenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path20histogram_with_mask.py
executable file
·43 lines (31 loc) · 1020 Bytes
/
20histogram_with_mask.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
from matplotlib import pyplot as plt
import numpy as np
import argparse
import cv2
def plot_histogram(image, title, mask = None):
chans = cv2.split(image)
colors = ("b", "g", "r")
plt.figure()
plt.title(title)
plt.xlabel("Bins")
plt.ylabel("# of Pixesl")
for (chan, color) in zip(chans, colors):
hist = cv2.calcHist([chan], [0], mask, [256], [0,256])
plt.plot(hist, color = color)
plt.xlim([0, 256])
# plt.show()
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("Origina", image)
plot_histogram(image, "Histogram for Original Image")
mask = np.zeros(image.shape[:2], dtype = "uint8")
cv2.rectangle(mask, (15, 15), (130, 100), 255, -1)
cv2.imshow("Mask", mask)
masked = cv2.bitwise_and(image, image, mask = mask)
cv2.imshow("Applying the Mask", masked)
# cv2.waitKey(0)
plot_histogram(image, "Histogram for Masked Image", mask = mask)
plt.show()
cv2.waitKey(0)