-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcropping.py
More file actions
23 lines (16 loc) · 724 Bytes
/
Copy pathcropping.py
File metadata and controls
23 lines (16 loc) · 724 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import numpy as np
def bbox_2d(data,border = 0, min_border = 1):
assert isinstance(data, np.ndarray)
if not np.any(data):
return 0,data.shape[0],0,data.shape[1]
if not isinstance(border, list):
border = np.ndim(data) * [border]
else:
assert len(border) == np.ndim(data)
x = np.any(data, axis=(1,))
y = np.any(data, axis=(0,))
xmin, xmax = np.where(x)[0][[0, -1]]
xmin, xmax = np.clip(np.array([xmin,xmax]) + np.array([-border[0]-min_border,+border[0]+min_border]),0,data.shape[0])[[0,1]]
ymin, ymax = np.where(y)[0][[0, -1]]
ymin, ymax = np.clip(np.array([ymin,ymax]) + np.array([-border[1]-min_border,+border[1]+min_border]),0,data.shape[1])[[0,1]]
return xmin, xmax, ymin, ymax