Calculate statistics over valid pixels#
Calculate the mean red, green and blue values of the sample image without including its missing-data border. Read the file’s validity masks, select pixels that are valid in every band, and compare the means with and without that selection. The arrays and calculations stay on the GPU.
Use the GPU installation from Read a file.
Start a new Python session from the repository root and run the
sample download snippet to define
sample_dir and source_path. Keep the temporary directory alive and the
current CuPy device and stream unchanged throughout this exercise.
Read the pixels and masks#
>>> import cugeo.raster as raster
>>> with raster.open(source_path) as src:
... pixels = src.read()
... masks = src.read_masks()
... print(src.nodata)
...
0.0
>>> masks.shape
(3, 718, 791)
>>> masks.dtype
dtype('uint8')
There is a mask for each band, with the same (bands, rows, columns) axes as
the pixels. This file declares zero as nodata; its masks are derived from
that declaration. Other files can store explicit masks, so use read_masks()
to obtain the file’s validity information instead of assuming that zero is
always invalid.
Inspect the mask values:
>>> import cupy as cp
>>> cp.unique(masks).tolist()
[0, 255]
Zero means invalid, and a nonzero mask value means valid. These are valid-data
masks: True in the Boolean selection below will mean a pixel is included.
Select a common valid footprint#
Use the same set of pixels for all three means. Require a valid value in every RGB band:
>>> valid = cp.all(masks != 0, axis=0)
>>> valid.shape
(718, 791)
>>> int(valid.sum()), valid.size
(382405, 567938)
>>> round(float(valid.mean()) * 100, 2)
67.33
The common footprint contains 382,405 pixels, or 67.33% of the image. The remaining pixels include the black border and locations where at least one band is marked invalid.
White pixels are included in all three means; black pixels are excluded.#
Compare the means#
First average every pixel, then select the common valid footprint before averaging:
>>> unmasked_mean = pixels.mean(axis=(1, 2))
>>> valid_mean = pixels[:, valid].mean(axis=1)
>>> cp.asnumpy(unmasked_mean).round(2).tolist()
[29.95, 44.52, 48.11]
>>> cp.asnumpy(valid_mean).round(2).tolist()
[44.47, 66.1, 71.44]
Including the nodata zeros lowers all three means. Boolean indexing gathers
the selected pixels into a temporary GPU array; the reductions also run on
the GPU. cp.asnumpy() copies just the three resulting means to the host for
display. The values are means of stored RGB samples, not physical reflectance.
This sample has a known limitation: some valid dark values were rounded to zero when the original imagery was reduced to UInt8. The masks express the file’s declared validity; they do not identify clouds, land or water. Rasterio’s nodata-mask example explains this sample’s zero-value ambiguity and combines its band masks. NumPy’s masked-mean example illustrates the same reason for excluding missing values from statistics.
You have computed mean RGB values over one explicitly selected footprint. The mask reference describes mask sources and dataset-wide combination rules for other workflows.