-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathexp_fusion.py
56 lines (51 loc) · 1.71 KB
/
exp_fusion.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
import tensorflow as tf
def saturation(img):
mean = tf.keras.backend.mean(img, axis=-1, keepdims = True)
mul = tf.constant([1,1,1,3], tf.int32)
mean = tf.tile(mean, mul)
img = tf.math.subtract(img, mean)
sat = tf.einsum('aijk,aijk->aij', img, img)
sat = tf.math.scalar_mul((1.0/3.0),sat)
sat = tf.math.add(sat, tf.math.scalar_mul(1e-7, tf.ones_like(sat)))
sat = tf.math.sqrt(sat)
return sat
def get_exp(img,c):
#cimg = tf.slice(img,[0,0,0,c],[img.get_shape()[0],img.get_shape()[1],img.get_shape()[2],1])
cimg = tf.squeeze(img,axis=-1)
m = tf.math.scalar_mul(0.5, tf.ones_like(cimg))
cimg = tf.math.subtract(cimg,m)
cimg = tf.math.multiply(cimg,cimg)
cimg = tf.math.scalar_mul(-12.5,cimg)
return cimg
def exposure(img):
rimg, gimg, bimg = tf.split(img, num_or_size_splits=3, axis=-1)
rimg = get_exp(rimg,0)
gimg = get_exp(gimg,1)
bimg = get_exp(bimg,2)
img = tf.math.add(rimg,gimg)
img = tf.math.add(img,bimg)
exp = tf.math.exp(img)
return exp
def contrast(img):
mean = tf.keras.backend.mean(img, axis=-1, keepdims=True)
lap_fil = [[0.0,-1.0,0.0],[-1.0,4.0,-1.0],[0.0,-1.0,0.0]]
lap_fil = tf.expand_dims(lap_fil,-1)
lap_fil = tf.expand_dims(lap_fil,-1)
con = tf.nn.convolution(mean, lap_fil, padding='SAME')
con = tf.math.abs(con)
con = tf.squeeze(con,axis=-1)
return con
def exp_map(img,pc,ps,pe):
con = contrast(img)
sat = saturation(img)
exp = exposure(img)
if pc!=1 or pe!=1 or ps!=1:
pc = tf.math.scalar_mul(pc, tf.ones_like(con))
ps = tf.math.scalar_mul(ps, tf.ones_like(con))
pe = tf.math.scalar_mul(pe, tf.ones_like(con))
con = tf.math.pow(con,pc)
sat = tf.math.pow(sat,pe)
exp = tf.math.pow(exp,ps)
wt_map = tf.math.multiply(con,sat)
wt_map = tf.math.multiply(wt_map,exp)
return wt_map