Skip to content

Commit

Permalink
fixed nodata mask
Browse files Browse the repository at this point in the history
  • Loading branch information
MWieland committed Nov 25, 2024
1 parent 708a362 commit bd82c28
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions ukis_csmask/mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

0 comments on commit bd82c28

Please sign in to comment.