Python加载自定义文件夹下的所有图像

 1. 文件格式形式

2. 读取选取的文件夹

# 数据集的根目录
img_path_input = r'val\input'
img_path_ph_A = r'val\ph_A'
img_path_ph_B = r'val\ph_B'

# 遍历该目录下的所有图片文件
def read_directory(directory_name):
    array_of_img = []  # this if for store all of the image data
    for filename in os.listdir(directory_name):
        img = cv2.imread(directory_name + '/' + filename)
        ####change to gray
        # (下面第一行是将RGB转成单通道灰度图,第二步是将单通道灰度图转成3通道灰度图)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        #image_np = cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
        array_of_img.append(img)
    return array_of_img

image_input = read_directory(img_path_input)
image_ph_A = read_directory(img_path_ph_A)
image_ph_B = read_directory(img_path_ph_B)

3. 转为张量Tensor格式 

image_input = torch.Tensor(np.array(image_input))
image_ph_A = torch.Tensor(np.array(image_ph_A))
image_ph_B = torch.Tensor(np.array(image_ph_B))

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值