-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathagcwd.py
71 lines (58 loc) · 2.48 KB
/
agcwd.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
# Adaptive gamma correction based on the reference.
# Reference:
# S. Huang, F. Cheng and Y. Chiu, "Efficient Contrast Enhancement Using Adaptive Gamma Correction With
# Weighting Distribution," in IEEE Transactions on Image Processing, vol. 22, no. 3, pp. 1032-1041,
# March 2013. doi: 10.1109/TIP.2012.2226047
# Revised from https://github.com/mss3331/AGCWD/blob/master/AGCWD.m
import numpy as np
import cv2
def agcwd(image, w=0.5):
is_colorful = len(image.shape) >= 3
img = extract_value_channel(image) if is_colorful else image
img_pdf = get_pdf(img)
max_intensity = np.max(img_pdf)
min_intensity = np.min(img_pdf)
w_img_pdf = max_intensity * (((img_pdf - min_intensity) / (max_intensity - min_intensity)) ** w)
w_img_cdf = np.cumsum(w_img_pdf) / np.sum(w_img_pdf)
l_intensity = np.arange(0, 256)
l_intensity = np.array([255 * (e / 255) ** (1 - w_img_cdf[e]) for e in l_intensity], dtype=np.uint8)
enhanced_image = np.copy(img)
height, width = img.shape
for i in range(0, height):
for j in range(0, width):
intensity = enhanced_image[i, j]
enhanced_image[i, j] = l_intensity[intensity]
enhanced_image = set_value_channel(image, enhanced_image) if is_colorful else enhanced_image
return enhanced_image
def extract_value_channel(color_image):
color_image = color_image.astype(np.float32) / 255.
hsv = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
v = hsv[:, :, 2]
return np.uint8(v * 255)
def get_pdf(gray_image):
height, width = gray_image.shape
pixel_count = height * width
hist = cv2.calcHist([gray_image], [0], None, [256], [0, 256])
return hist / pixel_count
def set_value_channel(color_image, value_channel):
value_channel = value_channel.astype(np.float32) / 255
color_image = color_image.astype(np.float32) / 255.
color_image = cv2.cvtColor(color_image, cv2.COLOR_BGR2HSV)
color_image[:, :, 2] = value_channel
color_image = np.array(cv2.cvtColor(color_image, cv2.COLOR_HSV2BGR) * 255, dtype=np.uint8)
return color_image
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--image', dest='img_path')
args = parser.parse_args()
img_path = args.img_path
img = cv2.imread(img_path)
cv2.imshow('base', img)
enhanced_image = agcwd(img)
cv2.imwrite('enhanced.jpg', enhanced_image)
cv2.imshow('enhanced', enhanced_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
if __name__ == '__main__':
main()