From bd82c28fb0fa6e486a519f25c8530409866c38a4 Mon Sep 17 00:00:00 2001 From: "Wieland, Marc" Date: Mon, 25 Nov 2024 13:45:09 +0100 Subject: [PATCH] fixed nodata mask --- ukis_csmask/mask.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/ukis_csmask/mask.py b/ukis_csmask/mask.py index 1ce7665..59699a9 100755 --- a/ukis_csmask/mask.py +++ b/ukis_csmask/mask.py @@ -106,6 +106,12 @@ def __init__( self.batch_size = batch_size self.model_version = model_dict["model"]["version"] + self.nodata_mask = np.ones(shape=(img.shape[0], img.shape[1], 1), dtype=np.uint8) + if self.nodata_value is not None: + # create image nodata mask + # needs to happen before normalization + self.nodata_mask[np.all(img == self.nodata_value, axis=2)] = 0 + # adjust band order and normalize image self.img = self.normalize( img=self.adjust_band_order( @@ -188,13 +194,13 @@ def _valid(self, invalid_buffer): class_dict = {"reclass_value_from": [0, 1, 2], "reclass_value_to": [1, 0, 0]} valid = reclassify(self.csm, class_dict) + if self.nodata_value is not None: + # add image nodata pixels to valid pixel mask + valid[self.nodata_mask == self.nodata_value] = 0 + # dilate the inverse of the binary valid pixel mask (invalid=0) # this effectively buffers the invalid pixels valid_i = ~valid.astype(bool) valid = (~scipy.ndimage.binary_dilation(valid_i, iterations=invalid_buffer).astype(bool)).astype(np.uint8) - if self.nodata_value is not None: - # add image nodata pixels to valid pixel mask - valid[np.all(self.img == self.nodata_value, axis=2)] = 0 - return valid