title: Detect and remove zero padding within an image array?
body: <p>Is there a way to detect and remove zero padding within an image array?
  In a way my question is very similar to <a href="https://stackoverflow.com/questions/16702966/rotate-image-and-crop-out-black-borders/16778797">this</a> except the image has already been rotated and I do not know the angle.</p>
  <p>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).</p>
  <p>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?</p>
  <p>Example 1 where <code>arr</code> 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:</p>
  <pre><code>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.]]])
  </code></pre>
  <p>In this example, I would slice the array as such to get rid of the zero padding: <code>arr[:, :, :-1]</code></p>
  <p>Example 2 where we have some padding on the top right corner:</p>
  <pre><code>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.]]])
  </code></pre>
  <p>In this example, I would clip the image to remove any padding by returning <code>arr2[:, 1:, :-1]</code>.</p>
  <p>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.</p>