Skip to content

Commit

Permalink
Use cv2 in eolearn.mask (#734)
Browse files Browse the repository at this point in the history
* use cv2_disk from s2cloudless, remove unnecessary code

* use cv2_disk from cv2 for snow masking

* fix types after change

* revert legacy task
  • Loading branch information
Matic Lubej authored Aug 31, 2023
1 parent 6037f4d commit 608e966
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
8 changes: 0 additions & 8 deletions eolearn/mask/cloud_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ def __init__(

self.threshold = threshold

self.avg_kernel = None
if average_over is not None and average_over > 0:
self.avg_kernel = disk(average_over) / np.sum(disk(average_over))

self.dil_kernel = None
if dilation_size is not None and dilation_size > 0:
self.dil_kernel = disk(dilation_size).astype(np.uint8)

self.classifier = S2PixelCloudDetector(
threshold=threshold, average_over=average_over, dilation_size=dilation_size, all_bands=all_bands
)
Expand Down
11 changes: 6 additions & 5 deletions eolearn/mask/snow_mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
from abc import ABCMeta
from typing import Any

import cv2
import numpy as np
from skimage.morphology import binary_dilation, disk

from eolearn.core import EOPatch, EOTask, FeatureType
from eolearn.core.types import Feature
Expand Down Expand Up @@ -43,15 +43,16 @@ def __init__(
"""
self.bands_feature = self.parse_feature(data_feature, allowed_feature_types={FeatureType.DATA})
self.band_indices = band_indices
self.dilation_size = dilation_size
self.disk_size = 2 * dilation_size + 1
self.undefined_value = undefined_value
self.mask_feature = (FeatureType.MASK, mask_name)

def _apply_dilation(self, snow_masks: np.ndarray) -> np.ndarray:
"""Apply binary dilation for each mask in the series"""
if self.dilation_size:
snow_masks = np.array([binary_dilation(mask, disk(self.dilation_size)) for mask in snow_masks])
return snow_masks
if self.disk_size > 0:
disk = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (self.disk_size, self.disk_size))
snow_masks = np.array([cv2.dilate(mask.astype(np.uint8), disk) for mask in snow_masks])
return snow_masks.astype(bool)


class SnowMaskTask(BaseSnowMaskTask):
Expand Down

0 comments on commit 608e966

Please sign in to comment.