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>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.