Debian Codegraph @

examples/domain-adaptation/plot_otda_color_images.py

raw symbols SCIP
1 # -*- coding: utf-8 -*-
2 """
3 =============================
4 OT for image color adaptation
5 =============================
6
7 This example presents a way of transferring colors between two images
8 with Optimal Transport as introduced in [6]
9
10 [6] Ferradans, S., Papadakis, N., Peyre, G., & Aujol, J. F. (2014).
11 Regularized discrete optimal transport.
12 SIAM Journal on Imaging Sciences, 7(3), 1853-1882.
13 """
14
15 # Authors: Remi Flamary <[email protected]>
16 # Stanislas Chambon <[email protected]>
17 #
18 # License: MIT License
19
20 # sphinx_gallery_thumbnail_number = 2
21
22 import os
23 from pathlib import Path
24
25 import numpy as np
26 from matplotlib import pyplot as plt
27 import ot
28
29
30 rng = np.random.RandomState(42)
31
32
33 def im2mat(img):
34 """Converts an image to matrix (one pixel per line)"""
35 return img.reshape((img.shape[0] * img.shape[1], img.shape[2]))
36
37
38 def mat2im(X, shape):
39 """Converts back a matrix to an image"""
40 return X.reshape(shape)
41
42
43 def minmax(img):
44 return np.clip(img, 0, 1)
45
46
47 ##############################################################################
48 # Generate data
49 # -------------
50
51 # Loading images
52 this_file = os.path.realpath("__file__")
53 data_path = os.path.join(Path(this_file).parent.parent.parent, "data")
54
55 I1 = plt.imread(os.path.join(data_path, "ocean_day.jpg")).astype(np.float64) / 256
56 I2 = plt.imread(os.path.join(data_path, "ocean_sunset.jpg")).astype(np.float64) / 256
57
58 X1 = im2mat(I1)
59 X2 = im2mat(I2)
60
61 # training samples
62 nb = 500
63 idx1 = rng.randint(X1.shape[0], size=(nb,))
64 idx2 = rng.randint(X2.shape[0], size=(nb,))
65
66 Xs = X1[idx1, :]
67 Xt = X2[idx2, :]
68
69
70 ##############################################################################
71 # Plot original image
72 # -------------------
73
74 plt.figure(1, figsize=(6.4, 3))
75
76 plt.subplot(1, 2, 1)
77 plt.imshow(I1)
78 plt.axis("off")
79 plt.title("Image 1")
80
81 plt.subplot(1, 2, 2)
82 plt.imshow(I2)
83 plt.axis("off")
84 plt.title("Image 2")
85
86
87 ##############################################################################
88 # Scatter plot of colors
89 # ----------------------
90
91 plt.figure(2, figsize=(6.4, 3))
92
93 plt.subplot(1, 2, 1)
94 plt.scatter(Xs[:, 0], Xs[:, 2], c=Xs)
95 plt.axis([0, 1, 0, 1])
96 plt.xlabel("Red")
97 plt.ylabel("Blue")
98 plt.title("Image 1")
99
100 plt.subplot(1, 2, 2)
101 plt.scatter(Xt[:, 0], Xt[:, 2], c=Xt)
102 plt.axis([0, 1, 0, 1])
103 plt.xlabel("Red")
104 plt.ylabel("Blue")
105 plt.title("Image 2")
106 plt.tight_layout()
107
108
109 ##############################################################################
110 # Instantiate the different transport algorithms and fit them
111 # -----------------------------------------------------------
112
113 # EMDTransport
114 ot_emd = ot.da.EMDTransport()
115 ot_emd.fit(Xs=Xs, Xt=Xt)
116
117 # SinkhornTransport
118 ot_sinkhorn = ot.da.SinkhornTransport(reg_e=1e-1)
119 ot_sinkhorn.fit(Xs=Xs, Xt=Xt)
120
121 # prediction between images (using out of sample prediction as in [6])
122 transp_Xs_emd = ot_emd.transform(Xs=X1)
123 transp_Xt_emd = ot_emd.inverse_transform(Xt=X2)
124
125 transp_Xs_sinkhorn = ot_sinkhorn.transform(Xs=X1)
126 transp_Xt_sinkhorn = ot_sinkhorn.inverse_transform(Xt=X2)
127
128 I1t = minmax(mat2im(transp_Xs_emd, I1.shape))
129 I2t = minmax(mat2im(transp_Xt_emd, I2.shape))
130
131 I1te = minmax(mat2im(transp_Xs_sinkhorn, I1.shape))
132 I2te = minmax(mat2im(transp_Xt_sinkhorn, I2.shape))
133
134
135 ##############################################################################
136 # Plot new images
137 # ---------------
138
139 plt.figure(3, figsize=(8, 4))
140
141 plt.subplot(2, 3, 1)
142 plt.imshow(I1)
143 plt.axis("off")
144 plt.title("Image 1")
145
146 plt.subplot(2, 3, 2)
147 plt.imshow(I1t)
148 plt.axis("off")
149 plt.title("Image 1 Adapt")
150
151 plt.subplot(2, 3, 3)
152 plt.imshow(I1te)
153 plt.axis("off")
154 plt.title("Image 1 Adapt (reg)")
155
156 plt.subplot(2, 3, 4)
157 plt.imshow(I2)
158 plt.axis("off")
159 plt.title("Image 2")
160
161 plt.subplot(2, 3, 5)
162 plt.imshow(I2t)
163 plt.axis("off")
164 plt.title("Image 2 Adapt")
165
166 plt.subplot(2, 3, 6)
167 plt.imshow(I2te)
168 plt.axis("off")
169 plt.title("Image 2 Adapt (reg)")
170 plt.tight_layout()
171
172 plt.show()