最近在项目中需要对分割模型得到的mask区域进行矩形裁切,就是截取包含mask的矩形区域。中间需要获取最左最右的x,最上最下的y,然后根据这个进行裁切。
index = np.nonzero(output)
if len(index[0])!=0 and len(index[1])!=0:
minx = np.min(index[1])
maxx = np.max(index[1])
miny = np.min(index[0])
maxy = np.max(index[0])
image = image[miny:maxy, minx:maxx]