-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPeDataSet.py
More file actions
48 lines (38 loc) · 1.34 KB
/
Copy pathPeDataSet.py
File metadata and controls
48 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
import os
from PIL import Image
from torch.utils.data import Dataset
from torchvision import transforms
class PeDataSet(Dataset):
"""
加载光弹性数据集
"""
def __init__(self, rootdir, transform=None, getname=None):
self.rootdir = rootdir
self.fringes = os.listdir(rootdir + '/fringes')
self.transform = transform
self.getname = getname
def __len__(self):
return len(self.fringes)
def __getitem__(self, index):
fringeindex = self.fringes[index] # 根据索引获取图片路径
imgnames = fringeindex.split('.')[0]
img = Image.open(self.rootdir + '/fringes/' + fringeindex)
img = self.transform(img)
stressmapIndex = 'Target_' + fringeindex.split('_')[1]
stressmap = Image.open(self.rootdir + '/stressmaps/' + stressmapIndex)
stressmap = self.transform(stressmap)
if self.getname:
return img, imgnames
else:
return img, stressmap
if __name__ == "__main__":
trans = transforms.ToTensor()
datasets = PeDataSet("pe_data", transform=trans)
img10, stress = datasets[100]
import matplotlib.pyplot as plt
plt.subplot(121)
plt.imshow(img10.permute(1, 2, 0))
plt.subplot(122)
plt.imshow(stress.permute(1, 2, 0), cmap="gray")
# plt.title(f"{imgname}")
plt.show()