Skip to content

Commit

Permalink
Updated code to only search for reference orthomosaics and for functi…
Browse files Browse the repository at this point in the history
…oning re-georeferencing with coregistration parameters
  • Loading branch information
havardlovas committed Apr 18, 2024
1 parent bce1bb3 commit c05ae5e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 24 deletions.
4 changes: 3 additions & 1 deletion gref4hsi/scripts/coregistration.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ def main(config_path, mode):

# Establish reference data
path_orthomosaic_reference_folder = config['Absolute Paths']['orthomosaic_reference_folder']
orthomosaic_reference_fn = os.listdir(path_orthomosaic_reference_folder)[0] # Grab the only file in folder
orthomosaic_reference_fn = [f for f in os.listdir(path_orthomosaic_reference_folder) if f.endswith('tif')][0] # Grab the only file in folder that ends with *.tif
ref_ortho_path = os.path.join(path_orthomosaic_reference_folder, orthomosaic_reference_fn)

dem_path = config['Absolute Paths']['dem_path']
Expand Down Expand Up @@ -494,6 +494,8 @@ def main(config_path, mode):
for file_count, hsi_composite_file in enumerate(hsi_composite_files):

file_base_name = hsi_composite_file.split('.')[0]



# The match data (hyperspectral)
hsi_composite_path = os.path.join(path_composites_match, hsi_composite_file)
Expand Down
8 changes: 4 additions & 4 deletions gref4hsi/scripts/georeference.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ def main(iniPath, viz = False, use_coreg_param = False):
dataset_name= h5_folder_position_ecef)
try:
# Use the coregistred dataset if it exists
pos_ref_ecef = Hyperspectral.get_dataset(h5_filename=h5_filename,
quat_ref_ecef = Hyperspectral.get_dataset(h5_filename=h5_filename,
dataset_name= h5_folder_quaternion_ecef_coreg)
except:
# If not use the original
Expand All @@ -230,9 +230,9 @@ def main(iniPath, viz = False, use_coreg_param = False):
# Extract the ecef orientations for each frame
quat_ref_ecef = Hyperspectral.get_dataset(h5_filename=h5_filename,
dataset_name=h5_folder_quaternion_ecef)
# Extract the timestamps for each frame
time_pose = Hyperspectral.get_dataset(h5_filename=h5_filename,
dataset_name= h5_folder_time_pose)
# Extract the timestamps for each frame
time_pose = Hyperspectral.get_dataset(h5_filename=h5_filename,
dataset_name= h5_folder_time_pose)


# Using the cal file, we can define lever arm, boresight and camera model geometry (in dictionary)
Expand Down
54 changes: 35 additions & 19 deletions gref4hsi/tests/test_main_specim.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
sys.path.append(module_path)

# Local resources
from gref4hsi.scripts import georeference, orthorectification, coregistration
from gref4hsi.scripts import georeference
from gref4hsi.scripts import orthorectification
from gref4hsi.scripts import coregistration
from gref4hsi.utils import parsing_utils, specim_parsing_utils
from gref4hsi.utils import visualize
from gref4hsi.utils.config_utils import prepend_data_dir_to_relative_paths, customize_config
Expand All @@ -38,7 +40,7 @@



def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, lab_calibration_path):
def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, lab_calibration_path, fast_mode = False):
# Read flight-specific yaml file
with open(config_yaml, 'r') as file:
config_data = yaml.safe_load(file)
Expand Down Expand Up @@ -67,10 +69,11 @@ def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, l
DEM_PATH = os.path.join(dem_fold, files[0])
#print(f"The file '{DEM_PATH}' is used as terrain.")
TERRAIN_TYPE = "dem_file"
print(DEM_PATH)


# Do coregistration if there is an orthomosaic to compare under "orthomosaic"
do_coreg = False
#do_coreg = True
ortho_ref_fold = os.path.join(specim_mission_folder, "orthomosaic")

if not os.path.exists(ortho_ref_fold):
Expand All @@ -82,8 +85,8 @@ def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, l
else:
# If there is a folder and it is not empty
# Find the only file that is there
ortho_ref_file = [f for f in os.listdir(ortho_ref_fold) if f not in ('.', '..')][0]
do_coreg == True
ortho_ref_file = [f for f in os.listdir(ortho_ref_fold) if f.endswith('tif')][0]
do_coreg = True
print(f"The file '{ortho_ref_file}' is used as as reference orthomosaic.")


