Skip to main content

imblackhat

imblackhat(img: np.ndarray, ksize: Union[int, Tuple[int, int]] = (3, 3), kstruct: Union[str, int, "MORPH"] = "MORPH.RECT") -> np.ndarray

  • Description: Black-hat operation: the result of closing operation subtracted from the original image. For multi-channel images, each channel is processed independently. This operation is useful for extracting darker areas than the original image, such as dark spots or small structures, while removing or reducing large dark regions.

  • Parameters

    • img (np.ndarray): The input image.
    • ksize (Union[int, Tuple[int, int]]): The size of the structuring element. Default is (3, 3).
    • kstruct (MORPH): The shape of the structuring element, which can be one of "MORPH.CROSS", "MORPH.RECT", or "MORPH.ELLIPSE". Default is "MORPH.RECT".
  • Example

    import numpy as np
    import capybara as cb

    img = np.array([[1, 1, 1, 0, 0],
    [1, 1, 1, 0, 0],
    [1, 1, 1, 0, 0],
    [0, 0, 0, 0, 0], # <- Look at this row
    [0, 0, 0, 1, 1],
    [0, 0, 0, 1, 1]], dtype=np.uint8)

    blackhat_img = cb.imblackhat(img, ksize=3, kstruct='CROSS')

    # Kernel will be like this:
    # >>> np.array([[0, 1, 0],
    # [1, 1, 1],
    # [0, 1, 0]], dtype=np.uint8)

    # After blackhat, the image will be like this:
    # >>> np.array([[0, 0, 0, 0, 0],
    # [0, 0, 0, 0, 0],
    # [0, 0, 0, 0, 0],
    # [0, 0, 1, 1, 0], # <- 1's are extracted
    # [0, 0, 0, 0, 0],
    # [0, 0, 0, 0, 0]], dtype=np.uint8)