-
Notifications
You must be signed in to change notification settings - Fork 0
/
processTiff.py
executable file
·164 lines (137 loc) · 5.44 KB
/
processTiff.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
"""
processTiff.py
Zhiang Chen, Feb 2019
Copyright (c) 2018 Distributed Robotic Exploration and Mapping Systems Laboratory, ASU
GeoTiff splitting is done by gdal_translate https://www.gdal.org/gdal_translate.html
"""
import os
import gdal
import cv2
import pickle
import numpy as np
from osgeo import gdal
from osgeo import osr
class splitTiff(object):
def __init__(self):
pass
def readTiff(self, tif):
ds = gdal.Open(tif)
band = ds.GetRasterBand(1)
self.tif = tif
self.xsize = band.XSize
self.ysize = band.YSize
os.system("gdalinfo " + tif)
def split(self, tile_x, tile_y, overlap_x, overlap_y, save_folder):
for i in range(0, self.xsize, tile_x - overlap_x):
for j in range(0, self.ysize, tile_y - overlap_y):
com_string = "gdal_translate -of GTIFF -srcwin " + str(i) + ", " + str(j) + ", " + str(
tile_x) + ", " + str(tile_y) + " " + str(self.tif) + " " + str(
save_folder) + str(i) + "_" + str(j) + ".tif"
os.system(com_string)
def convert2png(self, tif_folder, save_folder, rename = False):
tif_files = [f for f in os.listdir(tif_folder) if f.endswith(".tif")]
file_dict = dict()
for i, tif in enumerate(tif_files):
f = tif_folder + tif
img = cv2.imread(f)
if rename is False:
out_f = save_folder + tif.split('.')[0] + '.png'
cv2.imwrite(out_f, img)
if rename is True:
out_f = save_folder + str(i) + '.png'
cv2.imwrite(out_f, img)
file_dict.setdefault(str(i) + '.png', tif)
if rename is True:
with open(save_folder + "png2tiff_dict", 'wb') as f:
pickle.dump(file_dict, f)
def convert2jpg(self, tif_folder, save_folder, rename = False):
tif_files = [f for f in os.listdir(tif_folder) if f.endswith(".tif")]
file_dict = dict()
for i, tif in enumerate(tif_files):
f = tif_folder + tif
img = cv2.imread(f)
if rename is False:
out_f = save_folder + tif.split('.')[0] + '.jpg'
cv2.imwrite(out_f, img)
if rename is True:
out_f = save_folder + str(i) + '.jpg'
cv2.imwrite(out_f, img)
file_dict.setdefault(str(i) + '.jpg', tif)
if rename is True:
with open(save_folder + "jpg2tiff_dict", 'wb') as f:
pickle.dump(file_dict, f)
def lookupTiff(self, pickle_f, png):
with open(pickle_f, 'rb') as f:
file_dict = pickle.load(f)
print(file_dict[png])
class catTiff(object):
def __init__(self):
pass
def readImages(self, folder, prefix):
self.folder = folder
self.prefix = prefix
self.images = [self.folder+x for x in os.listdir(self.folder) if x.startswith(self.prefix)]
self.names = [x.split(self.prefix)[-1] for x in os.listdir(self.folder) if x.startswith(self.prefix)]
def cat(self, overlap_x, overlap_y, resize=None, detect_boundary=False):
dim = cv2.imread(self.images[0]).shape
y, x = dim[:2]
if resize != 0:
y, x = resize
suffix = self.images[0].split('.')[-1]
ul = list()
for name in self.names:
name = name.split('.')[0].split('_')
upper_left = [int(name[0]), int(name[1])]
ul.append(upper_left)
ul = np.asarray(ul)
min_x = np.min(ul[:,0])
min_y = np.min(ul[:,1])
max_x = np.max(ul[:,0])
max_y = np.max(ul[:,1])
X = max_x-min_x+x
Y = max_y-min_y+y
IMAGE = np.zeros((Y, X, 3))
print(IMAGE.shape)
for i in range(min_x, max_x, x-overlap_x):
for j in range(min_y, max_y, y-overlap_y):
name = self.folder + self.prefix + str(i) + '_' + str(j) + '.' + suffix
image = cv2.imread(name)
if resize != None:
image = cv2.resize(image,dsize=resize)
if not detect_boundary:
IMAGE[j:j+y, i:i+x, :] = image
return IMAGE
def writeTiff(self, data, folder, name, metadata_source, copy_meta=True):
if copy_meta:
y,x,c = data.shape
ds = gdal.Open(metadata_source)
md = ds.GetMetadata()
gt = ds.GetGeoTransform()
pj = ds.GetProjection()
driver = gdal.GetDriverByName('GTiff')
dataset = driver.Create(
folder+name,
x,
y,
c,
gdal.GDT_Byte)
dataset.SetGeoTransform(gt)
dataset.SetMetadata(md)
dataset.SetProjection(pj)
dataset.GetRasterBand(1).WriteArray(data[:,:,0])
dataset.GetRasterBand(2).WriteArray(data[:,:,1])
dataset.GetRasterBand(3).WriteArray(data[:,:,2])
dataset.FlushCache()
if __name__ == "__main__":
st = splitTiff()
#st.readTiff("./datasets/C3/C3.tif")
#st.split(400, 400, 10, 10, "./datasets/C3/split/")
st.convert2jpg("./datasets/C3/split/", "./datasets/C3/valid/")
#st.lookupTiff("./datasets/C3/valid/png2tiff_dict", '3.png')
"""
from processTiff import catTiff
ct = catTiff()
ct.readImages("./datasets/C3/masks/","masked_")
Image = ct.cat(10,10,resize=(400,400))
ct.writeTiff(Image, "./datasets/C3/", "C3_mask.tif", "./datasets/C3/C3.tif")
"""