Skip to content

Commit ce0c9c1

Browse files
batch_ortho (#34)
Hi Saif, I added the batch ortho function to the code. I also made two small modification to the correlate and batch_correlate sub_parser so that the output products go to the current directory. I made additional edits to gcpOptimization.py so that each GGP file has the name of the images (more practical when doing a batch ortho). I am also trying to get the file "CorregistrationError.png" (produced in geoTiePoints/misc.py) to have the image name included, but I could not manage yet. If you could take a look, that would be helpful. Cheers, Solene Signed-off-by: soleneantoine1 <[email protected]>
1 parent 6eac68c commit ce0c9c1

File tree

1 file changed

+120
-7
lines changed

1 file changed

+120
-7
lines changed

scripts/cosicorr.py

Lines changed: 120 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,98 @@ def ortho_func(args):
7171
orthorectify(args.input_img, o_ortho_path, ortho_params, None, args.dem, args.refine,
7272
args.gcps, args.ref_img, args.debug, args.show)
7373

74+
def batch_ortho_func(args):
75+
geoCosiCorr3DLog('Batch Orthorectification', os.getcwd())
76+
logging.info(f'Executing batch orthorectification module :{args}')
77+
78+
if args.base_images.startswith('"'): # for a string pattern including multiple images
79+
print("Input is a string")
80+
base_images = []
81+
for pattern in args.base_images.split(','):
82+
base_images.extend(glob.glob(pattern))
83+
base_rpcs = []
84+
for pattern in args.base_rpcs.split(','):
85+
base_rpcs.extend(glob.glob(pattern))
86+
if len(base_images) != len(base_rpcs):
87+
raise ValueError(f"Input_base_images:{len(base_images)} and base_rpcs:{len(base_rpcs)} -- "
88+
f"The number of base images and RPCs must be equal when using the --serial option.")
89+
print(base_images)
90+
print(base_rpcs)
91+
print(args.sat_id)
92+
93+
with open('error_log.txt', 'w') as log:
94+
for base_image, base_rpc in zip(base_images, base_rpcs):
95+
try:
96+
print(f"Processing {base_image} {base_rpc}")
97+
ortho_func(argparse.Namespace(input_img=base_image,
98+
o_ortho=args.o_ortho,
99+
dem=args.dem,
100+
refine=args.refine,
101+
gcps=args.gcps,
102+
gsd=args.gsd,
103+
ref_img=args.ref_img,
104+
debug=args.debug,
105+
show=args.show,
106+
model_name=args.model_name,
107+
rsm_fn=base_rpc,
108+
sat_id=args.sat_id,
109+
corr_model=args.corr_model,
110+
resampling_method=args.resampling_method,
111+
))
112+
except Exception as e:
113+
log.write(f"Error with {f}: {e}\n")
114+
log.write(traceback.format_exc() + "\n")
115+
116+
elif args.base_images.endswith('.txt'): # for a list of input data
117+
print("Input is a text file")
118+
base_images = []
119+
with open(args.base_images, 'r') as f:
120+
for line in f:
121+
pattern = line.strip() # Remove leading/trailing whitespace/newlines
122+
if pattern: # Skip empty lines
123+
base_images.extend(glob.glob(pattern))
124+
base_rpcs = []
125+
with open(args.base_rpcs, 'r') as f:
126+
for line in f:
127+
pattern = line.strip() # Remove leading/trailing whitespace/newlines
128+
if pattern: # Skip empty lines
129+
base_rpcs.extend(glob.glob(pattern))
130+
sensors = []
131+
with open(args.sat_id, 'r') as f:
132+
for line in f:
133+
pattern = line.strip()
134+
if pattern:
135+
sensors.append(pattern)
136+
if len(base_images) != len(base_rpcs):
137+
raise ValueError(f"Input_base_images:{len(base_images)} and base_rpcs:{len(base_rpcs)} -- "
138+
f"The number of base images and RPCs must be equal when using the --serial option.")
139+
print(base_images)
140+
print(base_rpcs)
141+
print(sensors)
142+
143+
with open('error_log.txt', 'w') as log:
144+
for base_image, base_rpc, sensor in zip(base_images, base_rpcs, sensors):
145+
try:
146+
print(f"Processing {base_image} {base_rpc} {sensor}")
147+
ortho_func(argparse.Namespace(input_img=base_image,
148+
o_ortho=args.o_ortho,
149+
dem=args.dem,
150+
refine=args.refine,
151+
gcps=args.gcps,
152+
gsd=args.gsd,
153+
ref_img=args.ref_img,
154+
debug=args.debug,
155+
show=args.show,
156+
model_name=args.model_name,
157+
rsm_fn=base_rpc,
158+
sat_id=sensor,
159+
corr_model=args.corr_model,
160+
resampling_method=args.resampling_method,
161+
))
162+
except Exception as e:
163+
log.write(f"Error with {f}: {e}\n")
164+
log.write(traceback.format_exc() + "\n")
165+
74166

75167
# ----------------------------------------------------------------
76168
# -------------------------- TRANSFORMATION ----------------------
@@ -384,6 +476,30 @@ def ortho_subparser(subparsers):
384476
"are supported in GeoCosiCorr3D.pro.")
385477
rsm_parser.set_defaults(func=ortho_func)
386478

