-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhistogram_quantization.py
More file actions
44 lines (34 loc) · 927 Bytes
/
histogram_quantization.py
File metadata and controls
44 lines (34 loc) · 927 Bytes
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
import cv2
import matplotlib.pyplot as plt
# Read grayscale image
img = cv2.imread('image.jpg', cv2.IMREAD_GRAYSCALE)
if img is None:
print("Error: image.jpg not found!")
exit()
# Histogram Equalization
equalized_img = cv2.equalizeHist(img)
# Calculate histograms
hist_original = cv2.calcHist([img], [0], None, [256], [0, 256])
hist_equalized = cv2.calcHist([equalized_img], [0], None, [256], [0, 256])
# Plot results
plt.figure(figsize=(14, 8))
# Original image
plt.subplot(2, 2, 1)
plt.imshow(img, cmap='gray')
plt.title("Original Image")
plt.axis('off')
# Equalized image
plt.subplot(2, 2, 2)
plt.imshow(equalized_img, cmap='gray')
plt.title("Equalized Image")
plt.axis('off')
# Original histogram
plt.subplot(2, 2, 3)
plt.plot(hist_original)
plt.title("Original Histogram")
# Equalized histogram
plt.subplot(2, 2, 4)
plt.plot(hist_equalized)
plt.title("Equalized Histogram")
plt.tight_layout()
plt.show()