forked from micasense/imageprocessing
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_processing_script.py
More file actions
190 lines (159 loc) · 6.1 KB
/
Copy pathbatch_processing_script.py
File metadata and controls
190 lines (159 loc) · 6.1 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
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
180
181
182
183
184
185
186
187
188
189
190
import argparse
import os
import time
from pathlib import Path
import pandas as pd
from micasense.capture import Capture
from micasense.warp_io import load_warp_matrices
import micasense.imageset as imageset
def df_to_geojson(df, properties, lat="latitude", lon="longitude"):
geojson = {"type": "FeatureCollection", "features": []}
for _, row in df.iterrows():
feature = {
"type": "Feature",
"properties": {},
"geometry": {"type": "Point", "coordinates": [row[lon], row[lat]]},
}
for prop in properties:
feature["properties"][prop] = row[prop]
geojson["features"].append(feature)
return geojson
parser = argparse.ArgumentParser(
prog="MicaSenseBatchProcessing",
description="Create aligned, radiometrically corrected image stacks from raw MicaSense imagery",
epilog="epilog",
)
parser.add_argument("--imagepath", required=True, type=Path)
parser.add_argument("--outputpath", required=True, type=Path)
parser.add_argument("--panelpath", required=True, nargs="*")
parser.add_argument("--alignmentimage")
args = parser.parse_args()
print(args.imagepath)
print(args.panelpath)
pan_sharpen = True
use_dls = True
image_path = args.imagepath
# these will return lists of image paths as strings
# panelNames = list(imagePath.glob('IMG_0000_*.tif'))
# panelNames = [x.as_posix() for x in panelNames]
panel_names = args.panelpath
panelCap = Capture.from_filelist(panel_names)
# destinations on your computer to put the stacks
# and RGB thumbnails
outputPath = args.outputpath.resolve().as_posix()
print(outputPath)
thumbnailPath = args.outputpath / "thumbnails"
thumbnailPath = thumbnailPath.resolve().as_posix()
print(thumbnailPath)
cam_model = panelCap.camera_model
cam_serial = panelCap.camera_serial
# determine if this sensor has a panchromatic band
if cam_model == "RedEdge-P" or cam_model == "Altum-PT":
panchro_cam = True
else:
panchro_cam = False
pan_sharpen = False
# if this is a multicamera system like the RedEdge-MX Dual,
# we can combine the two serial numbers to help identify
# this camera system later.
if len(panelCap.camera_serials) > 1:
cam_serial = "_".join(panelCap.camera_serials)
print("Serial number:", cam_serial)
else:
cam_serial = panelCap.camera_serial
print("Serial number:", cam_serial)
overwrite = False # can be set to False to continue interrupted processing
generateThumbnails = True
# Allow this code to align both radiance and reflectance images; but excluding
# a definition for panelNames above, radiance images will be used
# For panel images, efforts will be made to automatically extract the panel information
# but if the panel/firmware is before Altum 1.3.5, RedEdge 5.1.7 the panel reflectance
# will need to be set in the panel_reflectance_by_band variable.
# Note: radiance images will not be used to properly create NDVI/NDRE images below.
if panel_names is not None:
panelCap = Capture.from_filelist(panel_names)
else:
panelCap = None
if panelCap is not None:
if panelCap.panel_albedo() is not None and not any(
v is None for v in panelCap.panel_albedo()
):
panel_reflectance_by_band = panelCap.panel_albedo()
else:
panel_reflectance_by_band = [0.49] * len(
panelCap.eo_band_names()
) # RedEdge band_index order
panel_irradiance = panelCap.panel_irradiance(panel_reflectance_by_band)
img_type = "reflectance"
else:
if use_dls:
img_type = "reflectance"
else:
img_type = "radiance"
imgset = imageset.ImageSet.from_directory(image_path)
data, columns = imgset.as_nested_lists()
df = pd.DataFrame.from_records(data, index="timestamp", columns=columns)
geojson_data = df_to_geojson(df, columns[3:], lat="latitude", lon="longitude")
if panchro_cam:
warp_matrices_filename = cam_serial + "_warp_matrices_SIFT.npy"
else:
warp_matrices_filename = cam_serial + "_warp_matrices_opencv.npy"
if Path("./" + warp_matrices_filename).is_file():
print("Found existing warp matrices for camera", cam_serial)
loaded = load_warp_matrices(warp_matrices_filename, as_projective=panchro_cam)
if panchro_cam:
warp_matrices_SIFT = loaded
else:
warp_matrices = loaded
print("Loaded warp matrices from", Path("./" + warp_matrices_filename).resolve())
else:
print("No warp matrices found at expected location:", warp_matrices_filename)
warp_matrices = None
warp_matrices_SIFT = None
if not os.path.exists(outputPath):
os.makedirs(outputPath)
if generateThumbnails and not os.path.exists(thumbnailPath):
os.makedirs(thumbnailPath)
# Save out geojson data, so we can open the image capture locations in our GIS
with open(os.path.join(outputPath, "imageSet.json"), "w") as f:
f.write(str(geojson_data))
try:
irradiance = panel_irradiance + [0]
except NameError:
irradiance = None
start = time.time()
for i, capture in enumerate(imgset.captures):
outputFilename = capture.uuid + ".tif"
thumbnailFilename = capture.uuid + ".jpg"
fullOutputPath = os.path.join(outputPath, outputFilename)
fullThumbnailPath = os.path.join(thumbnailPath, thumbnailFilename)
if (not os.path.exists(fullOutputPath)) or overwrite:
if len(capture.images) == len(imgset.captures[0].images):
if panchro_cam:
capture.radiometric_pan_sharpened_aligned_capture(
warp_matrices=warp_matrices_SIFT, irradiance_list=irradiance
)
else:
capture.create_aligned_capture(
irradiance_list=irradiance, warp_matrices=warp_matrices
)
capture.save_capture_as_stack(
fullOutputPath, pansharpen=pan_sharpen, sort_by_wavelength=False
)
if generateThumbnails:
capture.save_capture_as_rgb(fullThumbnailPath)
current = time.time()
diff = current - start
print(
"Saved stack",
str(i),
"of",
str(len(imgset.captures)),
"in",
str(int(diff)),
"seconds",
end="\r",
)
capture.clear_image_data()
end = time.time()
print("Saving time:", end - start)