-
Notifications
You must be signed in to change notification settings - Fork 0
/
jbu.py
213 lines (181 loc) · 8.59 KB
/
jbu.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
import argparse
import matplotlib.pyplot as plt
from PIL import Image
import numpy as np
from concurrent.futures import ProcessPoolExecutor
import cv2 as cv2
import sys
from multiprocessing import Pool
import tqdm
from time import perf_counter
from numba import jit
# parser = argparse.ArgumentParser(description="Perform Joint Bilateral Upsampling with a source and reference image")
# parser.add_argument("source", help="Path to the source image")
# parser.add_argument("reference", help="Path to the reference image")
# parser.add_argument("output", help="Path to the output image")
# parser.add_argument('--radius', dest='radius', default=2, help='Radius of the filter kernels (default: 2)')
# parser.add_argument('--sigma-spatial', dest='sigma_spatial', default=2.5, help='Sigma of the spatial weights (default: 2.5)')
# parser.add_argument('--sigma-range', dest='sigma_range', help='Sigma of the range weights (default: standard deviation of the reference image)')
# args = parser.parse_args()
img_source = 'depth0592.png'
img_ref = 'color0592.png'
output = 'output.png'
img_source = 'depth_transformed.png'
img_ref = "color0177.png"
output = 'output_aligned.png'
args_radius = 2
args_sigma_spatial = 1.5
args_sigma_range = None
source_image = Image.open(img_source)
reference_image = Image.open(img_ref)
reference = np.array(reference_image)
source_image_upsampled = source_image.resize(reference_image.size, Image.BILINEAR)
source_upsampled = np.array(source_image_upsampled)
source_upsampled = np.array([source_upsampled, source_upsampled, source_upsampled]).transpose((1, 2, 0))
scale = source_image.width / reference_image.width
radius = int(args_radius)
diameter = 2 * radius + 1
step = int(np.ceil(1 / scale))
padding = radius * step
sigma_spatial = float(args_sigma_spatial)
sigma_range = float(args_sigma_range) if args_sigma_range else np.std(reference)
reference = np.pad(reference, ((padding, padding), (padding, padding), (0, 0)), 'symmetric').astype(np.float32)
source_upsampled = np.pad(source_upsampled, ((padding, padding), (padding, padding), (0, 0)), 'symmetric').astype(np.float32)
# Spatial Gaussian function.
x, y = np.meshgrid(np.arange(diameter) - radius, np.arange(diameter) - radius)
kernel_spatial = np.exp(-1.0 * (x**2 + y**2) / (2 * sigma_spatial**2))
kernel_spatial = np.repeat(kernel_spatial, 3).reshape(-1, 3)
# Lookup table for range kernel.
lut_range = np.exp(-1.0 * np.arange(256)**2 / (2 * sigma_range**2))
def process_row(y):
result = np.zeros((reference_image.width, 3))
# print(y)
y += padding
# time = perf_counter()
for x in range(padding, reference.shape[1] - padding):
I_p = reference[y, x]
# print("lol")
patch_reference = reference[y - padding:y + padding + 1:step, x - padding:x + padding + 1:step].reshape(-1, 3)
patch_source_upsampled = source_upsampled[y - padding:y + padding + 1:step, x - padding:x + padding + 1:step].reshape(-1, 3)
# print("lol")
kernel_range = lut_range[np.abs(patch_reference - I_p).astype(int)]
weight = kernel_range * kernel_spatial
k_p = weight.sum(axis=0)
# print("lol")
result[x - padding] = np.round(np.sum(weight * patch_source_upsampled, axis=0) / k_p)
# print(perf_counter() - time)
# sexit()
return result
time = perf_counter()
# pool = Pool(1, maxtasksperchild=None)
# result = pool.map(process_row, range(reference_image.height))
# pool.close()
# pool.join()
executor = ProcessPoolExecutor()
result = executor.map(process_row, range(reference_image.height), chunksize=2)
executor.shutdown(True)
print("elapsed time:", perf_counter() - time)
Image.fromarray(np.array(list(result)).astype(np.uint8)).save(output)
# source_image = Image.open(img_source)
# reference_image = Image.open(img_ref)
# reference = np.array(reference_image)
# source_image_upsampled = source_image.resize(reference_image.size, Image.BILINEAR)
# # plt.imshow(source_image_upsampled)
# # plt.show()
# source_upsampled = np.array(source_image_upsampled)
# source_upsampled = np.array([source_upsampled, source_upsampled, source_upsampled]).transpose((1, 2, 0))
# print(source_upsampled.shape)
# scale = source_image.width / reference_image.width
# radius = int(args_radius)
# diameter = 2 * radius + 1
# step = int(np.ceil(1 / scale))
# padding = radius * step
# sigma_spatial = float(args_sigma_spatial)
# sigma_range = float(args_sigma_range) if args_sigma_range else np.std(reference)
# print("?", reference.shape)
# reference = np.pad(reference, ((padding, padding), (padding, padding), (0, 0)), 'symmetric').astype(np.float32)
# source_upsampled = np.pad(source_upsampled, ((padding, padding), (padding, padding), (0, 0)), 'symmetric').astype(np.float32)
# # Spatial Gaussian function.
# x, y = np.meshgrid(np.arange(diameter) - radius, np.arange(diameter) - radius)
# kernel_spatial = np.exp(-1.0 * (x**2 + y**2) / (2 * sigma_spatial**2))
# kernel_spatial = np.repeat(kernel_spatial, 3).reshape(-1, 3)
# # Lookup table for range kernel.
# lut_range = np.exp(-1.0 * np.arange(256)**2 / (2 * sigma_range**2))
# def process_row(y):
# result = np.zeros((reference_image.width, 3))
# # print(y)
# y += padding
# for x in range(padding, reference.shape[1] - padding):
# I_p = reference[y, x]
# patch_reference = reference[y - padding:y + padding + 1:step, x - padding:x + padding + 1:step].reshape(-1, 3)
# patch_source_upsampled = source_upsampled[y - padding:y + padding + 1:step, x - padding:x + padding + 1:step].reshape(-1, 3)
# patch_source_upsampled_mask = patch_source_upsampled != 0
# # print("!", patch_source_upsampled)
# kernel_range = lut_range[np.abs(patch_reference - I_p).astype(int)]
# weight = kernel_range * kernel_spatial
# # print("?", weight[patch_source_upsampled_mask])
# # if np.any(patch_source_upsampled_mask):
# # print("yup!")
# # print(">", patch_source_upsampled)
# # print(">>", patch_source_upsampled_mask)
# # print(">>>", patch_source_upsampled[patch_source_upsampled_mask])
# # print(patch_source_upsampled.shape, weight.shape, patch_source_upsampled[patch_source_upsampled_mask].shape, patch_source_upsampled_mask.shape)
# k_p = weight[patch_source_upsampled_mask].sum(axis=0)
# #======
# if patch_source_upsampled[patch_source_upsampled_mask].shape[0] != 0:
# # print(patch_source_upsampled[patch_source_upsampled_mask])
# min_depth = np.amin(patch_source_upsampled[patch_source_upsampled_mask])
# max_depth = np.amax(patch_source_upsampled[patch_source_upsampled_mask])
# depth_delta = max_depth - min_depth
# skip_interpolation_ratio = 0.04693441759
# skip_interpolation_threshold = skip_interpolation_ratio * min_depth
# if depth_delta > skip_interpolation_threshold:
# # print("!", x, y)
# result[x - padding] = source_upsampled[y, x]
# else:
# #======
# if k_p == 0:
# k_p = 1
# # print(weight)
# # exit()
# result[x - padding] = np.round(np.sum(weight[patch_source_upsampled_mask] * patch_source_upsampled[patch_source_upsampled_mask], axis=0) / k_p)
# else:
# if k_p == 0:
# k_p = 1
# # print(weight)
# # exit()
# result[x - padding] = np.round(np.sum(weight[patch_source_upsampled_mask] * patch_source_upsampled[patch_source_upsampled_mask], axis=0) / k_p)
# # exit()
# return result
# # executor = ProcessPoolExecutor(max_workers=8)
# # result = executor.map(process_row, range(reference_image.height), chunksize=1)
# # executor.shutdown(True)
# pool = Pool(8, maxtasksperchild=sys.maxsize)
# result = list(tqdm.tqdm(pool.imap(process_row, np.arange(reference_image.height)), total=reference_image.height))
# # Image.fromarray(np.array(list(result)).astype(np.uint8)).save(output)
# result = np.array(list(result))
# result = result / np.amax(result) * np.amax(source_image)
# # print(result.shape, np.amax(result))
# # print(result[0, 0], type(result[0, 0, 0]))
# result = np.uint16(result)[:, :, 0]
# # print(result.shape, type(result[0, 0]))
# # plt.figure(figsize=(8,16))
# # plt.imshow(source_image)
# # plt.show()
# # plt.figure(figsize=(8,16))
# # plt.imshow(result)
# # plt.show()
# plt.figure(figsize=(16,16))
# plt.subplot(121)
# plt.imshow(source_image)
# plt.title('Reprojected depth map')
# # plt.colorbar()
# plt.subplot(122)
# plt.imshow(result)
# print(type(result[0, 0]))
# plt.title(f'Joint bilateral filtering (sigma_spatial={args_sigma_spatial}, sigma_range={sigma_range})')
# # plt.colorbar()
# plt.show()
# # plt.imshow(source_image - result)
# # plt.show()
# cv2.imwrite(output, result)