In a three-dimensional numpy array [row x col x ch], padded with NaNs, e.g.,
>>> a = np.ones((5, 5, 3))
>>> a[0, :, 0] = np.nan
>>> a[:, 0:2, :] = np.nan
>>> a[4, 4, 0] = np.nan
>>> a[:, :, 0]
array([[nan, nan, nan, nan, nan],
[nan, nan, 1., 1., 1.],
[nan, nan, 1., 1., 1.],
[nan, nan, 1., 1., 1.],
[nan, nan, 1., 1., nan]])
>>> a.shape
(5, 5, 3)
how do I crop all rows and columns containing NaNs in any channel from the border?
In my example, the result I want to have is an array
>>> a_cropped[:, :, 0]
array([[1., 1.],
[1., 1.],
[1., 1.]])
>>> a_cropped.shape
(3, 2, 3)
This is related to this question. However, I am perfectly fine with having only the rectangular area in the center that can unambiguously been extracted.
I assume I first have to strip bordering rows and columns one after another in order not to remove "valid" areas.
Context: Rotating an image and removing NaN-padded areas outside of the valid image area, illustrated in the following figure as black pixels.