moringa.py 3.82 KB
Newer Older
import sys
import argparse
import VHR.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 = VHR.segmentation.LSGRMParams(threshold, cw, sw, n_first_iter, margin)
    VHR.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):
    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)
        sp.pansharp()
        sp.write_outputs(out_fld)
    else:
        sp.write_outputs(out_fld)
    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')
Gaetano Raffaele's avatar
Gaetano Raffaele committed
    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')



    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)

    return 0

if __name__ == "__main__":
    main(sys.argv)