Skip to content

instancecontour

instancecontour

Module for data classes representing the found error instances in a predicted image

Classes

InstanceContour dataclass

Dataclass that represents a instance contour.

Functions
get_contour_mask
get_contour_mask(target_shape)

Creates a Mask from self.contour withtarget_shape` Args: target_shape: Shape of the mask

Returns:

  • ndarray

    Mask with self.contour on it and the form target_shape.

Source code in niceml/mlcomponents/resultanalyzers/instancefinders/instancecontour.py
def get_contour_mask(
    self, target_shape: Union[ImageSize, Tuple[int, int]]
) -> np.ndarray:
    """
    Creates a Mask from `self.contour with `target_shape`
    Args:
        target_shape: Shape of the mask

    Returns:
        Mask with `self.contour` on it and the form `target_shape`.
    """

    target_shape = (
        target_shape.to_numpy_shape()
        if isinstance(target_shape, ImageSize)
        else target_shape
    )
    mask = np.zeros(shape=target_shape)
    mask = cv2.drawContours(
        mask,
        [self.contour],
        -1,
        color=(255,),
        thickness=cv2.FILLED,
    )
    return mask
intersects_mask
intersects_mask(mask)

Check if self.contour intersect with mask.

Parameters:

  • mask (ndarray) –

    mask to check for intersection

Returns:

  • bool

    True if there is an intersection

Source code in niceml/mlcomponents/resultanalyzers/instancefinders/instancecontour.py
def intersects_mask(self, mask: np.ndarray) -> bool:
    """
    Check if `self.contour` intersect with `mask`.

    Args:
        mask: mask to check for intersection

    Returns:
        True if there is an intersection

    """

    defect_mask = np.zeros_like(mask)
    cv2.drawContours(
        defect_mask, [self.contour], -1, color=(1,), thickness=cv2.FILLED
    )
    intersect = np.logical_and(mask, defect_mask)
    return np.any(intersect)