-
Notifications
You must be signed in to change notification settings - Fork 2
/
smog.py
152 lines (112 loc) · 3.8 KB
/
smog.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from PIL import Image
from guidedfilter import *
def getDark(input_img, filter, frame):
"""get dark image from the input image"""
size = input_img.size
output = []
for x in xrange(size[1]):
temp = []
for y in xrange(size[0]):
temp.append(min(input_img.getpixel((y, x))))
output.append(temp)
output = filter2d(output, filter, frame)
output_img = Image.new('L', size)
for x in xrange(size[1]):
for y in xrange(size[0]):
output_img.putpixel((y, x), output[x][y])
return output_img
def getLight(srcImage, darkImage, cut):
"""get atmospheric light from the picture"""
size = darkImage.size
light = []
for x in xrange(size[0]):
for y in xrange(size[1]):
light.append(darkImage.getpixel((x, y)))
# all_light = []
# all_size = srcImage.size
# for x in xrange(all_size[0]):
# for y in xrange(all_size[1]):
# all_light.append(sum(srcImage.getpixel((x, y))))
# all_light.sort()
# all_light.reverse()
# all_threshold=all_light[int(cut * len(all_light))]
# image1=Image.new('RGB', all_size)
#
# for x in xrange(all_size[0]):
# for y in xrange(all_size[1]):
# if sum(srcImage.getpixel((x, y)))>= all_threshold:
# image1.putpixel((x,y), (255, 0, 0))
# else:
# image1.putpixel((x,y),srcImage.getpixel((x, y)))
#
# image1.save('image1.png')
light.sort()
light.reverse()
threshold = light[int(cut * len(light))]
atmosphere = {}
for x in xrange(size[0]):
for y in xrange(size[1]):
if darkImage.getpixel((x, y)) >= threshold:
atmosphere.update({(x, y): sum(srcImage.getpixel((x, y))) / 3.0})
pos = sorted(atmosphere.iteritems(), key = lambda item: item[1], reverse = True)[0][0]
return srcImage.getpixel(pos)
def getTransmission(input_img, light, omiga):
"""get transmission from the picture"""
size = input_img.size
output = []
for x in xrange(size[1]):
temp = []
for y in xrange(size[0]):
temp.append(min(input_img.getpixel((y, x))) / float(min(light)))
output.append(temp)
transmission = []
for x in xrange(size[1]):
temp = []
for y in xrange(size[0]):
temp.append(1 - omiga * minimizeFilter(output, (x, y), (10, 10)))
transmission.append(temp)
return transmission
def getRadiance(input_img, transmission, light, t0):
"""get radiance from the picture"""
size = input_img.size
output = Image.new('RGB', size)
for x in xrange(size[1]):
for y in xrange(size[0]):
r, g, b = input_img.getpixel((y, x))
r = int((r - light[0]) / float(max(t0, transmission[x][y])) + light[0])
g = int((g - light[1]) / float(max(t0, transmission[x][y])) + light[1])
b = int((b - light[2]) / float(max(t0, transmission[x][y])) + light[2])
output.putpixel((y, x), (r, g, b))
return output
def ensure(n):
if n < 0:
n = 0
if n > 255:
n = 255
return int(n)
if __name__ == '__main__':
image = Image.open('14.png')
image = image.convert('RGB')
dark = getDark(image, minimizeFilter, (10, 10))
# dark.save('3_dark.png')
light = getLight(image, dark, 0.001)
transmission = getTransmission(image, light, 0.95)
tranImage = Image.new('L', image.size)
grayImage = image.convert('L')
for x in xrange(image.size[0]):
for y in xrange(image.size[1]):
tranImage.putpixel((x, y), int(transmission[y][x] * 255))
# tranImage.save('transimage.png')
# grayImage.save('grayimage.png')
guided = guidedFilter(grayImage, tranImage, 25, 0.001)
guidedImage = Image.new('L', image.size)
for x in xrange(image.size[0]):
for y in xrange(image.size[1]):
guidedImage.putpixel((x, y), ensure(guided[y][x]))
guided[y][x] /= 255.0
#guidedImage.show()
# guidedImage.save('3_guided.png')
output = getRadiance(image, guided, light, 0.8)
output.save('3_haze.png')