Source code for ddd_pylib.image_manip._invert_operator
from ddd_pylib._base import ImageOperator
import xarray as xr
import numpy as np
[docs]
class InvertOperator(ImageOperator):
"""
Inverts the intensity values of the input image.
If the values use an integer type, the inversion is done based on the maximum and minimum values of that type.
Otherwise, the inversion is done based on the maximum and minimum values present in the image.
"""
def __init__(self):
super().__init__()
[docs]
def getPrefix(self) -> str:
return "Inverted-"
def _invert(self, img: xr.DataArray):
canvas = img.astype(np.float32)
maxVal = np.float32(
np.iinfo(img.dtype).max
if np.issubdtype(img.dtype, np.integer)
else np.max(img)
)
minVal = np.float32(
np.iinfo(img.dtype).min
if np.issubdtype(img.dtype, np.integer)
else np.min(img)
)
return (maxVal - canvas + minVal).astype(img.dtype)
def _applyToFrame(self, frameData, t, nT):
imA = frameData[0]
return self._invert(imA)