Expand Down Expand Up @@ -176,6 +179,12 @@ def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, l
# No need to orthorectify the data cube initially when coregistration with RGB composites is done
if do_coreg:
custom_config['Orthorectification']['resample_rgb_only'] = True

#
if fast_mode:
custom_config['Orthorectification']['resample_rgb_only'] = True
custom_config['Orthorectification']['resolutionhyperspectralmosaic'] = 1


# Customizes the config file according to settings
customize_config(config_path=config_file_mission, dict_custom=custom_config)
Expand All @@ -187,51 +196,58 @@ def main(config_yaml, specim_mission_folder, geoid_path, config_template_path, l
# This function parses raw specim data including (spectral, radiometric, geometric) calibrations and nav data
# into an h5 file. The nav data is written to "raw/nav/" subfolders, whereas hyperspectral data and calibration data
# written to "processed/hyperspectral/" and "processed/calibration/" subfolders
model_path = config['Absolute Paths']['model_path']

# If this folder exists, there is no need to repeat this step
"""specim_parsing_utils.main(config=config,
config_specim=config_specim_preprocess)"""
config_specim=config_specim_preprocess)
# Time interpolates and reformats the pose (of the vehicle body) to "processed/nav/" folder.
config = parsing_utils.export_pose(config_file_mission)
parsing_utils.export_pose(config_file_mission)
# Formats model to triangular mesh and an earth centered earth fixed / geocentric coordinate system
parsing_utils.export_model(config_file_mission)
if not os.path.exists(model_path):
parsing_utils.export_model(config_file_mission)
# Commenting out the georeference step is fine if it has been done
## Visualize the data 3D photo model from RGB images and the time-resolved positions/orientations
"""if ENABLE_VISUALIZE:
visualize.show_mesh_camera(config, show_mesh = True, show_pose = True, ref_frame='ENU')"""
# Georeference the line scans of the hyperspectral imager. Utilizes parsed data
# Georeference the line scans of the hyperspectral imager.
georeference.main(config_file_mission)
orthorectification.main(config_file_mission)
orthorectification.main(config_file_mission)"""
# The coregistration compares to the reference and optimizes geometric parameters which are used to re-georeference.
if do_coreg:
# Optional: coregistration
# Match RGB composite to reference, find features and following data, ground control point (gcp) list, for each feature pair:
# reference point 3D (from reference), position/orientation of vehicle (using resampled time) and pixel coordinate (using resampled pixel coordinate)
coregistration.main(config_file_mission, mode='compare')
"""coregistration.main(config_file_mission, mode='compare')
# The gcp list allows reprojecting reference points and evaluate the reprojection error,
# which is used to optimize static geometric parameters (e.g. boresight, camera model...) or dynamic geometric parameters (time-varying nav errors).
# Settings are currently in coregistration script
coregistration.main(config_file_mission, mode='calibrate')
coregistration.main(config_file_mission, mode='calibrate')"""

# The final orthorectification can be conducted on the datacube too
custom_config['Orthorectification']['resample_rgb_only'] = True
custom_config['Orthorectification']['resample_rgb_only'] = False

# No impact unless you rewrite the config
customize_config(config_path=config_file_mission, dict_custom=custom_config)



# Georeference with coregistred parameters
georeference.main(config_file_mission, use_coreg_param=True)


# And orthorectify
orthorectification.main(config_file_mission)



if __name__ == "__main__":
# Select a recording folder on drive
specim_mission_folder = os.path.join(base_fp, r"Specim/Missions/2022-08-31-Remoy/remoy_202208310800_ntnu_hyperspectral_74m")
specim_mission_folder = os.path.join(base_fp, r"Specim/Missions/2022-08-31-Remoy/remoy_202208311040_ntnu_hyperspectral_74m")

# Globally accessible files:
geoid_path = os.path.join(home, "VsCodeProjects/gref4hsi/data/world/geoids/egm08_25.gtx")
Expand Down

0 comments on commit c05ae5e

Please sign in to comment.