Source code for ddd_pylib.morphology._base_morphological_operator

from ddd_pylib._base import ImageOperator
from ddd_pylib.morphology import Kernel
from ddd_pylib import ImageUtils
import numpy as np


[docs] class BaseMorphologicalOperator(ImageOperator): """ Provides more precise functions for morphology operation, mainly for kernel, with a kernel_radius attribute and a shape attribute for the kernel shape, Circle, Square or Diamond for a 2D kernel, and Sphere, Cube or Diamond for a 3D one. Args: kernel_radius: The radius of the wanted kernel shape: The shape of the kernel """ def __init__(self): super().__init__() self._kernelRadius = self.defaultKernelRadius() self._kernelShape = self.defaultKernelShape() self._kernel = None
[docs] @staticmethod def defaultKernelRadius() -> int: return 1
[docs] @staticmethod def defaultKernelShape() -> str: return "Square"
[docs] def setKernelRadius(self, radius: int): """ Set the kernel radius Args: radius: the radius of the kernel that will be created """ if radius <= 0: raise ValueError("Kernel radius can't be 0 or under") self._kernelRadius = radius
[docs] def setKernelShape(self, shape: str): """ Set the shape of the kernel, depending of the number of dimensions Args: shape: The shape of the kernel, picked in shape_2d or shape_3d depending of the need """ if shape not in Kernel.validShapes: raise ValueError("Shape unknown or not supported") self._kernelShape = shape
[docs] def getKernel(self) -> np.ndarray: if self._kernel is None: im = self.getMainImage() self._kernel = Kernel.makeKernel( self._kernelShape, self._kernelRadius, im.dims, ImageUtils.scaleToTuple(im) ) return self._kernel