-
Notifications
You must be signed in to change notification settings - Fork 6
/
histogram_equalization+color.py
39 lines (32 loc) · 1.2 KB
/
histogram_equalization+color.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
import numpy as np
import cv2 as cv
img_list = [
'../data/lena.tif',
'../data/baboon.tif',
'../data/peppers.tif',
]
# Initialize a control parameter
img_select = 0
while True:
# Read the given image
img = cv.imread(img_list[img_select])
assert img is not None, 'Cannot read the given image, ' + img_list[img_select]
# Apply histogram equalization to each channel
img_hist1 = np.dstack((cv.equalizeHist(img[:,:,0]),
cv.equalizeHist(img[:,:,1]),
cv.equalizeHist(img[:,:,2])))
# Apply histogram equalization only to the luminance channel in YCbCr
img_cvt = cv.cvtColor(img, cv.COLOR_BGR2YCrCb)
img_hist2 = np.dstack((cv.equalizeHist(img_cvt[:,:,0]),
img_cvt[:,:,1],
img_cvt[:,:,2]))
img_hist2 = cv.cvtColor(img_hist2, cv.COLOR_YCrCb2BGR)
# Show all images
merge = np.hstack((img, img_hist1, img_hist2))
cv.imshow('Color Histogram Equalization: Image | Each Channel | Luminance Channel', merge)
key = cv.waitKey()
if key == 27: # ESC
break
elif key == ord('\t'):
img_select = (img_select + 1) % len(img_list)
cv.destroyAllWindows()