保存RGBD作为单个图像
scale = (64, 1216)
resize_img = transforms.Resize(scale, Image.BILINEAR)
resize_depth = transforms.Resize(scale, Image.NEAREST)
to_tensor = transforms.ToTensor()
img_id = 0
# load image and resize
img = Image.open('RGB_image.jpg')
img = resize_img(img)
img = np.array(img)
# load depth and resize
depth = Image.open('depth_image.png')
depth = resize_depth(depth)
depth = np.array(depth)
depth = depth[:, :, np.newaxis]
# tensor shape and value, normalization
img = Image.fromarray(img).convert('RGB')
img = to_tensor(img).float()
depth = depth / 65535
depth = to_tensor(depth).float()
rgbd = torch.cat((img, depth), 0)
print("\n\nRGBD shape")
print(rgbd.shape)
i used this code https://www.programmersought.com/article/8773686326/ to create RGBD by integrating RGB and depth image
now i wonder if that RGBD file could be saved as single image (jpeg,png...)
i tried it, but unsuccessfully, by using imageio.imwrite(), plt.imsave(), cv2.imwrite()... likely due dimension [4,64,1216], so is there a way to make it happen?
scale = (64, 1216)
resize_img = transforms.Resize(scale, Image.BILINEAR)
resize_depth = transforms.Resize(scale, Image.NEAREST)
to_tensor = transforms.ToTensor()
img_id = 0
# load image and resize
img = Image.open('RGB_image.jpg')
img = resize_img(img)
img = np.array(img)
# load depth and resize
depth = Image.open('depth_image.png')
depth = resize_depth(depth)
depth = np.array(depth)
depth = depth[:, :, np.newaxis]
# tensor shape and value, normalization
img = Image.fromarray(img).convert('RGB')
img = to_tensor(img).float()
depth = depth / 65535
depth = to_tensor(depth).float()
rgbd = torch.cat((img, depth), 0)
print("\n\nRGBD shape")
print(rgbd.shape)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我们可以将深度作为RGBA像素格式的图像的α通道保存。
Alpha通道应用透明度通道,但我们可以将其用作存储RGB和深度的第4个通道。
由于深度可能需要高精度 - 可能需要
float32
精确度,因此我建议使用图像格式。对于与OpenExr格式的兼容性,我们可以将所有通道转换为
float32
在范围[0,1]中。注意:
以下代码示例使用OpenCV而不是枕头。
我认为OpenCV支持EXR文件格式,但是我的OpenCV Python版本不是在EXR支持的情况下构建的。我改用Imageio软件包。
转换和写入RGB和深度到EXR文件的阶段:
加载RGB图像,调整它并转换为float:
加载深度图像,调整大小并转换为float:
合并
img
(3个频道)和depth
(1个频道)到4个频道:形状将为
(1216,64,4)
(应用OpenCV BGRA颜色约定)。编写
bgrd
到EXR文件:如果使用OpenExr构建OpenCV,我们可以使用:
cv2.imwrite('rgbd.exr',bgrd)
。如果我们使用Imageio,则最好在保存之前从BGRA转换为RGBA:
代码示例(转换RGB和RGBA EXR文件,然后读取并转换):
注意:
64x1216
或1216x64
),并且不确定代码depth = depth = depth [:,:,np.newaxis]
。depth_image.png
的格式可能是错误的。更新:
将16位RGBA保存到PNG文件:
而不是使用EXR文件和
float32
Pixel格式...我们可以使用png文件和
uint16
像素格式。PNG文件的像素格式将为RGBA(RGB和Alpha-透明频道)。
每个颜色通道将为16位(2个字节)。
Alpha通道存储深度图(以
uint16
格式)。Merge
img
(3个频道)和深度
(1个通道)到4个频道:将合并图像保存到PNG文件:
代码示例(第二部分读取并显示用于测试):
We may save the depth as an alpha channel of an image in RGBA pixel format.
The alpha channel applies transparency channel, but we may use it as 4'th channel for storing RGB and Depth.
Since the depth may require high precision - may require
float32
precision, I suggest using OpenEXR image format.For compatibility with OpenEXR format we may convert all channels to
float32
in range [0, 1].Note:
The following code sample uses OpenCV instead of Pillow.
I thought OpenCV supports EXR file format, but my OpenCV Python version is not built with EXR support. I used ImageIO package instead.
Stages for converting and writing RGB and depth to an EXR file:
Load RGB image, resize it and convert to float:
Load depth image, resize and convert to float:
Merge
img
(3 channels) anddepth
(1 channel) to 4 channels:The shape is going to be
(1216, 64, 4)
(applies OpenCV BGRA color convention).Writing
bgrd
to EXR file:If OpenCV is build with OpenEXR, we may use:
cv2.imwrite('rgbd.exr', bgrd)
.If we use ImageIO, we better to convert from BGRA to RGBA before saving:
Code sample (convert RGB and Range to RGBA EXR file, then read and convert back):
Note:
64x1216
or1216x64
), and not sure about the codedepth = depth[:, :, np.newaxis]
.I may be wrong about the format of
depth_image.png
.Update:
Saving 16 bits RGBA to PNG file:
Instead of using EXR file and
float32
pixel format...We may use PNG file and
uint16
pixel format.The pixel format of the PNG file is going to be RGBA (RGB and Alpha - transparency channel).
Each color channel is going to be 16 bits (2 bytes).
The alpha channel stores the depth map (in
uint16
format).Convert
img
touint16
(we may choose not to scale by 256):Merge
img
(3 channels) anddepth
(1 channel) to 4 channels:Save the merged image to PNG file:
Code sample (the second part reads and display for testing):