Skip to main content

imclose

imclose(img: np.ndarray, ksize: int | tuple[int, int] = (3, 3), kstruct: str | int | MORPH = MORPH.RECT) -> np.ndarray

  • Description: Closing operation: a process of dilation followed by erosion, which can be used to fill small holes inside objects, smooth object edges, or connect two objects. For multi-channel images, each channel is processed independently.

  • Parameters

    • img (np.ndarray): The input image.
    • ksize (int | tuple[int, int]): The size of the structuring element. Default is (3, 3).
    • kstruct (str | int | MORPH): Structuring element shape. Accepts MORPH.CROSS/RECT/ELLIPSE, string "CROSS"/"RECT"/"ELLIPSE", or an OpenCV integer. Default is MORPH.RECT.
  • Example

    import numpy as np
    from capybara.vision.morphology import imclose

    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)

    closed_img = imclose(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 closing, the image will be like this:
    # >>> np.array([[1, 1, 1, 0, 0],
    # [1, 1, 1, 0, 0],
    # [1, 1, 1, 0, 0],
    # [0, 0, 1, 1, 0], # <- 1's are connected
    # [0, 0, 0, 1, 1],
    # [0, 0, 0, 1, 1]], dtype=np.uint8)