diff --git a/celldetection/data/__init__.py b/celldetection/data/__init__.py index d2ee8e0..9fe2724 100644 --- a/celldetection/data/__init__.py +++ b/celldetection/data/__init__.py @@ -2,7 +2,7 @@ This submodule contains data related code and numpy operations. """ from .cpn import CPNTargetGenerator, contours2labels, render_contour, clip_contour_, masks2labels, \ - labels2contour_list as labels2contours + labels2contour_list as labels2contours, contours2boxes from .misc import * from .instance_eval import LabelMatcherList, LabelMatcher from .segmentation import * diff --git a/celldetection/data/cpn.py b/celldetection/data/cpn.py index 1e3d8bb..1f32b37 100644 --- a/celldetection/data/cpn.py +++ b/celldetection/data/cpn.py @@ -202,6 +202,22 @@ def contours2fourier(contours: dict, order=5, dtype=np.float32): return fouriers, locations +def contours2boxes(contours): + """Contours to boxes. + + Args: + contours: Array[num_contours, num_points, 2]. (x, y) format. + + Returns: + Array[num_contours, 4]. (x0, y0, x1, y1) format. + """ + if len(contours): + boxes = np.concatenate((contours.min(1), contours.max(1)), 1) + else: + boxes = np.empty((0, 4)) + return boxes + + def render_contour(contour, val=1, dtype='int32'): xmin, ymin = np.floor(np.min(contour, axis=0)).astype('int') xmax, ymax = np.ceil(np.max(contour, axis=0)).astype('int')