-
Notifications
You must be signed in to change notification settings - Fork 11
/
main.py
137 lines (116 loc) · 4.75 KB
/
main.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
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
import matplotlib.pyplot as plt
from scipy import stats
import numpy as np
import random
import PIL.ImageDraw as ImageDraw
import PIL.Image as Image
import os.path
from tqdm import tqdm
import math
def load_images():
img_path = './imgs/'
imgs = []
for file in tqdm(os.listdir(img_path)):
imgs.append(np.array(Image.open(img_path + file)))
# both input images are from 0-->255
return imgs
def print_img(img, histo_new, histo_old, index, L):
dpi = 80
width = img.shape[0]
height = img.shape[1]
if height > width:
figsize = (img.shape[0]*4) / float(dpi), (height)/ float(dpi)
fig, axs = plt.subplots(1, 3, gridspec_kw={'width_ratios': [3, 1,1]}, figsize=figsize)
else:
figsize = (width) / float(dpi), (height*4) / float(dpi)
fig, axs = plt.subplots(3, 1, gridspec_kw={'height_ratios': [3, 1,1]}, figsize=figsize)
fig.suptitle("Enhanced Image with L:" + str(L))
axs[0].title.set_text("Enhanced Image")
axs[0].imshow(img, vmin=np.amin(img), vmax=np.amax(img), cmap='gray')
axs[1].title.set_text("Equalized histogram")
axs[1].plot(histo_new, color='#f77f00')
axs[1].bar(np.arange(len(histo_new)), histo_new, color='#003049')
axs[2].title.set_text("Main histogram")
axs[2].plot(histo_old, color='#ef476f')
axs[2].bar(np.arange(len(histo_old)), histo_old, color='#b7b7a4')
plt.tight_layout()
plt.savefig("e" + index + str(L)+".pdf")
plt.savefig("e" + index + str(L)+".png")
def print_histogram(_histrogram, name, title):
plt.figure()
plt.title(title)
plt.plot(_histrogram, color='#ef476f')
plt.bar(np.arange(len(_histrogram)), _histrogram, color='#b7b7a4')
plt.ylabel('Number of Pixels')
plt.xlabel('Pixel Value')
plt.savefig("hist_" + name)
def generate_histogram(img, print, index):
if len(img.shape) == 3: # img is colorful
gr_img = np.mean(img, axis=-1)
else:
gr_img = img
'''now we calc grayscale histogram'''
gr_hist = np.zeros([256])
for x_pixel in range(gr_img.shape[0]):
for y_pixel in range(gr_img.shape[1]):
pixel_value = int(gr_img[x_pixel, y_pixel])
gr_hist[pixel_value] += 1
'''normalize Histogram'''
gr_hist /= (gr_img.shape[0] * gr_img.shape[1])
if print:
print_histogram(gr_hist, name="neq_"+str(index), title="Normalized Histogram")
return gr_hist, gr_img
def equalize_histogram(img, histo, L):
eq_histo = np.zeros_like(histo)
en_img = np.zeros_like(img)
for i in range(len(histo)):
eq_histo[i] = int((L - 1) * np.sum(histo[0:i]))
print_histogram(eq_histo, name="eq_"+str(index), title="Equalized Histogram")
'''enhance image as well:'''
for x_pixel in range(img.shape[0]):
for y_pixel in range(img.shape[1]):
pixel_val = int(img[x_pixel, y_pixel])
en_img[x_pixel, y_pixel] = eq_histo[pixel_val]
'''creating new histogram'''
hist_img, _ = generate_histogram(en_img, print=False, index=index)
print_img(img=en_img, histo_new=hist_img, histo_old=histo, index=str(index), L=L)
return eq_histo
def find_value_target(val, target_arr):
key = np.where(target_arr == val)[0]
if len(key) == 0:
key = find_value_target(val+1, target_arr)
if len(key) == 0:
key = find_value_target(val-1, target_arr)
vvv = key[0]
return vvv
def match_histogram(inp_img, hist_input, e_hist_input, e_hist_target, _print=True):
'''map from e_inp_hist to 'target_hist '''
en_img = np.zeros_like(inp_img)
tran_hist = np.zeros_like(e_hist_input)
for i in range(len(e_hist_input)):
tran_hist[i] = find_value_target(val=e_hist_input[i], target_arr=e_hist_target)
print_histogram(tran_hist, name="trans_hist_", title="Transferred Histogram")
'''enhance image as well:'''
for x_pixel in range(inp_img.shape[0]):
for y_pixel in range(inp_img.shape[1]):
pixel_val = int(inp_img[x_pixel, y_pixel])
en_img[x_pixel, y_pixel] = tran_hist[pixel_val]
'''creating new histogram'''
hist_img, _ = generate_histogram(en_img, print=False, index=3)
print_img(img=en_img, histo_new=hist_img, histo_old=hist_input, index=str(3), L=L)
if __name__ == '__main__':
L=50
print("\r\nLoading Images:")
imgs = load_images()
print("\r\ngenerating HistogramS:")
gr_img_arr = []
gr_hist_arr = []
eq_hist_arr = []
index = 0
for img in tqdm(imgs):
hist_img, gr_img = generate_histogram(img, print=True, index=index)
gr_hist_arr.append(hist_img)
gr_img_arr.append(gr_img)
eq_hist_arr.append(equalize_histogram(gr_img, hist_img, L))
index += 1
match_histogram(inp_img=gr_img_arr[0], hist_input=gr_hist_arr[0], e_hist_input=eq_hist_arr[0], e_hist_target=eq_hist_arr[1])