def compute_mask(anomaly_map: np.ndarray, threshold: float, kernel_size: int = 4) -> np.ndarray:
    """Compute binary anomaly mask by thresholding and post-processing anomaly map.

    This function converts a continuous-valued anomaly map into a binary mask by:
        - Thresholding the anomaly scores
        - Applying morphological operations to reduce noise
        - Scaling to 8-bit range [0, 255]

    Args:
        anomaly_map (np.ndarray): Anomaly map containing predicted anomaly scores.
            Should be a 2D array of float values.
        threshold (float): Threshold value to binarize anomaly scores. Values above
            this threshold are considered anomalous (1) and below as normal (0).
        kernel_size (int, optional): Size of the morphological structuring element
            used for noise removal. Higher values result in smoother masks.
            Defaults to ``4``.

    Returns:
        np.ndarray: Binary anomaly mask where anomalous regions are marked with
            255 and normal regions with 0. Output is uint8 type.

    Examples:
        Basic thresholding with default kernel size:

        >>> anomaly_scores = np.random.rand(100, 100)
        >>> mask = compute_mask(anomaly_scores, threshold=0.5)
        >>> mask.shape
        (100, 100)
        >>> mask.dtype
        dtype('uint8')
        >>> np.unique(mask)
        array([  0, 255], dtype=uint8)

        Custom kernel size for stronger smoothing:

        >>> mask = compute_mask(anomaly_scores, threshold=0.5, kernel_size=8)

    Note:
        - Input anomaly map is squeezed to remove singleton dimensions
        - Morphological opening is used to remove small noise artifacts
        - Output is scaled to [0, 255] range for visualization
    """
    anomaly_map = anomaly_map.squeeze()
    mask: np.ndarray = np.zeros_like(anomaly_map).astype(np.uint8)
    mask[anomaly_map > threshold] = 1

    kernel = morphology.disk(kernel_size)
    mask = morphology.opening(mask, kernel)

    mask *= 255

    return mask
Compute binary anomaly mask by thresholding and post-processing anomaly map.

    This function converts a continuous-valued anomaly map into a binary mask by:
        - Thresholding the anomaly scores
        - Applying morphological operations to reduce noise
        - Scaling to 8-bit range [0, 255]

    Args:
        anomaly_map (np.ndarray): Anomaly map containing predicted anomaly scores.
            Should be a 2D array of float values.
        threshold (float): Threshold value to binarize anomaly scores. Values above
            this threshold are considered anomalous (1) and below as normal (0).
        kernel_size (int, optional): Size of the morphological structuring element
            used for noise removal. Higher values result in smoother masks.
            Defaults to ``4``.

    Returns:
        np.ndarray: Binary anomaly mask where anomalous regions are marked with
            255 and normal regions with 0. Output is uint8 type.

    Examples:
        Basic thresholding with default kernel size:

        >>> anomaly_scores = np.random.rand(100, 100)
        >>> mask = compute_mask(anomaly_scores, threshold=0.5)
        >>> mask.shape
        (100, 100)
        >>> mask.dtype
        dtype('uint8')
        >>> np.unique(mask)
        array([  0, 255], dtype=uint8)

        Custom kernel size for stronger smoothing:

        >>> mask = compute_mask(anomaly_scores, threshold=0.5, kernel_size=8)

    Note:
        - Input anomaly map is squeezed to remove singleton dimensions
        - Morphological opening is used to remove small noise artifacts
        - Output is scaled to [0, 255] range for visualization
    
src/anomalib/utils/post_processing.py