2os.environ[
"OPENCV_IO_ENABLE_OPENEXR"]=
"1"
10output_dir =
"Q:\\Foo\\pyramid_1k";
15if not os.path.exists(output_dir):
16 os.makedirs(output_dir)
18def handle_cube_face_level(conf, path, l, x, y, w, source_img):
20 cropped = source_img[y:y+w, x:x+w]
21 rescaled = cv2.resize(cropped, [conf.base_size]*2, cv2.INTER_NEAREST
if conf.is_distance
else cv2.INTER_LANCZOS4)
27 output_path = os.path.join(output_dir,
"{}{}.u16".format(conf.prefix, path))
28 np.round(65535 * rescaled[:,:]).astype(
'uint16').tofile(output_path)
32 output_path = os.path.join(output_dir,
"{}{}.png".format(conf.prefix, path,
".png"))
35 rescaled = np.power(rescaled, 1.0/2.4)
38 rescaled = np.clip(np.round(255 * rescaled), 0, 255).astype(
'uint8')
39 cv2.imwrite(output_path, rescaled)
41 if conf.is_color
and w <= conf.base_size:
42 rescaled = cv2.rectangle(rescaled, (0,0), (conf.base_size - 1, conf.base_size - 1), (1, 0.5, 1))
44 print(
"Wrote {}".format(output_path))
46 if l + 1 == conf.tree_depth:
47 assert w == conf.base_size
54 yy = y + w*((k >> 1) & 1)
55 handle_cube_face_level(conf, path + str(k), l, xx, yy, w, source_img)
57def handle_cube_faces(conf, base_images):
59 base_image_size = conf.base_size << (conf.tree_depth-1)
61 assert(len(base_images) == 6)
63 source_img = cv2.imread(base_images[i], cv2.IMREAD_UNCHANGED);
66 assert source_img.shape[0] == source_img.shape[1],
"not square"
68 if source_img.shape[0] != base_image_size:
69 source_img = cv2.resize(source_img, [base_image_size]*2, cv2.INTER_NEAREST)
71 assert source_img.shape[0] == base_image_size,
"base image has unexpected size"
74 if source_img.dtype ==
'float32':
76 if source_img.dtype ==
'uint8':
77 source_img = source_img.astype(
'float32')/255.0
78 elif source_img.dtype ==
'uint16':
79 source_img = source_img.astype(
'float32')/65535.0
81 assert False,
"unsupported dtype"
85 assert len(source_img.shape) == 2,
"Distance image is not single channel"
87 if conf.distance_from_depth:
89 w = source_img.shape[0]
90 unit = (1.0/w)*(0.5+np.arange(w))
91 theta = np.pi * 0.5 * (unit - 0.5)
94 factor2d = np.outer(factor, factor)
95 source_img = np.clip(factor2d * source_img, 0.0, 1.0)
99 print(source_img.shape)
100 assert len(source_img.shape) == 3,
"Color image has not three axises"
103 source_img = np.power(source_img, 2.4)
105 handle_cube_face_level(conf, str(i), 0, 0, 0, base_image_size, source_img)
108if __name__ ==
"__main__":
109 from collections
import namedtuple
111 Conf = namedtuple(
'Conf', [
'base_size',
'tree_depth',
'prefix',
'is_color',
'is_distance',
'distance_from_depth'])
115 conf_color =Conf(base_size=256,
120 distance_from_depth=
False)
122 color_base_images = [
123 "Q:\\Foo\\16k\\color_east.png",
124 "Q:\\Foo\\16k\\color_west.png",
125 "Q:\\Foo\\16k\\color_down.png",
126 "Q:\\Foo\\16k\\color_up.png",
127 "Q:\\Foo\\16k\\color_north.png",
128 "Q:\\Foo\\16k\\color_south.png",
130 handle_cube_faces(conf_color, color_base_images)
133 conf_depth = Conf(base_size=256,
138 distance_from_depth=
True)
139 depth_base_images = [
140 "Q:\\Foo\\16k\\depth_east.png",
141 "Q:\\Foo\\16k\\depth_west.png",
142 "Q:\\Foo\\16k\\depth_down.png",
143 "Q:\\Foo\\16k\\depth_up.png",
144 "Q:\\Foo\\16k\\depth_north.png",
145 "Q:\\Foo\\16k\\depth_south.png",
147 handle_cube_faces(conf_depth, depth_base_images)