-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexp1.py
More file actions
181 lines (150 loc) · 5.91 KB
/
exp1.py
File metadata and controls
181 lines (150 loc) · 5.91 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import numpy as np
from matplotlib import pyplot as plt
from PIL import Image
import cv2
def read_image(file_path):
img = Image.open(r"C:\Users\Rheina Trudy\Documents\UNI\SEMESTER 5\Multimedia\Experiments\images\1.jpg").convert('L') # Convert to grayscale
return np.array(img)
def calculate_histogram(image):
histogram = np.zeros(256)
for pixel_value in range(256):
mask = (image == pixel_value)
histogram[pixel_value] = np.sum(mask)
return histogram
def normalize_histogram(histogram, total_pixels):
return histogram / total_pixels
def cumulative_distribution_function(normalized_histogram):
return np.cumsum(normalized_histogram)
def histogram_equalization(image, cdf):
return np.interp(image, np.arange(256), cdf * 255).astype(np.uint8)
def plot_cumulative_histogram(cdf):
plt.plot(range(256), cdf, color='g')
plt.grid(True)
plt.ylabel('Cumulative Distribution Function (CDF)')
plt.xlabel('Level of intensity')
plt.title('Histogram Cumulative')
plt.show()
def plot_histogram_and_cdf(image, cdf):
hist, bins = np.histogram(image.flatten(), 256, [0, 256])
cdf_normalized = cdf * hist.max() / cdf.max()
plt.plot(cdf_normalized, color='b')
plt.hist(image.flatten(), 256, [0, 256], color='r')
plt.xlim([0, 256])
plt.legend(('CDF', 'Histogram'), loc='upper left')
plt.show()
def plot_original_histogram(image):
plt.figure(figsize=(12, 6))
# Original Image
plt.subplot(231)
plt.imshow(image, cmap='gray')
plt.title('Original Image')
# Original Histogram
plt.subplot(232)
hist = calculate_histogram(image)
plt.bar(range(256), hist, color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Original Histogram')
plt.show()
def plot_normalized_histogram(histogram):
plt.bar(range(256), histogram, color='b')
plt.grid(True)
plt.ylabel('Pixels with same intensity (normalized)')
plt.xlabel('Level of intensity')
plt.title('Normalized Histogram')
plt.show()
def show_comparison(original_img, custom_img, built_in_img, original_hist, custom_hist, built_in_hist):
plt.figure(figsize=(15, 8))
# Original Image + Original Histogram
plt.subplot(331)
plt.imshow(original_img, cmap='gray')
plt.title('Original Image')
plt.subplot(334)
plt.hist(original_img.flatten(), 256, [0, 256], color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Original Histogram')
# Enhanced Image + Enhanced Histogram (Written by me)
plt.subplot(332)
plt.imshow(custom_img, cmap='gray')
plt.title('Enhanced Image (Written by me)')
plt.subplot(335)
plt.hist(custom_img.flatten(), 256, [0, 256], color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Equalized Histogram (Written by me)')
# Enhanced Image + Enhanced Histogram (Built-in using OpenCV)
plt.subplot(333)
plt.imshow(built_in_img, cmap='gray')
plt.title('Enhanced Image (Built-in using OpenCV)')
plt.subplot(336)
plt.hist(built_in_img.flatten(), 256, [0, 256], color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Equalized Histogram (Built-in using OpenCV)')
# Comparison: Enhanced Image (Written by me) vs Enhanced Image (Built-in using OpenCV)
plt.subplot(337)
plt.imshow(custom_img, cmap='gray')
plt.title('Enhanced Image (Written by me)')
plt.subplot(338)
plt.imshow(built_in_img, cmap='gray')
plt.title('Enhanced Image (Built-in using OpenCV)')
# Histogram: Enhanced Image (Written by me) vs Enhanced Image (Built-in using OpenCV)
plt.subplot(339)
plt.hist(custom_img.flatten(), 256, [0, 256], color='r', alpha=0.5, label='Written by me')
plt.hist(built_in_img.flatten(), 256, [0, 256], color='b', alpha=0.5, label='Built-in OpenCV')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Comparison: Equalized Histogram')
plt.legend()
plt.tight_layout()
plt.show()
# Using custom function to read the image
img = read_image("path/to/your/image.jpg")
plot_original_histogram(img)
hist = calculate_histogram(img)
# Normalize
total_pixels = img.shape[0] * img.shape[1]
norm_hist = normalize_histogram(hist, total_pixels)
# Histogram and CDF
plot_histogram_and_cdf(img, norm_hist)
# Normalized histogram
plot_normalized_histogram(norm_hist)
# CDF calculation
cdf = cumulative_distribution_function(norm_hist)
# Cumulative histogram
plot_cumulative_histogram(cdf)
# Equalization
img_equalized = histogram_equalization(img, cdf)
# Enhanced image + histogram (written by me)
plt.subplot(121)
plt.imshow(img_equalized, cmap='gray')
plt.title('Enhanced Image (Written by me)')
plt.subplot(122)
plt.hist(img_equalized.flatten(), 256, [0, 256], color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of intensity')
plt.title('Equalized Histogram (Written by me)')
plt.show()
# Equalization using OpenCV built-in function
img_histeq = cv2.equalizeHist(img)
# Image + histogram (Built-in using OpenCV)
plt.subplot(121)
plt.imshow(img_histeq, cmap='gray')
plt.title('Enhanced Image (Built-in using OpenCV)')
plt.subplot(122)
plt.hist(img_histeq.flatten(), 256, [0, 256], color='r')
plt.grid(True)
plt.ylabel('Pixels with same intensity')
plt.xlabel('Level of Intensity')
plt.title('Equalized Histogram (Built-in using OpenCV)')
plt.show()
# Compare
show_comparison(img, img_equalized, img_histeq, hist, norm_hist, cv2.calcHist([img_histeq], [0],
None, [256], [0, 256]))