I have dataset of images which are all like this one.
The task is to crop the white space surrounding the image as much as possible and return the image that contains less white surrounding image:
def crop_object(img):
lst = []
# hold min and max of height
for i in range(img.shape[0]):
r = img[:,i,0]
g = img[:,i,1]
b = img[:,i,2]
if (np.min(r) != 255) or (np.min(g) != 255) or (np.min(b) != 255):
lst.append(i)
a1 = min(lst)
a2 = max(lst)
for i in range(img.shape[1]):
r = img[i,:,0]
g = img[i,:,1]
b = img[i,:,2]
if (np.min(r) != 255) or (np.min(g) != 255) or (np.min(b) != 255):
lst.append(i)
a3 = min(lst)
a4 = max(lst)
return img [a3:a4, a1:a2, :]
I want a more pythonic way to handle this. Something like less code and faster run.
Can you help me guys?
img [a3:a4+1, a1:a2+1, :]
instead to use full extent.