Skip to content

Commit

Permalink
Add more morphological operations to 'utils'
Browse files Browse the repository at this point in the history
  • Loading branch information
Alexey Pechnikov committed Feb 1, 2024
1 parent 554218c commit 35a3217
Showing 1 changed file with 37 additions and 9 deletions.
46 changes: 37 additions & 9 deletions pygmtsar/pygmtsar/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,13 +95,41 @@ def binary_erosion(data, *args, **kwargs):
"""
import xarray as xr
from scipy.ndimage import binary_erosion
# Perform binary erosion on the NumPy array data
array = binary_erosion(data.values, *args, **kwargs)
# Create a new DataArray from the eroded NumPy array
array = xr.DataArray(
array,
coords=data.coords,
dims=data.dims,
attrs=data.attrs,
)
return array
return xr.DataArray(array, coords=data.coords, dims=data.dims, attrs=data.attrs)

@staticmethod
def binary_dilation(data, *args, **kwargs):
"""
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.binary_dilation.html
"""
import xarray as xr
from scipy.ndimage import binary_dilation
array = binary_dilation(data.values, *args, **kwargs)
return xr.DataArray(array, coords=data.coords, dims=data.dims, attrs=data.attrs)

@staticmethod
def binary_opening(data, *args, **kwargs):
"""
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.binary_opening.html
corrmask = utils.binary_closing(corrmask, structure=np.ones((10,10)))
corrmask = utils.binary_opening(corrmask, structure=np.ones((10,10)))
"""
import xarray as xr
from scipy.ndimage import binary_opening
array = binary_opening(data.values, *args, **kwargs)
return xr.DataArray(array, coords=data.coords, dims=data.dims, attrs=data.attrs)

@staticmethod
def binary_closing(data, *args, **kwargs):
"""
https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.binary_opening.html
corrmask = utils.binary_closing(corrmask, structure=np.ones((10,10)))
corrmask = utils.binary_opening(corrmask, structure=np.ones((10,10)))
"""
import xarray as xr
from scipy.ndimage import binary_closing
array = binary_closing(data.values, *args, **kwargs)
return xr.DataArray(array, coords=data.coords, dims=data.dims, attrs=data.attrs)

0 comments on commit 35a3217

Please sign in to comment.