自定义超分辨率图像HDF5数据集保存后无法显示图像求助
超分辨率数据集HDF5存储后图像显示黑框问题排查
我正在为超分辨率深度学习网络开发自定义图像数据集,已将图像保存到磁盘并能生成HDF5数据集文件,使用的代码如下:
import os, cv2, h5py, glob import numpy as np from glob import glob # define the paths to the dataset BASE_DATA_PATH = '/usr/local/home/.../esrgan_data' HR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_HR') LR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_LR') # create LR and HR image lists LR_images = glob(LR_TRAIN_PATH + '**/*.png') HR_images = glob(HR_TRAIN_PATH + '**/*.png') # sort the lists LR_images.sort() HR_images.sort() # create an h5 file with h5py.File('datasets/esrgan_trainDS.h5', 'w') as h5_file: # create 2 datasets for LR and HR images in the h5 file lr_ds = h5_file.create_dataset('trainLR', (len(LR_images), 150, 150, 3), dtype='f') hr_ds = h5_file.create_dataset('trainHR', (len(HR_images), 600, 600, 3), dtype='f') for i in range(len(LR_images)): LR_image = cv2.imread(LR_images[i]) HR_image = cv2.imread(HR_images[i]) lr_trainDS[i] = LR_image hr_trainDS[i] = HR_image # load the h5 dataset trainDS = h5py.File('datasets/esrgan_trainDS.h5', 'r') print('Files in the training dataset: ', list(trainDS.keys()))
运行后输出:
Files in the training dataset: ['trainHR', 'trainLR']
执行以下代码查看数据集形状:
LRset = trainDS['trainLR'] HRset = trainDS['trainHR'] print('LR dataset shape: ', LRset.shape) print('HR dataset shape: ', HRset.shape)
输出结果:
LR dataset shape: (450, 150, 150, 3) HR dataset shape: (450, 600, 600, 3)
但尝试查看数据集中的单张图像时,显示为全黑框:
cv2_imshow('', HRset[100])
代码无报错,可正常读写HDF5文件并打印属性,但无法正常显示图像。
问题排查与修复方案
1. 核心问题:变量名笔误导致数据未写入
代码中创建HDF5数据集时用的变量是lr_ds和hr_ds,但循环写入数据时错误使用了未定义的lr_trainDS和hr_trainDS。这会导致HDF5数据集仅被初始化了形状,但实际没有写入任何图像数据,所有值都是默认的0,因此显示为黑框。
2. 数据类型与显示格式问题
- cv2读取的图像是
uint8类型(像素值范围0-255),直接存储为float类型会导致数据范围不匹配,建议归一化到0-1区间; - 读取显示时需要将float类型转换回
uint8类型,同时注意cv2默认读取的是BGR格式,显示时可转换为RGB适配常规显示逻辑。
修复后的完整代码
import os, cv2, h5py, glob import numpy as np from glob import glob # define the paths to the dataset BASE_DATA_PATH = '/usr/local/home/.../esrgan_data' HR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_HR') LR_TRAIN_PATH = os.path.join(BASE_DATA_PATH, 'train_LR') # create LR and HR image lists(添加recursive=True确保遍历子文件夹) LR_images = glob(LR_TRAIN_PATH + '**/*.png', recursive=True) HR_images = glob(HR_TRAIN_PATH + '**/*.png', recursive=True) # sort the lists LR_images.sort() HR_images.sort() # 校验LR和HR图像数量是否匹配 assert len(LR_images) == len(HR_images), "LR和HR图像数量不匹配" # create an h5 file with h5py.File('datasets/esrgan_trainDS.h5', 'w') as h5_file: # create 2 datasets for LR and HR images in the h5 file lr_ds = h5_file.create_dataset('trainLR', (len(LR_images), 150, 150, 3), dtype='float32') hr_ds = h5_file.create_dataset('trainHR', (len(HR_images), 600, 600, 3), dtype='float32') for i in range(len(LR_images)): # 读取图像,确保读取成功 LR_image = cv2.imread(LR_images[i]) HR_image = cv2.imread(HR_images[i]) assert LR_image is not None, f"无法读取LR图像:{LR_images[i]}" assert HR_image is not None, f"无法读取HR图像:{HR_images[i]}" # 归一化到0-1范围,转换为float32 lr_ds[i] = LR_image.astype(np.float32) / 255.0 hr_ds[i] = HR_image.astype(np.float32) / 255.0 # load the h5 dataset trainDS = h5py.File('datasets/esrgan_trainDS.h5', 'r') print('Files in the training dataset: ', list(trainDS.keys())) LRset = trainDS['trainLR'] HRset = trainDS['trainHR'] print('LR dataset shape: ', LRset.shape) print('HR dataset shape: ', HRset.shape) # 查看图像的正确方式 sample_hr = HRset[100] # 将0-1的float转换回0-255的uint8,并转换BGR到RGB sample_hr = (sample_hr * 255).astype(np.uint8) sample_hr_rgb = cv2.cvtColor(sample_hr, cv2.COLOR_BGR2RGB) cv2_imshow('Sample HR Image', sample_hr_rgb)
内容的提问来源于stack exchange,提问作者west42
相关产品推荐
相关产品推荐

