Hello Mat

 找回密码
 立即注册
查看: 5566|回复: 3

读取文件夹内图像--方法2

[复制链接]

84

主题

115

帖子

731

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
1467
发表于 2019-7-26 22:04:50 | 显示全部楼层 |阅读模式
读取文件夹内图像:
供深度学习算法调用:Pytorch_Unet图像分割
  1. import torch.utils.data as data
  2. import PIL.Image as Image
  3. import os

  4. def make_dataset_txt(root, txt_file):
  5.     imgs=[]
  6.     with open(txt_file) as f:
  7.         indexs=f.readlines()
  8.     for i in range( len(indexs) ):
  9.         index = str( indexs[i] ).split('.')[0]
  10.         img=os.path.join(root, index+".jpg")
  11.         mask=os.path.join(root,index+".png")
  12.         imgs.append((img,mask))
  13.     return imgs

  14. class LiverDataset(data.Dataset):
  15.     def __init__(self, root, txt_file, transform=None, target_transform=None):
  16.         imgs = make_dataset_txt(root, txt_file)
  17.         self.imgs = imgs
  18.         self.transform = transform
  19.         self.target_transform = target_transform

  20.     def __getitem__(self, index):
  21.         x_path, y_path = self.imgs[index]
  22.         img_x = Image.open(x_path)
  23.         img_y = Image.open(y_path)
  24.         if self.transform is not None:
  25.             img_x = self.transform(img_x)
  26.         if self.target_transform is not None:
  27.             img_y = self.target_transform(img_y)
  28.         return img_x, img_y

  29.     def __len__(self):
  30.         return len(self.imgs)
复制代码
txt_file:
  1. 000_label.png
  2. 001_label.png
  3. 002_label.png
  4. 003_label.png
  5. 004_label.png
  6. 005_label.png
  7. 006_label.png
  8. 007_label.png
  9. 008_label.png
  10. 009_label.png
  11. 010_label.png
  12. 011_label.png
  13. ……
复制代码





回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-4-24 06:43 , Processed in 0.234351 second(s), 24 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表