Align two images for comparison#

Place one image on the other’s grid before comparing their pixels. This example uses an independent reference image with known pixels, so you can check cell placement before interpreting the difference count.

Locate corresponding cells#

From the checkout root, load the uint8-nearest-single-band-nodata case from cugeo-raster/tests/data/allocating_reproject.json and its matching .npz payload. The source has shape (1, 3, 4) and rows [0, 2, 3, 4], [5, 0, 7, 8] and [9, 10, 11, 12]. Zero is NoData. The independent target is the reference image.

Both grids use EPSG:32631, but the source grid is skewed and the reference has 7-by-6 map-unit cells. The same row and column index can therefore refer to different ground locations.

import json
from pathlib import Path

import numpy as np
import cupy as cp
from cugeo.raster import Affine
from cugeo.raster.enums import Resampling
from cugeo.raster.warp import reproject

manifest_path = Path("cugeo-raster/tests/data/allocating_reproject.json")
manifest = json.loads(manifest_path.read_text())
case = next(c for c in manifest["cases"]
            if c["name"] == "uint8-nearest-single-band-nodata")
with np.load(manifest_path.with_suffix(".npz"), allow_pickle=False) as payload:
    source_host = payload[case["arrays"]["source"]]
    reference_host = payload[case["arrays"]["gdal_pixels"]]
    validity_host = payload[case["arrays"]["validity"]].astype(bool)

source_transform = Affine.from_gdal(500000, 10, 3, 70, 1, -10)
reference_transform = Affine.from_gdal(500000, 7, 0, 74, 0, -6)

Resample onto the reference grid#

The output shape sets the band count and grid dimensions, and reference_transform sets the cell locations. Nearest sampling lets you compare individual UInt8 samples. Keep the uploads, warp and comparisons on the same selected CUDA stream.

stream = cp.cuda.get_current_stream()
with stream:
    source = cp.asarray(source_host)
    reference = cp.asarray(reference_host)
    reference_validity = cp.asarray(validity_host)
    out = cp.empty((1, 6, 7), dtype=cp.uint8)

    result = reproject(
        source, out,
        src_transform=source_transform, src_crs="EPSG:32631", src_nodata=0,
        dst_transform=reference_transform, dst_crs="EPSG:32631", dst_nodata=0,
        resampling=Resampling.nearest, return_status=True,
    )

    masks_equal = cp.array_equal(result.validity, reference_validity)
    common = result.validity & reference_validity
    common_count = cp.count_nonzero(common)
    unequal_count = cp.count_nonzero(result.data[0][common] != reference[0][common])

stream.synchronize()

Check the grid, then compare the pixels#

print(result.data is out)
print(result.data.shape)
print(result.transform == reference_transform)
print(bool(masks_equal))
print(int(common_count), int(unequal_count))

The grid checks should print True, (1, 6, 7) and True. The leading axis holds the single band. Exact mask equality should also be True. It checks both shape and values, catching a missing valid cell that an intersection alone would hide. The final counts should be 26 0: 26 cells have valid observations in both images, and none of those samples differ. The remaining cells do not provide a pair of valid observations for this comparison.