-
Gaetano Raffaele authorede73d0014
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
import sys
import argparse
import OBIA.segmentation
import VHR.vhrbase
def run_segmentation(img, threshold, cw, sw , out_seg,
n_first_iter, margin, n_proc, memory,
remove_graph, force_parallel):
params = OBIA.segmentation.LSGRMParams(threshold, cw, sw, n_first_iter, margin)
OBIA.segmentation.lsgrm(img, params, out_seg, n_proc, memory, None, remove_graph, force_parallel)
return
def preprocess_spot67(in_fld, out_fld, dem_fld, geoid_file, skip_ps, compress):
sp = VHR.vhrbase.SPOT67RasterPipeline(in_fld)
sp.to_toa()
sp.orthorectify(dem_fld, geoid_file)
if not skip_ps:
sp.write_outputs(out_fld, update_pipe=True, compress=compress)
sp.pansharp()
sp.write_outputs(out_fld, compress=compress)
else:
sp.write_outputs(out_fld, compress=compress)
return
def main(args):
parser = argparse.ArgumentParser(prog="moringa", add_help=False)
subpar = parser.add_subparsers(dest="cmd")
prepr = subpar.add_parser("preprocess", help="Performs basic time series preprocessing (unimplemented).",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
# Put arguments and options here
segmt = subpar.add_parser("segment", help="Performs (large scale Baatz-Shape) segmentation of an input image.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
segmt.add_argument("img", type=str, help="Path to the image to segment.")
segmt.add_argument("threshold", type=float, help="Threshold for the heterogeneity criterion in Baatz-Shape.")
segmt.add_argument("outimg", type=str, help="Path to the output segmentation file (.tif, .shp, .gpkg, .gml).")
segmt.add_argument("--cw", type=float, nargs="?", default=0.5, help="Color weight in Baatz-Shape criterion.")
segmt.add_argument("--sw", type=float, nargs="?", default=0.5, help="Spatial weight in Baatz-Shape criterion.")
segmt.add_argument("--n_first_iter", type=int, nargs="?", default=12, help="Number of iterations for parallel tile processing.")
segmt.add_argument("--tile_margin", type=int, nargs="?", default=100, help="Margin for tile overlap.")
segmt.add_argument("--n_proc", type=int, help="Number of cores to use.")
segmt.add_argument("--mem_limit", type=int, help="Memory limit in MB.")
segmt.add_argument("--keep_graph", help="Keep the graph files (.bin) after segmentation.", action='store_true')
segmt.add_argument("--force_parallel", help="Force the spot6/7 preprocess one-liner parallelization of the process even if the full graph fits in memory.", action='store_true')
vhrprep = subpar.add_parser("preprocess_spot67", help="Perform baseline pre-processing of a SPOT6/7 scene in raster sensor.",
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
vhrprep.add_argument("fld", type=str, help="Path to the root folder containing PAN and MS subfolders.")
vhrprep.add_argument("out_fld", type=str, help="Path to the output folder for preprocessed images.")
vhrprep.add_argument("dem_fld", type=str, help="Path to the folder containing DEM covering the scene in WGS84 projection.")
vhrprep.add_argument("geoid", type=str, help="Path to the geoid file.")
vhrprep.add_argument("--skip_ps", help="Skip pansharpening step.", action='store_true')
vhrprep.add_argument("--compress", help="Use lossless compresion on outputs.", action='store_true')
if len(args) == 1:
parser.print_help()
sys.exit(0)
arg = parser.parse_args()
if arg.cmd == "preprocess":
print('Not yet implemented.')
if arg.cmd == "segment":
run_segmentation(arg.img, arg.threshold, arg.cw, arg.sw, arg.outimg, arg.n_first_iter, arg.tile_margin,
arg.n_proc, arg.mem_limit, not arg.keep_graph, arg.force_parallel)
if arg.cmd == "preprocess_spot67":
preprocess_spot67(arg.fld, arg.out_fld, arg.dem_fld, arg.geoid, arg.skip_ps, arg.compress)
return 0
if __name__ == "__main__":
main(sys.argv)