-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathBackprojectionResample.py
123 lines (98 loc) · 4.92 KB
/
BackprojectionResample.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
import numpy as np
from numba import jit
from osgeo import gdal, osr
@jit(nopython=True)
def projectedCoord(boundary, boundary_rows, boundary_cols, gsd, eo, ground_height):
proj_coords = np.empty(shape=(3, boundary_rows * boundary_cols))
i = 0
for row in range(boundary_rows):
for col in range(boundary_cols):
proj_coords[0, i] = boundary[0, 0] + col * gsd - eo[0]
proj_coords[1, i] = boundary[3, 0] - row * gsd - eo[1]
i += 1
proj_coords[2, :] = ground_height - eo[2]
return proj_coords
def backProjection(coord, R, focal_length, pixel_size, image_size):
coord_CCS_m = np.dot(R, coord) # unit: m 3 x (row x col)
scale = (coord_CCS_m[2]) / (-focal_length) # 1 x (row x col)
plane_coord_CCS = coord_CCS_m[0:2] / scale # 2 x (row x col)
# Convert CCS to Pixel Coordinate System
coord_CCS_px = plane_coord_CCS / pixel_size # unit: px
coord_CCS_px[1] = -coord_CCS_px[1]
coord_out = image_size[::-1] / 2 + coord_CCS_px # 2 x (row x col)
return coord_out
@jit(nopython=True)
def resample(coord, boundary_rows, boundary_cols, image):
# Define channels of an orthophoto
b = np.zeros(shape=(boundary_rows, boundary_cols), dtype=np.uint8)
g = np.zeros(shape=(boundary_rows, boundary_cols), dtype=np.uint8)
r = np.zeros(shape=(boundary_rows, boundary_cols), dtype=np.uint8)
a = np.zeros(shape=(boundary_rows, boundary_cols), dtype=np.uint8)
rows = np.reshape(coord[1], (boundary_rows, boundary_cols))
cols = np.reshape(coord[0], (boundary_rows, boundary_cols))
rows = rows.astype(np.int16)
#rows = np.int16(rows)
cols = cols.astype(np.int16)
for row in range(boundary_rows):
for col in range(boundary_cols):
if cols[row, col] < 0 or cols[row, col] >= image.shape[1]:
continue
elif rows[row, col] < 0 or rows[row, col] >= image.shape[0]:
continue
else:
b[row, col] = image[rows[row, col], cols[row, col]][0]
g[row, col] = image[rows[row, col], cols[row, col]][1]
r[row, col] = image[rows[row, col], cols[row, col]][2]
a[row, col] = 255
return b, g, r, a
def createGeoTiff(b, g, r, a, boundary, gsd, rows, cols, dst):
# https://stackoverflow.com/questions/33537599/how-do-i-write-create-a-geotiff-rgb-image-file-in-python
geotransform = (boundary[0], gsd, 0, boundary[3], 0, -gsd)
# create the 4-band(RGB+Alpha) raster file
dst_ds = gdal.GetDriverByName('GTiff').Create(dst + '.tif', cols, rows, 4, gdal.GDT_Byte)
dst_ds.SetGeoTransform(geotransform) # specify coords
# Define the TM central coordinate system (EPSG 5186)
srs = osr.SpatialReference() # establish encoding
srs.ImportFromEPSG(5186)
# srs.ImportFromEPSG(3857)
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(r) # write r-band to the raster
dst_ds.GetRasterBand(2).WriteArray(g) # write g-band to the raster
dst_ds.GetRasterBand(3).WriteArray(b) # write b-band to the raster
dst_ds.GetRasterBand(4).WriteArray(a) # write a-band to the raster
dst_ds.FlushCache() # write to disk
dst_ds = None
@jit(nopython=True)
def resampleThermal(coord, boundary_rows, boundary_cols, image):
# Define channels of an orthophoto
gray = np.zeros(shape=(boundary_rows, boundary_cols))
rows = np.reshape(coord[1], (boundary_rows, boundary_cols))
cols = np.reshape(coord[0], (boundary_rows, boundary_cols))
rows = rows.astype(np.int16)
cols = cols.astype(np.int16)
for row in range(boundary_rows):
for col in range(boundary_cols):
if cols[row, col] < 0 or cols[row, col] >= image.shape[1]:
continue
elif rows[row, col] < 0 or rows[row, col] >= image.shape[0]:
continue
else:
gray[row, col] = image[rows[row, col], cols[row, col]]
return gray
def createGeoTiffThermal(grey, boundary, gsd, rows, cols, dst):
# https://stackoverflow.com/questions/33537599/how-do-i-write-create-a-geotiff-rgb-image-file-in-python
geotransform = (boundary[0], gsd, 0, boundary[3], 0, -gsd)
# create the 4-band(RGB+Alpha) raster file
dst_ds = gdal.GetDriverByName('GTiff').Create(dst + '.tif', cols, rows, 1, gdal.GDT_Float32)
dst_ds.SetGeoTransform(geotransform) # specify coords
# Define the TM central coordinate system (EPSG 5186)
srs = osr.SpatialReference() # establish encoding
# srs.ImportFromEPSG(5186)
srs.ImportFromEPSG(3857)
dst_ds.SetProjection(srs.ExportToWkt()) # export coords to file
dst_ds.GetRasterBand(1).WriteArray(grey) # write gray-band to the raster
# https://gis.stackexchange.com/questions/220753/how-do-i-create-blank-geotiff-
# with-same-spatial-properties-as-existing-geotiff
dst_ds.GetRasterBand(1).SetNoDataValue(0)
dst_ds.FlushCache() # write to disk
dst_ds = None