一、参考资料
论文:Indoor Segmentation and Support Inference from RGBD Images
二、 相关介绍
1.简介
NYU-Depth V2数据集由来自微软 Kinect 的RGB和深度相机记录的各种室内场景的视频序列组成。它具有:
- 1449对密集标记的RGB和深度图像;
- 来自3个城市的464个新场景;
- 407,024个新的无标签帧;
- 每个对象都标有类别和实例编号(如cup1、cup2、cup3等)。
2. 下载数据集
NYU Depth V2数据集的下载链接可以在多个地方找到。以下是一些可用的下载链接:
- 官方下载地址:NYU Depth V2的官方网站提供了数据集的下载,链接为 NYU Depth Dataset V2 homepage。
- TensorFlow Datasets:TensorFlow Datasets也提供了NYU Depth V2数据集,可以通过以下链接访问 nyu_depth_v2 on TensorFlow Datasets。
- 超神经(HyperAI)提供的下载信息:超神经平台上也有关于NYU Depth V2数据集的下载信息,链接为 NYU Depth V2 on HyperAI。
- ATYUN官网提供的链接:ATYUN官网上也有关于NYU Depth V2数据集的介绍和下载链接,链接为 sayakpaul/nyu_depth_v2 on ATYUN。
- Gitee AI:Gitee AI上也有该数据集的镜像,可以通过以下链接访问 nyu_depth_v2 on Gitee AI。
3. 训练集与验证集
Split | Examples |
---|---|
'train' |
47,584 |
'validation' |
654 |
三、常用操作
1. 深度值归一化
# 方法一
max_depth = np.max(depths)
depth_normalized = (depth / max_depth) * 255
# 方法二
depth_normalized = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
depth_normalized *= 255 # 转换到0-255范围
2. RGB/深度图/标注图可视化
为了可视化NYU Depth V2数据集中的原图、深度图和标注图,我们可以使用Python的h5py库来读取.mat文件,然后使用matplotlib库来生成热力图。以下是如何实现这一过程的代码示例:
import h5py
import numpy as np
import matplotlib.pyplot as plt
import matplotlib
matplotlib.use('TkAgg')
from PIL import Image
# 读取数据
# 指定.mat文件路径
mat_file_path = './data/nyu_depth_v2_labeled.mat'
# 使用h5py打开.mat文件
with h5py.File(mat_file_path, 'r') as f:
# 提取RGB图像、深度图和标注图
images = f['images'][:] # Nx3xWxH
depths = f['depths'][:] # NxWxH
labels = f['labels'][:] # NxWxH
# 假设取第一个样本进行可视化
image = images[0] # 选择第一个深度图像
depth = depths[0] # 选择第一个深度图像
label = labels[0] # 选择第一个深度图像
# image = images[0, :, :, :] # 选择第一个图像
# depth = depths[0, :, :] # 选择第一个深度图
# label = labels[0, :, :] # 选择第一个标注图
# 转换颜色空间
# 将图像数据从3xWxH转换为HxWx3
image = image.swapaxes(0, 2)
# 将深度和标注图数据从WxH转换为HxW
depth = depth.swapaxes(0, 1)
label = label.swapaxes(0, 1)
# 可视化
# 定义颜色映射
cmap = plt.cm.jet
# 可视化RGB图像
plt.figure(figsize=(12, 6))
plt.subplot(131)
plt.imshow(image)
plt.title('Original Image')
# plt.axis('off') # 不显示坐标轴
# 可视化深度图
depth_normalized = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
plt.subplot(132)
plt.imshow(depth_normalized, cmap=cmap, interpolation='nearest')
plt.title('Depth Map (Heatmap)')
# plt.axis('off') # 不显示坐标轴
# 可视化标注图
label_normalized = (label - np.min(label)) / (np.max(label) - np.min(label))
plt.subplot(133)
plt.imshow(label_normalized, cmap=cmap, interpolation='nearest')
plt.title('Label Map (Heatmap)')
# plt.axis('off') # 不显示坐标轴
# 显示图表
plt.tight_layout()
plt.show()
3. 保存RGB图/深度图/标注图
import h5py
import numpy as np
import os
from PIL import Image
# 指定.mat文件路径
mat_file_path = './data/nyu_depth_v2_labeled.mat'
# RGB图像、深度图和标注图的保存目录
images_path = './nyu_images/'
depths_path = './nyu_depths/'
labels_path = './nyu_labels/'
if not os.path.exists(images_path):
os.makedirs(images_path)
if not os.path.exists(depths_path):
os.makedirs(depths_path)
if not os.path.exists(labels_path):
os.makedirs(labels_path)
# 使用h5py打开.mat文件
with h5py.File(mat_file_path, 'r') as f:
# 提取RGB图像、深度图和标注图
print("Data extraction begin!")
images = f['images'][:] # HxWx3xN
depths = f['depths'][:] # NxWxH
labels = f['labels'][:] # NxWxH
# 保存RGB图像
for idx, image in enumerate(images):
image = image.swapaxes(0, 2)
image = Image.fromarray(image)
image.save(os.path.join(images_path, f'image_{idx}.png'))
print(f"{idx} image")
# 保存深度图像
for idx, depth in enumerate(depths):
depth = depth.swapaxes(0, 1)
# 深度值归一化
depth_normalized = (depth - np.min(depth)) / (np.max(depth) - np.min(depth))
# depth_normalized *= 255 # 转换到0-255范围
depth_image = Image.fromarray(np.uint8(depth_normalized), 'L')
depth_image.save(os.path.join(depths_path, f'depth_{idx}.png'))
print(f"{idx} depth_image")
# 保存标注图像
for idx, label in enumerate(labels):
label = label.swapaxes(0, 1)
label_image = Image.fromarray(label, 'L')
label_image.save(os.path.join(labels_path, f'label_{idx}.png'))
print(f"{idx} label_image")
print("Data extraction completed!")