-
Notifications
You must be signed in to change notification settings - Fork 8
/
warping.py
149 lines (106 loc) · 4.6 KB
/
warping.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
from skylibs.envmap import EnvironmentMap, rotation_matrix
import os
import numpy as np
import math
# from imageio import imread, imsave
from PIL import Image
import glob
import cv2
from skylibs.hdrio import imread, imsave
# image_path = os.path.join("/media/deep/HardDisk4T-new/datasets/laval-processed/hdrOutputs/","9C4A0010-others-00-1.80587-1.11567.exr")
#image_path = os.path.join("/home/deep/datasets/3D60/processed/Matterport3D-central-png","0_0b217f59904d4bdf85d35da2cab963471_color_0_Left_Down_0.0.png")
if False:
image_paths = glob.glob('/home/deep/projects/mini-stylegan2/Evaluation/data/ground_truth_ours_has_blacks/test_not_warp/*exr')
to_path_warp = '/home/deep/projects/mini-stylegan2/Evaluation/data/ground_truth_ours_neg0.6_60degree/test_warp'
elif False:
image_paths = glob.glob('/home/deep/projects/PTI/checkpoints_edit_new_wild_images/*exr')
to_path_warp = '/home/deep/projects/PTI/checkpoints_edit_new_wild_images_no_black_warp'
else:
image_paths = glob.glob('/home/deep/projects/mini-stylegan2/Evaluation/data/ground_truth_ours_neg0.6_60degree_HR/test_select_resized/*exr')
to_path_warp = '/home/deep/projects/mini-stylegan2/Evaluation/data/ground_truth_ours_neg0.6_60degree_HR/test_warp'
if False:
to_path_crop = '/home/deep/projects/mini-stylegan2/Evaluation/data/ground_truth_ours_neg0.6_60degree/test_crop'
if not os.path.exists(to_path_warp):
os.mkdir(to_path_warp)
if False:
if not os.path.exists(to_path_crop):
os.mkdir(to_path_crop)
for image_path in image_paths:
env = EnvironmentMap(image_path, 'latlong')
#(128, 256, 3)
# print('image size:', e.data.shape)
#######################################################################
h, w, c = env.data.shape
warp_image = np.zeros_like(env.data)
beta = -0.6 # sin(beta)
scale = 1
theta_s, phi_s = [], []
# compute
x=np.zeros((h,w))
y=np.zeros((h,w))
z=np.zeros((h,w))
for ii in range(h):
for jj in range(w):
# direction of original coor
x[ii,jj] = np.sin(ii*1.0/h*math.pi)*np.cos(jj*2.0/w*math.pi)#+beta-beta
y[ii,jj] = np.sin(ii*1.0/h*math.pi)*np.sin(jj*2.0/w*math.pi)
z[ii,jj] = np.cos(ii*1.0/h*math.pi)
a = x**2+y**2+z**2
b = 2*x*beta
c = beta**2-1
t = (-b+np.sqrt(b**2-4*a*c))/(2*a)
# print('t:',t)
# # intersection
x_ = x*t+beta #-beta
y_ = y*t
z_ = z*t
# # norm
mag = np.sqrt(x_**2+y_**2+z_**2)
x_norm = x_/mag
y_norm = y_/mag
z_norm = z_/mag
# angle (i.j)--> (x_norm, y_norm, z_norm)
theta = np.arccos(z_norm)
phi = np.arctan2(y_norm,x_norm)#-math.pi/2
theta_index = (theta/math.pi*h).astype(int)
phi_index = (phi/(2*math.pi)*w).astype(int)
# remove black pixels
remove_black_pixels = True
if remove_black_pixels:
img_hdr = env.data[:107,:,:]
img_hdr = cv2.resize(img_hdr, (256,128))
# img_hdr = np.zeros_like(env.data)
# img_hdr[:107,:,:] = env.data[:107,:,:]
# img_hdr[107:,:,:] = env.data[86:107,:,:]
# img_hdr = cv2.resize(img_hdr, (256,128))
else:
img_hdr = env.data
for ii in range(h):
for jj in range(w):
warp_image[ii,jj,:] = img_hdr[theta_index[ii,jj]-1, phi_index[ii,jj]-1, :]
image_warp_name = os.path.join(to_path_warp, image_path.split('/')[-1])
imsave(image_warp_name, warp_image)
# target_new_name= image_path.split('/')[-1].split('.')[0]
# imsave(f'{to_paths}/{target_new_name}_warp.exr', warp_image)
#########################################################################
#######################################################################
if False:
# center crop
dcm = rotation_matrix(azimuth=0,
# elevation=np.pi/8,
elevation=0,
# roll=np.pi/12)
roll=0)
# print('env.data size:', env.data.shape)
crop = env.project(vfov=60., # degrees
rotation_matrix=dcm,
ar=4./3.,
# resolution=(640, 480),
# resolution=(256, 128),
resolution=(256, 192),
projection="perspective",
mode="normal") #for cropping
# mode="mask") # for mask
# crop = np.clip(255.*crop, 0, 255).astype('uint8')
image_crop_name = os.path.join(to_path_crop, image_path.split('/')[-1])
imsave(image_crop_name, crop)