Skip to content

Commit 523f783

Browse files
perf: faster foreground offset
1 parent 48856e5 commit 523f783

2 files changed

Lines changed: 19 additions & 1 deletion

File tree

cc3d/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,8 @@ def connected_components_stack(
410410
return_N=True, out_dtype=np.uint64,
411411
binary_image=bool(binary_image),
412412
)
413-
np.add(cc_labels, offset, out=cc_labels, where=(cc_labels != 0))
413+
# np.add(cc_labels, offset, out=cc_labels, where=(cc_labels != 0))
414+
fastcc3d.offset_foreground(cc_labels, offset)
414415
offset += N
415416
binary = crackle.compress(cc_labels)
416417

cc3d/fastcc3d.pyx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1370,3 +1370,20 @@ def each(
13701370
if in_place:
13711371
return InPlaceImageIterator()
13721372
return ImageIterator()
1373+
1374+
@cython.boundscheck(False)
1375+
@cython.wraparound(False)
1376+
def offset_foreground(
1377+
cnp.ndarray[cnp.uint64_t, ndim=3] labels,
1378+
cnp.uint64_t offset,
1379+
):
1380+
cdef:
1381+
Py_ssize_t i
1382+
Py_ssize_t n = labels.size
1383+
cnp.uint64_t* data = <cnp.uint64_t*>labels.data
1384+
1385+
with nogil:
1386+
for i in range(n):
1387+
if data[i] != 0:
1388+
data[i] += offset
1389+

0 commit comments

Comments
 (0)