2017-08-25 180 views
1

我正在使用.h5文件来存储大量图像数据。然后调整图像大小并将其存储在其中。图像从.h5文件加载图像数据,在Python 3.5和h5py中显示不寻常的颜色

创建数据集: t1=hdf5_file.create_dataset("train_img", train_shape, np.int8)

遍历图像地址调整,并将其储存:

for i in range(len(train_addrs)): 
    addr = train_addrs[i] 
    img = cv2.imread(addr) 
    img = cv2.resize(img, (128, 128), interpolation=cv2.INTER_CUBIC) 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) 
#save 
    hdf5_file["train_img"][i, ...] = img[None] 

hdf5_file.close() 

当我尝试检查的图像看起来像下面的代码:

hdf5_path = 'dataset.hdf5' 
train_dataset = h5py.File(hdf5_path, "r") 
train_set_x_orig = np.array(train_dataset["train_img"][:]) 
plt.imshow(train_set_x_orig[5]) #see 5th image 
plt.show() 

我得到这个不寻常的形象。 .h5文件中的第一个,底部是原始图像。我检查了一切的形状,他们很好。 resize代码cv2也行。任何形式的帮助将不胜感激。

After loading image from .h5 file

Original image

+0

什么是D型(S)?保存之前显示'img'好吗?这个'[i,...]'保存在'i'循环的内部还是外部? – hpaulj

+0

看来,.h5文件默认使用7位表示。 –

+0

'create_datset'中使用的dtype是'np.int8'。我单独检查了“resize”部分。那部分是好的。 ''我''循环里面有'[我,...]'。改变了。任何想法如何得到它的权利? @hpaulj – sayem48

回答

1

我打开一个B/W的scipy.misc

In [1354]: arr2 = misc.imread('../Desktop/newworld2.png') 
In [1355]: arr2.shape 
Out[1355]: (500, 778, 3) 
In [1356]: arr2.dtype 
Out[1356]: dtype('uint8') 

In [1343]: d2 =f.create_dataset('newworld set',shape=(2, *arr2.shape), dtype=np.uint8) 
In [1344]: d2[0]=arr2 
In [1345]: d2[1]=arr2 

其保存为png保存的原始

储蓄作为int8生产出了样的,你告诉

In [1348]: d3 =f.create_dataset('newworldbad',shape=(2, *arr2.shape), dtype=np.int8) 
In [1349]: d3[0]=arr2 
In [1350]: d3[0] 

白色像素的颜色变化,[255, 255, 255]变成灰色[127, 127, 127]

+0

是的,那正是发生了什么! – sayem48

相关问题