Orthorectify an image onto its terrain grid#
Orthorectification uses camera geometry and terrain heights to place image samples on a map grid. cuGeo Raster runs the geometry and sampling work on an NVIDIA GPU. In this tutorial, you’ll correct a small image and check where each of its eight samples lands.
Predict the two output rows#
The image’s rational polynomial coefficients (RPCs) describe its camera model. The tutorial fixtures are two uncompressed, single-band GeoTIFFs in the cugeo-raster checkout.
docs/guide/tutorial/assets/rpc-image.tif: a UInt16 image with an identity camera.docs/guide/tutorial/assets/terrain.tif: a Float32 terrain grid whose heights are all zero relative to the WGS84 ellipsoid.
Both use a 4-column by 2-row EPSG:4326 grid with an identity affine transform. The image rows are shown below.
10 20 30 40
50 60 70 80
With this camera, matching destination grid and nearest-neighbor sampling, each destination cell should contain the corresponding image value.
Place and check the samples#
Start in the cugeo-raster checkout with CuPy and Raster and Proj installations that support CUDA orthorectification. Check that cr.diagnostics.capabilities()["orthorect_cuda"] is true.
The following call writes into a caller-owned CuPy array. The native TIFF reader decodes bounded windows on the host and stages them to the GPU. CUDA performs the geometry and sampling. The call completes synchronously. The explicit stream synchronization below also makes the stream boundary visible before the pixels are copied to the host for inspection.
from pathlib import Path
import cupy as cp
import numpy as np
import cugeo.raster as cr
from cugeo.raster.enums import Resampling
assets = Path("docs/guide/tutorial/assets")
image = assets / "rpc-image.tif"
terrain = assets / "terrain.tif"
expected = np.array([[10, 20, 30, 40], [50, 60, 70, 80]], dtype=np.uint16)
assert cr.diagnostics.capabilities()["orthorect_cuda"]
stream = cp.cuda.Stream(non_blocking=True)
with stream:
out = cp.empty((2, 4), dtype=cp.uint16)
result = cr.orthorectify(
image, terrain,
out=out,
destination_transform=cr.Affine(1, 0, 0, 0, 1, 0),
destination_crs=4326,
terrain_vertical_reference="wgs84_ellipsoid",
resampling=Resampling.nearest,
terrain_resampling=Resampling.nearest,
preferred_shape=(2, 2),
minimum_shape=(1, 1),
memory_budget_bytes=1 << 20,
stream=stream,
return_status=True,
return_receipt=True,
)
stream.synchronize()
pixels = cp.asnumpy(result.data)
np.testing.assert_array_equal(pixels, expected)
assert result.data is out
assert type(result.validity) is np.ndarray and result.validity.all()
assert type(result.status) is np.ndarray and (result.status == 0).all()
assert result.receipt.route == "native-tiff-host-staged-cuda-orthorectify"
print(pixels.tolist())
print(result.data is out, bool(result.validity.all()), bool((result.status == 0).all()))
print("receipt_route", result.receipt.route)
The output is shown below.
[[10, 20, 30, 40], [50, 60, 70, 80]]
True True True
receipt_route native-tiff-host-staged-cuda-orthorectify
All eight placements match the prediction. The first True confirms that the corrected image is in out. Validity and status are already NumPy arrays on the host. This verifies the pipeline for the identity camera. The accuracy of a real sensor model requires separate verification.
To choose a scene, DEM and terrain height reference for your own data, see Orthorectify your own RPC scene.