Is there a way to detect and remove zero padding within an image array? In a way my question is very similar to this except the image has already been rotated and I do not know the angle.
I am basically cropping a box out of a larger image which may have zero padding around the edges (due to translations or rotations). Now it's possible that the crop may contain some of this padding. However, in such cases, I want to clip the box where the padding edge starts. The images are in a CHW (can be easily changed to HWC).
The paddings in this case will be 0s in all channels. However, due to rotations, it's possible that sometimes, the 0s might not always be in completely horizontal or vertical strips in the array. Is there a way to detect if there are 0s going all the way to the edge in the array and at what location the edges start?
Example 1 where arr
is an image with 3 channels and width and height of 4 (3, 4, 4) and the crop contains vertical padding on the rightmost edge:
array([[[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.]],
[[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.]],
[[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 0.]]])
In this example, I would slice the array as such to get rid of the zero padding: arr[:, :, :-1]
Example 2 where we have some padding on the top right corner:
array([[[1., 1., 0., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 0., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]],
[[1., 1., 0., 0.],
[1., 1., 1., 0.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]])
In this example, I would clip the image to remove any padding by returning arr2[:, 1:, :-1]
.
I want to do this in Tensorflow so tensor operations would be great but I am trying to figure out any algorithm, for example using numpy, that can achieve this result.