# -*- coding: UTF-8 -*- import numpy as np def find_minx(mat,val = 1): mat2 = mat.reshape((-1,1), order='F') (n,m) = mat.shape n2 = mat2.shape[0] for (el,idx) in zip(np.nditer(mat2),range(0,n2)): if el == val: return idx / n return None def find_miny(mat,val = 1): mat2 = mat.reshape((1,-1)) (n,m) = mat.shape n2 = mat2.shape[1] for (el,idx) in zip(np.nditer(mat2),range(0,n2)): if el == val: return idx / m return None def find_maxx(mat,val = 1): mat2 = np.rot90(np.rot90(mat)).reshape((-1,1), order='F') (n,m) = mat.shape n2 = mat2.shape[0] for (el,idx) in zip(np.nditer(mat2),range(0,n2)): if el == val: return m - 1 - idx / n return None def find_maxy(mat,val = 1): mat_rotated = np.rot90(np.rot90(mat)) mat2 = mat_rotated.reshape((1,-1)) (n,m) = mat.shape n2 = mat2.shape[1] for (el,idx) in zip(np.nditer(mat2),range(0,n2)): if el == val: return n - 1 - idx / m return None def calc_image_crop(img, val = 255): minx = find_minx(img,val) maxx = find_maxx(img,val) miny = find_miny(img,val) maxy = find_maxy(img,val) return (minx,maxx,miny,maxy)