@@ -37,11 +37,24 @@ def find_all_objects_2D(np.uint32_t[:, :] label_img):
3737 if j < cs[label]: cs[label] = < unsigned int > j
3838 if j + 1 > ce[label]: ce[label] = < unsigned int > (j + 1 )
3939
40- return [
41- (lbl, (rs[lbl], re[lbl], cs[lbl], ce[lbl]))
42- for lbl in range (1 , max_label + 1 )
43- if re[lbl] != 0
44- ]
40+ # Collect present labels into compact numpy arrays (avoids per-label tuple allocation)
41+ cdef unsigned int n_labels = 0
42+ for lbl in range (1 , max_label + 1 ):
43+ if re[lbl] != 0 :
44+ n_labels += 1
45+
46+ cdef np.ndarray[np.uint32_t, ndim= 1 ] out_labels = np.empty(n_labels, dtype = np.uint32)
47+ cdef np.ndarray[np.uint32_t, ndim= 2 ] out_bboxes = np.empty((n_labels, 4 ), dtype = np.uint32)
48+ cdef unsigned int idx = 0
49+ for lbl in range (1 , max_label + 1 ):
50+ if re[lbl] != 0 :
51+ out_labels[idx] = lbl
52+ out_bboxes[idx, 0 ] = rs[lbl]
53+ out_bboxes[idx, 1 ] = re[lbl]
54+ out_bboxes[idx, 2 ] = cs[lbl]
55+ out_bboxes[idx, 3 ] = ce[lbl]
56+ idx += 1
57+ return out_labels, out_bboxes
4558
4659def find_all_objects_3D (np.uint32_t[:, :, :] label_img ):
4760 cdef Py_ssize_t n_z = label_img.shape[0 ]
@@ -83,8 +96,23 @@ def find_all_objects_3D(np.uint32_t[:, :, :] label_img):
8396 if k < cs[label]: cs[label] = < unsigned int > k
8497 if k + 1 > ce[label]: ce[label] = < unsigned int > (k + 1 )
8598
86- return [
87- (lbl, (zs[lbl], ze[lbl], rs[lbl], re[lbl], cs[lbl], ce[lbl]))
88- for lbl in range (1 , max_label + 1 )
89- if ze[lbl] != 0
90- ]
99+ # Collect present labels into compact numpy arrays (avoids per-label tuple allocation)
100+ cdef unsigned int n_labels = 0
101+ for lbl in range (1 , max_label + 1 ):
102+ if ze[lbl] != 0 :
103+ n_labels += 1
104+
105+ cdef np.ndarray[np.uint32_t, ndim= 1 ] out_labels = np.empty(n_labels, dtype = np.uint32)
106+ cdef np.ndarray[np.uint32_t, ndim= 2 ] out_bboxes = np.empty((n_labels, 6 ), dtype = np.uint32)
107+ cdef unsigned int idx = 0
108+ for lbl in range (1 , max_label + 1 ):
109+ if ze[lbl] != 0 :
110+ out_labels[idx] = lbl
111+ out_bboxes[idx, 0 ] = zs[lbl]
112+ out_bboxes[idx, 1 ] = ze[lbl]
113+ out_bboxes[idx, 2 ] = rs[lbl]
114+ out_bboxes[idx, 3 ] = re[lbl]
115+ out_bboxes[idx, 4 ] = cs[lbl]
116+ out_bboxes[idx, 5 ] = ce[lbl]
117+ idx += 1
118+ return out_labels, out_bboxes
0 commit comments