-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils_rgb.py
77 lines (59 loc) · 3.48 KB
/
utils_rgb.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
import numpy as np
import collections
import cv2
# Written by Ying Qu <[email protected]>
# This code is a demo code for our paper
# “Non-local Representation based Mutual Affine-Transfer Network for Photorealistic Stylization”, TPAMI 2021
# The code is for research purpose only
# All Rights Reserved
def preprocess(img):
# bgr to rgb
img = img[..., ::-1]
if np.max(img)>1:
img = img/255.0
return img
def readData(file_content,file_style,scale=4):
data = collections.namedtuple('data', ['content', 'content_scaled','style','style_scaled'
'dimc', 'dimc_sacaled','dims_scaled','dims',
'content_reduced', 'content_reduced_scaled'
'style_reduced','style_reduced_scaled','num'])
scale_factor = 1.0/scale
content_bgr = cv2.imread(file_content, cv2.IMREAD_COLOR)
data.content = preprocess(content_bgr)
data.content = data.content.astype(np.float32)
data.dimc = data.content.shape
target_h_c = int(data.dimc[0] * scale_factor)
target_w_c = int(data.dimc[1] * scale_factor)
content_bgr_scaled = cv2.resize(content_bgr.copy(), (target_w_c, target_h_c))
data.content_scaled = preprocess(content_bgr_scaled)
data.content_scaled = data.content_scaled.astype(np.float32)
data.dimc_scaled = data.content_scaled.shape
style_bgr = cv2.imread(file_style, cv2.IMREAD_COLOR)
data.style = preprocess(style_bgr)
data.style = data.style.astype(np.float32)
data.dims = data.style.shape
target_h_s = int(data.dims[0] * scale_factor)
target_w_s = int(data.dims[1] * scale_factor)
style_bgr_scaled = cv2.resize(style_bgr.copy(), (target_w_s, target_h_s))
data.style_scaled = preprocess(style_bgr_scaled)
data.style_scaled = data.style_scaled.astype(np.float32)
data.dims_scaled = data.style_scaled.shape
data.col_content = np.reshape(data.content,[data.dimc[0]*data.dimc[1],data.dimc[2]])
data.meanc = np.mean(data.col_content,axis=0,keepdims=True)
data.content_reduced = np.subtract(data.col_content,data.meanc)
data.content_reduced_img = np.reshape(data.content_reduced,[data.dimc[0],data.dimc[1],data.dimc[2]])
data.col_style = np.reshape(data.style,[data.dims[0]*data.dims[1],data.dims[2]])
data.means = np.mean(data.col_style,axis=0,keepdims=True)
data.style_reduced = np.subtract(data.col_style,data.means)
data.style_reduced_img = np.reshape(data.style_reduced, [data.dims[0],data.dims[1],data.dims[2]])
data.col_content_scaled = np.reshape(data.content_scaled,[data.dimc_scaled[0]*data.dimc_scaled[1],data.dimc_scaled[2]])
data.meanc_scaled = np.mean(data.col_content_scaled,axis=0,keepdims=True)
data.content_reduced_scaled = np.subtract(data.col_content_scaled,data.meanc_scaled)
data.content_reduced_img_scaled = np.reshape(data.content_reduced_scaled,[data.dimc_scaled[0],data.dimc_scaled[1],data.dimc_scaled[2]])
data.uu,data.ss,data.vv = np.linalg.svd(data.content_reduced_scaled.T,full_matrices=False)
data.projU = data.uu
data.col_style_scaled = np.reshape(data.style_scaled,[data.dims_scaled[0]*data.dims_scaled[1],data.dims_scaled[2]])
data.means_scaled = np.mean(data.col_style_scaled,axis=0,keepdims=True)
data.style_reduced_scaled = np.subtract(data.col_style_scaled,data.means_scaled)
data.style_reduced_img_scaled = np.reshape(data.style_reduced_scaled, [data.dims_scaled[0],data.dims_scaled[1],data.dims_scaled[2]])
return data