我正在尝试从单个numpy数组中的文件夹中读取多个图像,以将它们提供给我的深度学习模型。当我打印形状时,我得到一个错误消息,即没有类型的对象没有形状。这意味着图像没有被opencv(对于jpg)/ tifffile(对于.tif)读取我的代码如下
import numpy as np
import cv2 as cv
import tifffile as tiff
filepath1 = "D:/Programs/transnet/dataset/train"
filepath2 = "D:/Programs/transnet/dataset/train_label"
img = np.ndarray((cv.imread(filepath1 + "*.jpg")))
clt = np.ndarray((tiff.imread(filepath2 + "*.tif")))
print(img.shape)
print(clt.shape)
我已经尝试过glob.glob,但是它不起作用。 我期望一个4维的rgb图像数组
答案 0 :(得分:1)
您可以使用os模块执行文件遍历操作,尤其是如下的os.listdir
和os.path.join
import os
def get_all_images(folder, ext):
all_files = []
#Iterate through all files in folder
for file in os.listdir(folder):
#Get the file extension
_, file_ext = os.path.splitext(file)
#If file is of given extension, get it's full path and append to list
if ext in file_ext:
full_file_path = os.path.join(folder, file)
all_files.append(full_file_path)
#Get list of all files
return all_files
filepath1 = "D:/Programs/transnet/dataset/train"
filepath2 = "D:/Programs/transnet/dataset/train_label"
#List of all jps and tif files
jpg_files = get_all_images(filepath1, 'jpg')
tif_files = get_all_images(filepath2, 'tif')
现在,一旦有了所有文件的列表,就可以遍历列表并打开图像。