Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions masked_tiles_nodata.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "4b4eced1",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000006_c000024_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000006_c000025_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000006_c000028_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000008_c000015_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000008_c000026_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000008_c000031_masked.tif\n",
"OK: C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles_masked\\tile_r000008_c000032_masked.tif\n"
]
}
],
"source": [
"import numpy as np\n",
"import rasterio\n",
"from pathlib import Path\n",
"from rasterio.windows import from_bounds\n",
"from rasterio.warp import reproject, Resampling\n",
"\n",
"tiles_dir = Path(r\"C:\\Users\\leoni\\kirgis_repo\\segmenteverygrain\\leonieexamples\\2509_with_vegetation_mask_nodataset\\inputtiles\")\n",
"\n",
"veg_mask_path = r\"C:\\Users\\leoni\\kirgis_repo\\veg-shad-wat-filtering\\1_uav-filters\\vegetation\\veg_masks_rgbvi_fallback\\25092025_Aktal_gravel_2_10m_OM_RGB_utm_coregistered_ws128_clipwater_vegmask_rgbvi.tif\"\n",
"shadow_mask_path = r\"C:\\Users\\leoni\\kirgis_repo\\veg-shad-wat-filtering\\1_uav-filters\\shadow\\0-02\\25092025_Aktal_gravel_2_10m_OM_RGB_utm_coregistered_ws128_clipwater_shadowmask.tif\"\n",
"\n",
"out_dir = tiles_dir.parent / (tiles_dir.name + \"_masked\")\n",
"out_dir.mkdir(parents=True, exist_ok=True)\n",
"\n",
"\n",
"def read_mask_aligned(mask_src, dst_src):\n",
" \"\"\"\n",
" Liest den Masken-Ausschnitt passend zum Tile (dst_src) und bringt ihn\n",
" per nearest-neighbor auf exakt das Tile-Grid (falls nötig).\n",
" \"\"\"\n",
" # Window im Maskenraster, basierend auf den Bounds des Tiles\n",
" b = dst_src.bounds\n",
" win = from_bounds(b.left, b.bottom, b.right, b.top, transform=mask_src.transform)\n",
" win = win.round_offsets().round_lengths()\n",
"\n",
" mask_crop = mask_src.read(1, window=win, boundless=True, fill_value=0)\n",
"\n",
" aligned = np.zeros((dst_src.height, dst_src.width), dtype=np.uint8)\n",
"\n",
" reproject(\n",
" source=mask_crop,\n",
" destination=aligned,\n",
" src_transform=mask_src.window_transform(win),\n",
" src_crs=mask_src.crs,\n",
" dst_transform=dst_src.transform,\n",
" dst_crs=dst_src.crs,\n",
" resampling=Resampling.nearest,\n",
" )\n",
" return aligned\n",
"\n",
"\n",
"with rasterio.open(veg_mask_path) as veg_src, rasterio.open(shadow_mask_path) as shad_src:\n",
" for tile_path in sorted(tiles_dir.glob(\"*.tif\")):\n",
" with rasterio.open(tile_path) as src_rgb:\n",
" rgb = src_rgb.read() # (bands, h, w)\n",
" profile = src_rgb.profile.copy()\n",
"\n",
" veg_aligned = read_mask_aligned(veg_src, src_rgb)\n",
" shad_aligned = read_mask_aligned(shad_src, src_rgb)\n",
"\n",
" # robust: alles >0 gilt als \"maskiert\" (egal ob 1 oder 255)\n",
" invalid = (veg_aligned > 0) | (shad_aligned > 0)\n",
"\n",
" # NoData Wert: bei uint8 typischerweise 0\n",
" nodata_value = 0\n",
" profile.update(\n",
" nodata=nodata_value,\n",
" compress=\"deflate\",\n",
" tiled=True,\n",
" driver=\"GTiff\",\n",
" )\n",
"\n",
" # Pixel überschreiben (für Tools, die keine Masken beachten)\n",
" rgb[:, invalid] = nodata_value\n",
"\n",
" out_path = out_dir / tile_path.name.replace(\".tif\", \"_masked.tif\")\n",
" with rasterio.open(out_path, \"w\", **profile) as dst:\n",
" dst.write(rgb)\n",
" # Zusätzlich eine echte GDAL-Maske schreiben (QGIS liebt das)\n",
" dst.write_mask(np.where(invalid, 0, 255).astype(\"uint8\"))\n",
"\n",
" print(f\"OK: {out_path}\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "seg_2",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.19"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Loading