Click4Ai

320.

Medium

Opening and Closing

====================

Implement opening and closing operations on a binary image.

Example:

Suppose we have a binary image represented as a 2D numpy array.

import numpy as np

# Binary image

image = np.array([

[0, 0, 1, 1, 0],

[0, 0, 1, 1, 0],

[0, 0, 0, 0, 0],

[1, 1, 1, 1, 1],

[0, 0, 0, 0, 0]

])

We want to perform opening and closing operations on this image using a 3x3 structuring element.

Constraints:

  • The input image is a 2D numpy array of binary values (0 or 1).
  • The structuring element is a 3x3 numpy array of binary values (0 or 1).
  • Your function should return the resulting images after the opening and closing operations.

    Test Cases

    Test Case 1
    Input: [[0, 0, 1, 1, 0],[0, 0, 1, 1, 0],[0, 0, 0, 0, 0],[1, 1, 1, 1, 1],[0, 0, 0, 0, 0]]
    Expected: [[False, False, False, False, False],[False, False, False, False, False],[False, False, False, False, False],[False, False, False, False, False],[False, False, False, False, False]]
    Test Case 2
    Input: [[1, 1, 1, 1, 1],[1, 1, 1, 1, 1],[1, 1, 1, 1, 1],[1, 1, 1, 1, 1],[1, 1, 1, 1, 1]]
    Expected: [[True, True, True, True, True],[True, True, True, True, True],[True, True, True, True, True],[True, True, True, True, True],[True, True, True, True, True]]
    + 3 hidden test cases