将 RGB 和 Depth 图像整合成 4 通道 RGBD 图像

2018-12-16  本文已影响66人  谢小帅

RGB:24位 3 通道
Depth:16位 单通道

torch.cat((img, depth), 0)
from PIL import Image
from torchvision import transforms
import numpy as np
import torch

scale = (480, 640)

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('/temp_disk/xs/sun/train/image/' + str(0) + '.jpg')
img = resize_img(img)
img = np.array(img)

# load depth and resize
depth = Image.open('/temp_disk/xs/sun/train/depth/' + str(0) + '.png')
depth = resize_depth(depth)
depth = np.array(depth)
depth = depth[:, :, np.newaxis]

# ori shape and value
print(img.shape)
print(depth.shape)
print(img[2][2][0])  # 215
print(img[2][2][1])  # 230
print(img[2][2][2])  # 227
print(depth[2][2][0])  # 16664

# tensor shape and value, normalization
img = Image.fromarray(img).convert('RGB')
img = to_tensor(img).float()
print(img.shape)
print(img[0][2][2])  # tensor(0.8431)
print(img[1][2][2])  # tensor(0.9020)
print(img[2][2][2])  # tensor(0.8902)

depth = depth / 65535
depth = to_tensor(depth).float()
print(depth.shape)
print(depth[0][2][2])  # tensor(0.2543)

rgbd = torch.cat((img, depth), 0)
print(rgbd.shape)

print(rgbd[0][2][2])  # tensor(0.8431)
print(rgbd[1][2][2])  # tensor(0.9020)
print(rgbd[2][2][2])  # tensor(0.8902)
print(rgbd[3][2][2])  # tensor(0.2543)
(480, 640, 3)
(480, 640, 1)
215
230
227
16664
torch.Size([3, 480, 640])
tensor(0.8431)
tensor(0.9020)
tensor(0.8902)
torch.Size([1, 480, 640])
tensor(0.2543)
torch.Size([4, 480, 640])
tensor(0.8431)
tensor(0.9020)
tensor(0.8902)
tensor(0.2543)
上一篇下一篇

猜你喜欢

热点阅读