代码如图:
#将10张 .png 图合并为一个.npy文件
import cv2
import os
import numpy
image_dir = 'D:/workspace/Matlab/MSI_data/通道选择后的数据集/'
savepath = 'D:/workspace/Matlab/MSI_data/npy_10/'
image_list = os.listdir(image_dir)
image_num = 10
for image_name in image_list:
image_mer = None
#os.mkdir(os.path.join(savepath,image_name))
print("image_name=",image_name)
for i in range(image_num):
image_path = os.path.join(image_dir, image_name,'%s_%d.png'%(image_name, i+1))
print("image_path=",image_path)
image = cv2.imread(image_path,0)
print("image.shape=",image.shape)
if i == 0:
image_mer = image
else:
image_mer = cv2.merge([image_mer, image])
print(image_mer.shape)
numpy.save(os.path.join(savepath,'%s.npy'%image_name),image_mer)
报错如下:
Traceback
原因如下:
image_path = os.path.join(image_dir, image_name,'%s_%d.png'%(image_name, i+1))
载入图像地址的格式不对,例如图像的名字是balloons_ms_11.png, 代码要与其对应
读取文件的时候路径不要有中文