forked from hwiyoung/Orthophoto_Maps_Multispectral
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrthophotoMultiSpectral.py
180 lines (152 loc) · 7.91 KB
/
OrthophotoMultiSpectral.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
170
171
172
173
174
175
176
177
178
179
import os
import numpy as np
import cv2
import time
from ExifData import getMetadataExiv2
from EoData import readEOfromMetadata, convertCoordinateSystem, Rot3D
from Boundary import boundary
from BackprojectionResample import projectedCoord, backProjection,\
resampleThermal, createGeoTiffThermal
from system_calibration import calibrate
import gdal
import subprocess
import gdal2tiles
if __name__ == '__main__':
ground_height = 0 # unit: m
R_CB = np.array(
[[0.990635238726878, 0.135295782209043, 0.0183541578119133],
[-0.135993334134149, 0.989711806459606, 0.0444561944563446],
[-0.0121505910810649, -0.0465359159242159, 0.998842716179817]], dtype=float)
band = "bgrne" # blue, green, red, nir, redEdge
bandList_b_in = []
bandList_g_in = []
bandList_r_in = []
bandList_n_in = []
bandList_e_in = []
dstPath = '/internalCompany/PM2019007_nifs/DKC/gomso_stacks_orthophoto/'
# for root, dirs, files in os.walk('./tests/yeosu_stacks'):
for root, dirs, files in os.walk('/internalCompany/PM2019007_nifs/DKC/gomso_stacks_test'):
files.sort()
for file in files:
filename = os.path.splitext(file)[0]
extension = os.path.splitext(file)[1]
file_path = root + '/' + file
if extension == '.tiff' or extension == '.tif':
print('Read the image - ' + file)
image_start_time = time.time()
raster = gdal.Open(file_path, gdal.GA_ReadOnly)
# 1. Extract metadata from the image
focal_length, sensor_width = getMetadataExiv2(file_path) # unit: m, mm
# pixel_size = sensor_width / image_cols # unit: mm/px
pixel_size = 0.00375 # unit: mm/px
pixel_size = pixel_size / 1000 # unit: m/px
# For each band
for i in range(raster.RasterCount):
# 2. Extract an array from each band
print('Read the band (' + str(i+1) + ') in ' + file)
band_start_time = time.time()
start_time = time.time()
# https://gis.stackexchange.com/questions/32995/fully-load-raster-into-a-numpy-array
image = raster.GetRasterBand(i+1).ReadAsArray()
image_rows = image.shape[0]
image_cols = image.shape[1]
end_time = time.time()
print("--- %s seconds ---" % (time.time() - start_time))
# 3. Extract EOP from metadata of each band
print('Read EOP')
start_time = time.time()
print('Easting | Northing | Altitude | Roll | Pitch | Yaw')
eo = readEOfromMetadata(file_path)
eo = convertCoordinateSystem(eo)
print(eo)
# System Calibration
OPK = calibrate(eo[3], eo[4], eo[5], R_CB)
eo[3] = OPK[0]
eo[4] = OPK[1]
eo[5] = OPK[2]
print('Easting | Northing | Altitude | Omega | Phi | Kappa')
print(eo)
R = Rot3D(eo)
# 4. Extract a projected boundary of the image
print('boundary')
start_time = time.time()
bbox = boundary(image, eo, R, ground_height, pixel_size, focal_length)
print("--- %s seconds ---" % (time.time() - start_time))
# 5. Compute GSD & Boundary size
# GSD
gsd = (pixel_size * (eo[2] - ground_height)) / focal_length # unit: m/px
# Boundary size
boundary_cols = int((bbox[1, 0] - bbox[0, 0]) / gsd)
boundary_rows = int((bbox[3, 0] - bbox[2, 0]) / gsd)
# 6. Compute coordinates of the projected boundary
print('projectedCoord')
start_time = time.time()
proj_coords = projectedCoord(bbox, boundary_rows, boundary_cols, gsd, eo, ground_height)
print("--- %s seconds ---" % (time.time() - start_time))
# Image size
image_size = np.reshape(image.shape[0:2], (2, 1))
# 7. Back-projection into camera coordinate system
print('backProjection')
start_time = time.time()
backProj_coords = backProjection(proj_coords, R, focal_length, pixel_size, image_size)
print("--- %s seconds ---" % (time.time() - start_time))
# 8. Resample the pixels
print('resample')
start_time = time.time()
gray = resampleThermal(backProj_coords, boundary_rows, boundary_cols, image)
print("--- %s seconds ---" % (time.time() - start_time))
# 9. Create GeoTiff
print('Save the image in GeoTiff')
start_time = time.time()
dst = dstPath + filename + '_' + band[i]
createGeoTiffThermal(gray, bbox, gsd, boundary_rows, boundary_cols, dst)
print("--- %s seconds ---" % (time.time() - start_time))
if i == 0:
bandList_b_in.append(dst + '.tif')
elif i == 1:
bandList_g_in.append(dst + '.tif')
elif i == 2:
bandList_r_in.append(dst + '.tif')
elif i == 3:
bandList_n_in.append(dst + '.tif')
else:
bandList_e_in.append(dst + '.tif')
print('*** Processing time per each image')
print("--- %s seconds ---" % (time.time() - band_start_time))
print(filename + ' is processed!\n')
# 10. Mosaic individual orthophotos for each band
working_path1 = './OTB-7.0.0-Linux64/'
working_path2 = './bin/'
set_env = './otbenv.profile'
mosaic_execution = './otbcli_Mosaic'
os.chdir(working_path1) # change path
# https://stackoverflow.com/questions/13702425/source-command-not-found-in-sh-shell/13702876
subprocess.call(set_env, shell=True)
os.chdir(working_path2)
bandList_b_out = dstPath + '/IMG_b.tif'
bandList_g_out = dstPath + '/IMG_g.tif'
bandList_r_out = dstPath + '/IMG_r.tif'
bandList_n_out = dstPath + '/IMG_n.tif'
bandList_e_out = dstPath + '/IMG_e.tif'
subprocess.call(mosaic_execution + ' -il ' + ' '.join(bandList_b_in) +
' -out ' + bandList_b_out, shell=True)
subprocess.call(mosaic_execution + ' -il ' + ' '.join(bandList_g_in) +
' -out ' + bandList_g_out, shell=True)
subprocess.call(mosaic_execution + ' -il ' + ' '.join(bandList_r_in) +
' -out ' + bandList_r_out, shell=True)
subprocess.call(mosaic_execution + ' -il ' + ' '.join(bandList_n_in) +
' -out ' + bandList_n_out, shell=True)
subprocess.call(mosaic_execution + ' -il ' + ' '.join(bandList_e_in) +
' -out ' + bandList_e_out, shell=True)
# 11. Merge 3-band to one image
# https://gis.stackexchange.com/questions/44003/python-equivalent-of-gdalbuildvrt
vrt_options = gdal.BuildVRTOptions(resolution='average', resampleAlg='cubic', separate=True, VRTNodata=0)
my_vrt = gdal.BuildVRT(dstPath + '/IMG_RGB.vrt', [bandList_r_out, bandList_g_out, bandList_b_out],
options=vrt_options)
my_vrt = None
# https://gis.stackexchange.com/questions/42584/how-to-call-gdal-translate-from-python-code
ds = gdal.Translate(dstPath + '/IMG_RGB.tif', dstPath + '/IMG_RGB.vrt')
ds = None
# 12. Generate tiles
options = {'zoom': (14, 21)}
gdal2tiles.generate_tiles(dstPath + '/IMG_RGB.tif', dstPath + '/tiles/', **options)