-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy path#13_histogram.py
75 lines (56 loc) · 2 KB
/
#13_histogram.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
66
67
68
69
70
71
72
73
74
75
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Original #
image = cv2.imread("images/boat.jpg")
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
#? Histogram #
#* Gray #
# hist = cv2.calcHist(image_gray, [0], None, [256], [0, 256])
# plt.title("Histogram for grayscale image".title())
# plt.xlabel("pixel value".title())
# plt.ylabel("number of pixels that have this value".title())
# plt.plot(hist)
# plt.xlim([0, 256])
# cv2.imshow("Grayscale", image_gray)
# plt.show()
#* Colored #
def hist_colored(pic, use_mask=False):
channels = cv2.split(pic)
colors = list('bgr') # <<<<==>>>> ['b','g','r']
plt.title("Histogram for Colored image".title())
plt.xlabel("pixel value".title())
plt.ylabel("number of pixels that have this value".title())
mask = None
if use_mask:
plt.title("Histogram for Colored image (with mask)".title())
mask = np.zeros(image.shape[:2], dtype="uint8")
cv2.rectangle(mask, (15, 15), (130, 100), 255, -1)
masked = cv2.bitwise_and(pic, pic, mask=mask)
cv2.imshow("Mask", mask)
cv2.imshow("Applying the Mask", masked)
for ch, c in zip(channels, colors):
#! you must put the image in a list if you used a mask
hist = cv2.calcHist([ch], [0], mask, [256], [0, 256])
plt.plot(hist, color=c)
plt.xlim([0, 256])
cv2.imshow("The Image", pic)
plt.show()
# hist_colored(image)
# #* with mask #
hist_colored(image, use_mask=True)
# #? Histogram Equalization #
old = cv2.cvtColor(cv2.imread("images/old.jpg"), cv2.COLOR_BGR2GRAY)
eq = cv2.equalizeHist(old)
cv2.imshow('Histogram Equalization', np.hstack((old, eq)))
cv2.waitKey(0)
# # hist_eq = cv2.calcHist(eq, [0], None, [256], [0, 256])
# # plt.plot(hist)
# # plt.title("Histogram equalized image".title())
# # plt.xlim([0, 256])
# # plt.show()
# # hist_old = cv2.calcHist(old, [0], None, [256], [0, 256])
# # plt.plot(hist)
# # plt.title("Histogram for the original image".title())
# # plt.xlim([0, 256])
# # plt.show()