forked from NanNanmei/BFINet
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dataset.py
171 lines (131 loc) · 4.94 KB
/
dataset.py
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""
The role of this file completes the data reading
"""
import torch
import numpy as np
import cv2
from PIL import Image, ImageFile
from skimage import io
import imageio
from torch.utils.data import Dataset
from torchvision import transforms
from scipy import io
import os
from osgeo import gdal
import random
from skimage import io
def rand_flip_MCD(img1, label1, label2):
r = random.random()
# showIMG(img.transpose((1, 2, 0)))
if r < 0.25:
return img1, label1, label2
elif r < 0.5:
return np.flip(img1, axis=0).copy(), np.flip(label1, axis=0).copy(), np.flip(label2, axis=0).copy()
elif r < 0.75:
return np.flip(img1, axis=1).copy(), np.flip(label1, axis=1).copy(), np.flip(label2, axis=1).copy()
else:
return img1[::-1, ::-1, :].copy(), label1[::-1, ::-1].copy(), label2[::-1, ::-1].copy()
def rand_rot90_MCD(img1, label1, label2):
r = random.random()
# showIMG(img.transpose((1, 2, 0)))
if r < 0.5:
return img1, label1, label2
else:
return np.rot90(img1).copy(), np.rot90(label1).copy(), np.rot90(label2).copy()
def rand_rot90_flip_MCD(img1, label1, label2):
img1, label1, label2 = rand_rot90_MCD(img1, label1, label2)
return rand_flip_MCD(img1, label1, label2)
### Reading and saving of remote sensing images (Keep coordinate information)
def readTif(fileName, xoff = 0, yoff = 0, data_width = 0, data_height = 0):
dataset = gdal.Open(fileName)
if dataset == None:
print(fileName + "文件无法打开")
# 栅格矩阵的列数
width = dataset.RasterXSize
# 栅格矩阵的行数
height = dataset.RasterYSize
# 波段数
bands = dataset.RasterCount
# 获取数据
if(data_width == 0 and data_height == 0):
data_width = width
data_height = height
data = dataset.ReadAsArray(xoff, yoff, data_width, data_height)
# 获取仿射矩阵信息
geotrans = dataset.GetGeoTransform()
# 获取投影信息
proj = dataset.GetProjection()
return width, height, bands, data, geotrans, proj
#保存遥感影像
def writeTiff(im_data, im_geotrans, im_proj, path):
if 'int8' in im_data.dtype.name:
datatype = gdal.GDT_Byte
elif 'int16' in im_data.dtype.name:
datatype = gdal.GDT_UInt16
else:
datatype = gdal.GDT_Float32
if len(im_data.shape) == 3:
im_bands, im_height, im_width = im_data.shape
else:
im_bands, (im_height, im_width) = 1, im_data.shape
# 创建文件
driver = gdal.GetDriverByName("GTiff")
dataset = driver.Create(path, int(im_width), int(im_height), int(im_bands), datatype)
if (dataset != None):
dataset.SetGeoTransform(im_geotrans) # 写入仿射变换参数
dataset.SetProjection(im_proj) # 写入投影
if im_bands == 1:
dataset.GetRasterBand(1).WriteArray(im_data)
else:
for i in range(im_bands):
dataset.GetRasterBand(i + 1).WriteArray(im_data[i])
del dataset
#######
class Dataset_test(Dataset):
def __init__(self, file_names):
self.file_names = file_names
# self.distance_type = distance_type
# self.dir = dir
def __len__(self):
return len(self.file_names)
def __getitem__(self, idx):
img_file_name = self.file_names[idx]
image = load_image(img_file_name)
mask = load_mask(img_file_name)
contour = load_contour(img_file_name)
# dist = load_distance(os.path.join(self.dir,img_file_name+'.tif'), self.distance_type)
return img_file_name, image, mask, contour
###train_dataset
class DatasetImageMaskContourDist(Dataset):
def __init__(self, dir, file_names):
self.file_names = file_names
# self.distance_type = distance_type
self.dir = dir
def __len__(self):
return len(self.file_names)
def __getitem__(self, idx):
img_file_name = self.file_names[idx]
image = load_image(os.path.join(self.dir, img_file_name+'.tif'))
mask = load_mask(os.path.join(self.dir, img_file_name+'.tif'))
contour = load_contour(os.path.join(self.dir, img_file_name+'.tif'))
return img_file_name, image, mask, contour
def load_image(path):
img = Image.open(path)
data_transforms = transforms.Compose(
[
# transforms.Resize(256),
transforms.ToTensor(),
transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
img = data_transforms(img)
return img
def load_mask(path):
mask = io.imread(path.replace("image", "mask").replace("tif", "tif"), 0)
# im_width, im_height, im_bands, mask, im_geotrans, im_proj = readTif(path.replace("image", "mask").replace("tif", "tif"))
mask = mask/255.
return torch.from_numpy(np.expand_dims(mask, 0)).float()
def load_contour(path):
contour = io.imread(path.replace("image", "contour").replace("tif", "tif"), 0)
contour = contour/255.
return torch.from_numpy(np.expand_dims(contour, 0)).float()