我需要找到一种文件类型,可以在不占用太多内存的情况下保存数组。我以为npy文件会比PNG文件少,但我想我错了。
depth_img = np.uint16(depth * 256)
cv2.imwrite(name_dest_im, depth_img)
np.save(name_dest_npy, depth_img)

是因为我在导出数组时做错了什么,还是PNG是存储uint16的最有效的方式?
编辑:这是https://github.com/nianticlabs/monodepth2/blob/master/test_simple.py的修改版本
input_image = pil.open(image_path).convert('RGB')
original_width, original_height = input_image.size
input_image = input_image.resize((feed_width, feed_height), pil.LANCZOS)
input_image = transforms.ToTensor()(input_image).unsqueeze(0)
# PREDICTION
input_image = input_image.to(device)
features = encoder(input_image)
outputs = depth_decoder(features)
disp = outputs[("disp", 0)]
disp_resized = torch.nn.functional.interpolate(
disp, (original_height, original_width), mode="bilinear", align_corners=False)
# Saving numpy file
output_name = os.path.splitext(os.path.basename(image_path))[0]
name_dest_npy = os.path.join(output_directory, "{}_disp.npy".format(output_name))
scaled_disp, _ = disp_to_depth(disp, 0.1, 100)
name_dest_im = os.path.join(output_directory, "{}_disp.png".format(output_name))
# Saving colormapped depth image
map_resized_np = scaled_disp.squeeze().cpu().numpy()
map_resized_np = cv2.resize(map_resized_np, (original_width, original_height))
depth = 5.4 / map_resized_np
depth = np.clip(depth, 0, 80)
depth_img = np.uint16(depth * 256)
cv2.imwrite(name_dest_im, depth_img)
np.savez_compressed(name_dest_npy, depth_img)发布于 2020-07-27 01:41:56
PNG是压缩的,但np.save不使用压缩。您可以使用np.savez_compressed保存压缩数组。如果你的数据允许,你也可以保存为uint8,就像@vlovero在评论中建议的那样。
import cv2
import numpy as np
img = np.random.randint(0, 256, size=(128, 128, 3)).astype(np.uint16)
cv2.imwrite("random.png", img)
np.save("random.npy", img)
np.savez_compressed("random.npz", img=img)
f = np.load("random.npz")
img_loaded = f["img"]
# The loaded array is equal to the original.
np.testing.assert_array_equal(img, img_loaded)文件大小如下:
$ du -h random*
100K random.npy
64K random.npz
64K random.pnghttps://stackoverflow.com/questions/63103569
复制相似问题