我正在为一个超分辨率的深度学习网络开发一个定制的图像数据集。我将映像保存到磁盘中,并可以创建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()))培训数据集中的文件:“trainHR”、“trainLR”
LRset = trainDS['trainLR']
HRset = trainDS['trainHR']
print('LR dataset shape: ', LRset.shape)
print('HR dataset shape: ', HRset.shape)LR数据集形状:(450,150,150,3) HR数据集形状:(450,600,600,3)
我的问题是,当我试图从数据集中查看单个图像时,我会看到一个黑匣子,它告诉我图像要么没有保存,要么没有正确加载。
cv2_imshow('', HRset[100])

我的代码是基于这个post的。代码运行时没有错误--我可以编写f5文件,读取它们并打印文件属性。我只是看不见图像,没有错误信息,我不知道我哪里出错了。
我猜这是一个简单的错误,我没有看到,但我希望你能提供任何帮助。谢谢!
发布于 2022-11-04 20:58:18
您是引用文章中一个小错误的受害者。(我很抱歉,我是那篇文章的作者。显然,没有人注意到创建具有错误dtype的数据集的错误。我更正了这个答案,以便从图像中获取dtype,然后在创建数据集时使用它。)
此外,您还需要在代码中添加一些行来查看图像。修复HDF5文件后,将这2行代码添加到代码中以查看图像。(同时,纠正imshow()上的小错误)。修改后的代码如下:
cv2.imshow('', HRset[100])
cv2.waitKey(0) # waits until a key is pressed in the image window
cv2.destroyAllWindows() # destroys the window showing the image这些台词是干什么用的?
cv2.waitKey(0)暂停执行您的程序。因此,图像窗口将保持可见。如果不包括此语句,则cv2.imshow()将在眨眼之间执行,程序将关闭它打开的所有窗口。这使得你不太可能看到窗口中的图像。(您发布的黑色图像是图像窗口的残留物。)
https://stackoverflow.com/questions/74319305
复制相似问题