Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 9f5ba33

Browse files
committedJan 17, 2025·
Added histogram equalization to computer vision
1 parent 0040ad4 commit 9f5ba33

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
 
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import cv2
2+
import numpy as np
3+
4+
"""
5+
Histogram Equalization for Image Enhancement
6+
https://en.wikipedia.org/wiki/Histogram_equalization
7+
"""
8+
9+
10+
def hist_equalization(image):
11+
"""
12+
Returns the histogram equalization image
13+
:param image: input image
14+
"""
15+
l_channel, a_channel, b_channel = cv2.split(image)
16+
histogram = cv2.calcHist([l_channel], [0], None, [256], [0, 256])
17+
histogram_sum = np.sum(histogram)
18+
probability_density_function = histogram / histogram_sum
19+
cumulative_distribution_function = np.cumsum(probability_density_function)
20+
lookup_table = np.round(cumulative_distribution_function * 255).astype(np.uint8)
21+
equalized_l = cv2.LUT(l_channel, lookup_table)
22+
new_image = cv2.merge((equalized_l, a_channel, b_channel))
23+
new_image = cv2.cvtColor(new_image, cv2.COLOR_LAB2BGR)
24+
return new_image
25+
26+
27+
if __name__ == "__main__":
28+
image = cv2.imread("path_to_image")
29+
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
30+
new_image = hist_equalization(image)
31+
cv2.imshow("image", new_image)
32+
cv2.waitKey(0)
33+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)
Please sign in to comment.