479+
def batch_ortho_subparser(subparsers):
480+
batch_ortho_parser = subparsers.add_parser('batch_ortho', help='batch orthorectification process')
481+
batch_ortho_parser.add_argument('base_images', type=str, help='Input files for batch ortho')
482+
batch_ortho_parser.add_argument('--o_ortho', type=str, default=os.getcwd(), help='output files for batch ortho')
483+
batch_ortho_parser.add_argument('--corr_model', type=str, default=None, help='Correction model path (None)')
484+
batch_ortho_parser.add_argument('--dem', type=str, default=None, help='DEM path (None)')
485+
batch_ortho_parser.add_argument('--gsd', type=float, default=None, help='Output file for ortho (None)')
486+
batch_ortho_parser.add_argument('--resampling_method', type=str, default=C.Resampling_Methods.SINC,
487+
choices=C.GEOCOSICORR3D_RESAMLING_METHODS, help='Resampling method (SINC)')
488+
batch_ortho_parser.add_argument("--debug", action="store_true")
489+
batch_ortho_parser.add_argument("--show", action="store_true")
490+
batch_ortho_parser.add_argument("--refine", action="store_true",
491+
help="Refine model, this require GCPs or reference imagery to collect GCPs")
492+
batch_ortho_parser.add_argument('--ref_img', type=str, default=None, help='Reference Ortho image (None)')
493+
batch_ortho_parser.add_argument('--gcps', type=str, default=None, help='GCPs file (None)')
494+
model_subparsers = batch_ortho_parser.add_subparsers(title='model', dest='model_name', metavar='<model_name>',
495+
required=True)
496+
rfm_parser = model_subparsers.add_parser(C.SATELLITE_MODELS.RFM, help="RFM model specific arguments")
497+
rfm_parser.add_argument('rfm_fn', type=str, help="RFM file name (.tiff or .TXT)")
498+
rfm_parser.set_defaults(func=ortho_func)
499+
rsm_parser = model_subparsers.add_parser(C.SATELLITE_MODELS.RSM, help="RSM model specific arguments")
500+
rsm_parser.add_argument('sat_id', type=str, choices=C.GEOCOSICORR3D_SENSORS_LIST, help="Sat-name")
501+
rsm_parser.add_argument('base_rpcs', type=str, help="list of .xml or DIM file")
502+
rsm_parser.set_defaults(func=batch_ortho_func)
387503

388504
def transform_subparser(subparsers):
389505
transform_parser = subparsers.add_parser('transform', help='Transformation')
@@ -415,7 +531,7 @@ def correlate_subparser(subparsers):
415531
correlate_parser.add_argument("target_image", type=str, help="Path to the target image.")
416532
correlate_parser.add_argument("--base_band", type=int, default=1, help="Base image band.")
417533
correlate_parser.add_argument("--target_band", type=int, default=1, help="Target image band.")
418-
correlate_parser.add_argument("--output_path", type=str, default=C.SOFTWARE.WKDIR, help="Output correlation path.")
534+
correlate_parser.add_argument("--output_path", type=str, default=os.getcwd(), help="Output correlation path.")
419535
correlate_parser.add_argument("--method", type=str,
420536
choices=[C.CORR_METHODS.FREQUENCY_CORR.value, C.CORR_METHODS.SPATIAL_CORR.value],
421537
default=C.CORR_METHODS.FREQUENCY_CORR.value,
@@ -452,7 +568,7 @@ def batch_correlate_subparser(subparsers):
452568
batch_correlate_parser.add_argument("--all", action="store_true", help="Correlate all possible combinations.")
453569
batch_correlate_parser.add_argument("--base_bands", type=int, nargs='+', help="List of base image bands.")
454570
batch_correlate_parser.add_argument("--target_bands", type=int, nargs='+', help="List of target image bands.")
455-
batch_correlate_parser.add_argument("--output_path", type=str, default=C.SOFTWARE.WKDIR,
571+
batch_correlate_parser.add_argument("--output_path", type=str, default=os.getcwd(),
456572
help="Output correlation path.")
457573
batch_correlate_parser.add_argument("--method", type=str, choices=[C.CORR_METHODS.FREQUENCY_CORR.value,
458574
C.CORR_METHODS.SPATIAL_CORR.value],
@@ -518,22 +634,19 @@ def multiband_correlate_subparser(subparsers):
518634
def cosicorr():
519635
parser = argparse.ArgumentParser(prog='cosicorr3d', description='GeoCosiCorr3D CLI',
520636
formatter_class=argparse.ArgumentDefaultsHelpFormatter)
521-
522637
subparsers = parser.add_subparsers(title='modules', dest='module', metavar='<module>')
523-
524638
ortho_subparser(subparsers)
639+
batch_ortho_subparser(subparsers)
525640
transform_subparser(subparsers)
526641
correlate_subparser(subparsers)
527642
batch_correlate_subparser(subparsers)
528643
multiband_correlate_subparser(subparsers)
529-
530644
args = parser.parse_args()
531-
532645
if hasattr(args, 'func'):
533646
args.func(args)
534647
else:
535648
parser.print_help()
536649

537-
538650
if __name__ == '__main__':
539651
cosicorr()
652+

0 commit comments

Comments
 (0)