# Phase 1: Image Processing and Tiling Tools for converting UAS orthomosaics into spatially aligned, fixed-size predictor tiles across RGB, multispectral, and hyperspectral sensors. ## Method Orthomosaic imagery from UAV acquisitions was processed into gridded tiles suitable for annotation and model training. The workflow involves: 1. Harmonising coordinate reference systems and converting float rasters to scaled `int16` 2. Building a row-major annotation grid aligned to the raster origin 3. Clipping orthomosaics by that grid into fixed-size tiles (GeoTIFF or ENVI) 4. Randomly sampling paired tiles across modalities for annotation 5. Computing global statistics and percentile stretches for visual composites 6. Stacking band subsets into reduced predictor cubes Because every sensor is clipped against a grid derived from the same ground extent, tile IDs correspond to the same location across all modalities and resolutions. ## Grid Alignment Grid alignment is the foundation of every later phase — if tiles are not registered, labels and predictors drift apart and the models train on mismatched pairs. All grids resolve to the same ~30.7 m ground extent: | Grid | Tile size | Resolution | Ground extent | | --- | --- | --- | --- | | `tiles_3072` | 3072 px | 1 cm | 30.72 m | | `tiles_3070` | 3070 px | 1 cm | 30.70 m | | `grid_annotation_614` | 614 px | 5 cm | 30.70 m | | `grid_annotation_hyspecFX10_307` | 307 px | 10 cm | 30.70 m | `tiles_3070` exists so that 1 cm labels divide evenly into 5 cm and 10 cm products without a remainder, keeping downsampled labels registered to the predictor tiles. Tile IDs follow **row-major spatial order** (top-left → right, then next row) so filenames increase left-to-right across the map. Reindex an existing grid with `utils/reindex_grid_rowmajor.py`. ## Contents ``` phase_1_image_processing/ ├── step_1_rasterReprojection/ │ ├── 1_1_imageProcessing_rasterReprojection.py # CRS harmonisation │ ├── 1_2_imageProcessing_tiffCog.py # Cloud-optimised GeoTIFF conversion │ └── multispecFloat_int16.py # Float → scaled int16 ├── step_2_orthomosaic_to_tiling/ │ ├── a_qgis/ # Run from the QGIS Python console │ │ ├── 1_createGrid_referenceData.py # Create the annotation grid │ │ └── 2_clip_shp_gridAnnotation.py # Clip by grid │ ├── b_tiling_3072_rgb/ # RGB tiling + grid clipping │ ├── c_tiling_3072_multispec/ # Multispectral tiling (GeoTIFF + ENVI) │ └── d_tiling_3072_hyspec/ # Hyperspectral clipping (FX10/FX17) ├── step_3_randomSampling/ │ └── 3_1_random_sampling.py # Paired tile sampling, rejects saturated RGB ├── step_4_globalStats_percentileStretch/ │ ├── a_rgb/ # Global stats + stretch (RGB) │ └── b_multispec/ # Global stats + stretch (multispectral) ├── step_5_stackBands_predictors/ │ ├── 5_1_create_compositeColour.py # Visual composites │ ├── 5_2_imageProcessing_stackedBandsRaster.py # Band subset stacking │ ├── 5_4_stack_hyspec_by_wavelength_ranges_envi.py # Subset by wavelength range │ ├── 5_5_stack_fx10_fx17_orthomosaic_envi.py # Stack FX10 + FX17 mosaics │ └── 5_6_stack_fx10_fx17_subset_orthomosaic_envi.py └── utils/ ├── build_aligned_grid_from_raster.py # Row-major grid from raster origin ├── reindex_grid_rowmajor.py # Reindex grid IDs in spatial order ├── convert_float_ortho_to_int16_envi.py # Streaming float → int16 ENVI ├── audit_micasense_reflectance_scale.py # Verify reflectance scaling ├── run_hyspec_mosaic_to_256_then_subset.py # Mosaic → tiles → band subset ├── run_fx10_mosaic_to_256_then_subset.py └── clip_mica10_to_reference_patch_grid.py # MICA 10 cm ortho → existing patch grid ``` ## Usage All paths resolve through [configs/paths.yaml](../configs/paths.yaml) — see [docs/REPRODUCTION.md](../docs/REPRODUCTION.md). ```bash python utils/fvc_paths.py --check # confirm the data roots resolve first ``` ### Step 1: Build alignment grids ```bash bash scripts/build_site1_alignment_grids.sh ``` Produces `grid_annotation_614.shp`, `grid_annotation_hyspecFX10_307.shp`, and `grid_annotation_hyspecFX17_307.shp`. Run this before anything else — later steps inherit tile IDs and extents from it. ### Step 2: Clip orthomosaics to tiles ```bash bash scripts/regenerate_site1_multispec_5cm_614.sh # MicaSense 5 cm → 614 grid bash scripts/refresh_site1_alignment_and_labels.sh # FX10 + FX17 → 307 px stack ``` ### Step 3: Site 2 hyperspectral (run in order) ```bash bash scripts/repair_site2_fx10_fx17_215_trim_scanline_gaps.sh # repair scanline gaps bash scripts/chunk_site2_fx10_fx17_215_trim_304.sh # → 304 px tiles bash scripts/prepare_site2_s224_hyspec_patches.sh # → 224 px patches ``` ### Direct script use ```bash python phase_1_image_processing/utils/reindex_grid_rowmajor.py \ --in-shp /path/to/grid_annotation_extraLabels_256.shp \ --out-shp /path/to/grid_annotation_extraLabels_256_rowmajor.shp \ --id-field id --start 1 ``` ### Crop the 10 cm MICA ortho onto an existing patch grid `utils/clip_mica10_to_reference_patch_grid.py` clips a 10 cm MicaSense ortho onto the exact grid (bounds, size, pixel size) of existing reference patches — for example the FX10_FX17 `152_crop304_ov76` mask or predictor tiles, which share the same metre footprint as the 5 cm MICA `304_crop608_ov152` patches (30.4 m crops, 7.6 m stride). Outputs are ENVI int16 `tiles_multispectral_.dat` tiles in the ortho's CRS, pixel-aligned with the 10 cm masks and named by the reference tile suffix (e.g. `109_r00_c01`). ```bash INPUT_ORTHO=/home/laura/Documents/data_model/imagery/mica/20220519_SASMDD0001_dual_ortho_10_int16.dat \ REFERENCE_DIR=/home/laura/Documents/data_model/masks/tiles_152_crop304_ov76/raw/fvc_class/downsample_10cm_mode_fx10_fx17_215_trim_from_5cm_152_crop304_ov76 \ OUTPUT_DIR=/home/laura/Documents/data_model/predictors/mica_10/tiles_multispectral_int16_envi_152_crop304_ov76 \ python phase_1_image_processing/utils/clip_mica10_to_reference_patch_grid.py ``` Optional: `OUTPUT_PREFIX` (default `tiles_multispectral`) and `RESAMPLING` (default `near`, which preserves int16 reflectance values exactly). Use `OUTPUT_TYPE=Float32 NODATA=0` for non-reflectance inputs such as PCA-score orthos, and `OUTPUT_SCALE=2` to write finer tiles on the same bounds (e.g. 608 px 5 cm tiles from a 304 px 10 cm reference grid). ### Tile an ortho onto a new regular grid (external-validation sites) For a site with no existing grid (e.g. the SASMDD0013 cross-site inference area), `utils/tile_ortho_regular_grid.py` tiles an ortho into fixed-size, non-overlapping ENVI tiles anchored at the raster origin, with row-major IDs (edge remainders are dropped): ```bash # 1. Define footprints: 304 px (30.4 m) tiles from the 10 cm ortho INPUT_ORTHO=.../20240310_SASMDD0013_multispec_ortho_10_subset_int16.dat \ OUTPUT_DIR=.../site2_SASMDD0013/mica_10/tiles_multispectral_int16_envi_304px \ TILE_SIZE=304 python phase_1_image_processing/utils/tile_ortho_regular_grid.py # 2. Clip the 5 cm ortho onto the SAME footprints at 608 px INPUT_ORTHO=.../20240310_SASMDD0013_multispec_ortho_05_subset_int16.dat \ REFERENCE_DIR=.../mica_10/tiles_multispectral_int16_envi_304px \ OUTPUT_DIR=.../site2_SASMDD0013/mica_05/tiles_multispectral_int16_envi_608px \ OUTPUT_SCALE=2 python phase_1_image_processing/utils/clip_mica10_to_reference_patch_grid.py ``` The paired 5 cm / 10 cm tile sets share identical bounds and filenames, so one annotation per footprint serves both resolutions. Both tiling utilities propagate ENVI header metadata (`band names`, `reflectance scale factor`, `data ignore value`, `wavelength`) from the source header via `utils/envi_hdr_propagation.py` — GDAL alone drops these dataset-level lines when writing new ENVI files. ## Sensor Profiles - **RGB**: 3-band orthomosaics, used for visual annotation reference - **Multispectral**: MicaSense 10-band stacks at 1 cm and 5 cm; 5-band subsets for reduced predictors - **Hyperspectral**: Specim FX10 (~400–1000 nm) and FX17 (~900–1700 nm), band wavelengths read from ENVI headers, resampled onto the FX10 grid and stacked into a single cube **Storage:** hyperspectral products are written as ENVI (`.dat` + `.hdr`) to preserve per-band wavelength metadata; multispectral and RGB are GeoTIFF or ENVI depending on the step. ## Dependencies ```bash mamba env create -f environment.yml && mamba activate fvc_function ``` Core requirements: `gdal` (`osgeo`), `rasterio`, `geopandas`, `shapely`, `numpy`. The scripts in `step_2_orthomosaic_to_tiling/a_qgis/` run inside the QGIS Python console and are **not** covered by `requirements.txt`. ## Outputs - **Grid shapefiles**: row-major annotation grids per sensor and resolution - **Tiled rasters**: fixed-size predictor tiles (`tiles_multispectral_.tif`, `.dat`) preserving CRS and transform - **Global statistics**: per-band min/max/percentile tables for stretching - **Predictor cubes**: stacked band subsets and FX10+FX17 composites - **Visual composites**: percentile-stretched RGB renders for manual annotation ## Notes Row-major tile IDs matter when imagery is first tiled into 3072×3072 blocks and later subdivided — without reindexing, sub-tile IDs follow block order rather than map order, and filenames stop matching spatial position. Related documentation: - [../docs/METHODS.md](../docs/METHODS.md) — paper-ready method description for this phase - [step_2_orthomosaic_to_tiling/c_tiling_3072_multispec/README.md](step_2_orthomosaic_to_tiling/c_tiling_3072_multispec/README.md) — multispectral tiling detail - [../docs/REPRODUCTION.md](../docs/REPRODUCTION.md) — full driver-script map by phase - [../phase_2_reference_data/README_phase2.md](../phase_2_reference_data/README_phase2.md) — the labels these tiles are paired with