Skip to content

Adding a new post-processing step

HistoKit supports adding new post-processing operations through a common PostProcessStep interface.

1. Inherit from the base class

All post-processing steps must inherit from the abstract PostProcessStep class.

The base class defines a common interface for applying post-processing operations and retrieving their configuration.

Method Description
__init__() Initializes step-specific configuration parameters.
__call__(mask) Applies the post-processing operation to a binary mask.
get_config() Returns the serialized configuration of the step.

The __call__() method is abstract and must be implemented by every post-processing step.

2. Implement the post-processing logic

Define a new class with operation-specific parameters and implement the __call__() method.

The following example implements morphological closing using a disk-shaped structuring element:

from typing import Any

import numpy as np
import scipy.ndimage as ndi
from skimage.morphology import disk


class Closing(PostProcessStep):
    def __init__(self, disk_radius: int = 2):
        self.disk_radius = disk_radius

    def __call__(self, mask: np.ndarray) -> np.ndarray:
        se = disk(self.disk_radius)
        return ndi.binary_closing(mask, structure=se)

    def get_config(self) -> dict[str, Any]:
        return {
            "name": "Closing",
            "params": {
                "disk_radius": self.disk_radius,
            },
        }

The __call__() method receives a binary mask as a NumPy array and returns the processed mask.

Define operation-specific parameters in the constructor rather than hardcoding them in the processing logic.

Parameter Default Description
disk_radius 2 Radius of the disk-shaped structuring element.

Note

In most cases post-processing steps should preserve the spatial dimensions of the input mask and return a binary mask compatible with subsequent processing steps.

3. Implement configuration serialization

Override the get_config() method to return a dictionary containing the step name and its configuration parameters.

The configuration must follow the common structure:

{
    "name": "Closing",
    "params": {
        "disk_radius": 2,
    },
}
Field Description
name Identifier of the post-processing step.
params Dictionary containing step-specific configuration parameters.

All configurable constructor parameters should be included in params to ensure that the operation can be reproduced using the serialized configuration.

For steps without configurable parameters, return an empty params dictionary.

4. Use the post-processing step

Instantiate the new post-processing step with the desired parameters and apply it directly to a binary mask:

closing = Closing(disk_radius=3)

processed_mask = closing(mask)

The step configuration can be retrieved using get_config():

config = closing.get_config()

The corresponding configuration represented in YAML is:

name: Closing
params:
  disk_radius: 3

Combining multiple post-processing steps

Post-processing steps share a common interface and can be applied sequentially to a binary mask.

steps = [
    RemoveSmallRegions(thr_area=100),
    Opening(disk_radius=2),
    Closing(disk_radius=3),
    FillHoles(),
]

processed_mask = mask

for step in steps:
    processed_mask = step(processed_mask)

Each step receives the output of the previous operation.

Warning

The order of post-processing steps may affect the final mask. Select the order according to the expected segmentation result.