Cropping binary images based on pixel counts in python I have a numpy binary masked image that looks like the following:

I want to automatically crop this image like the following:

Can I achieve this using some sort of image processing rather than building a bounding box regressor?
The method should also work if the image is horizontally flipped.
I am currently using th below crop function to crop the image if any row or column have all 0 pixels:
def crop_image(img,tol=0):
    # img is 2D image data
    # tol  is tolerance
    mask = img&gt;tol
    return img[np.ix_(mask.any(1),mask.any(0))]

This crops out the upper black space from the image but does not work for the lower part for obvious reasons.
My attempt was to try to threshold the image based on pixel counts. Below is the code:
counts = np.sum(image==1,axis=0)
plt.plot(counts)


If we can threshold the image as below we might also consider the task partially successful as we would get something like the following:


Thanks alot. Any help will be highly appreciated.
One approach that is always available for binary images are morphological operations.

The critical piece of code is
# from skimage import morphology
# import numpy as np

# filter elements smaller than 1/6-th of the total width
selem = morphology.rectangle(1, img_binary.shape[1] // 6)
img_opened = morphology.opening(img_binary, selem=selem)

hist = np.sum(img_opened, axis=1)
non_zero = np.argwhere(hist &gt; 0)
top_bound = int(np.round(np.min(non_zero) * (1 - 0.05))) # 5% tolerance
bottom_bound = int(np.round(np.max(non_zero) * (1 + 0.05))) # 5% tolerance

Here is the notebook I used to produce the image above:
https://gist.github.com/FirefoxMetzger/5d0d5e207bf8182f61374ac9ad80f10f