knshnf commited on
Commit
f5d7b29
1 Parent(s): cbef43a

Add project files

Browse files
Files changed (3) hide show
  1. app.py +24 -0
  2. color_transfer_MKL.py +38 -0
  3. requirements.txt +1 -0
app.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import numpy as np
3
+ import color_transfer_MKL as ct
4
+
5
+
6
+ def color_transfer(source, target):
7
+ source = np.array(source)/255
8
+ target = np.array(target)/255
9
+ result = ct.color_transfer_MKL(source, target)
10
+ return np.uint8(result * 255)
11
+
12
+ interface = gr.Interface(
13
+ fn=color_transfer,
14
+ inputs=[
15
+ gr.Image(type="numpy"),
16
+ gr.Image(type="numpy"),
17
+ ],
18
+ outputs=["image"],
19
+ title="Image Color Transfer",
20
+ description="DIGIMAP Final Project"
21
+ )
22
+
23
+ if __name__ == "__main__":
24
+ interface.launch()
color_transfer_MKL.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+
3
+ EPS = 2.2204e-16
4
+
5
+
6
+ def color_transfer_MKL(source, target):
7
+ assert len(source.shape) == 3, 'Images should have 3 dimensions'
8
+ assert source.shape[-1] == 3, 'Images should have 3 channels'
9
+ X0 = np.reshape(source, (-1, 3), 'F')
10
+ X1 = np.reshape(target, (-1, 3), 'F')
11
+ A = np.cov(X0, rowvar=False)
12
+ B = np.cov(X1, rowvar=False)
13
+ T = MKL(A, B)
14
+ mX0 = np.mean(X0, axis=0)
15
+ mX1 = np.mean(X1, axis=0)
16
+ XR = (X0 - mX0) @ T + mX1
17
+ IR = np.reshape(XR, source.shape, 'F')
18
+ IR = np.real(IR)
19
+ IR[IR > 1] = 1
20
+ IR[IR < 0] = 0
21
+ return IR
22
+
23
+
24
+ def MKL(A, B):
25
+ Da2, Ua = np.linalg.eig(A)
26
+
27
+ Da2 = np.diag(Da2)
28
+ Da2[Da2 < 0] = 0
29
+ Da = np.sqrt(Da2 + EPS)
30
+ C = Da @ np.transpose(Ua) @ B @ Ua @ Da
31
+ Dc2, Uc = np.linalg.eig(C)
32
+
33
+ Dc2 = np.diag(Dc2)
34
+ Dc2[Dc2 < 0] = 0
35
+ Dc = np.sqrt(Dc2 + EPS)
36
+ Da_inv = np.diag(1 / (np.diag(Da)))
37
+ T = Ua @ Da_inv @ Uc @ Dc @ np.transpose(Uc) @ Da_inv @ np.transpose(Ua)
38
+ return T
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ numpy