Spaces:
Running
Running
File size: 10,393 Bytes
d1ffd83 | 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 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | # 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_<suffix>.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_<id>.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
|