Skip to content

Commit bd2f271

Browse files
perf: vectorize connected_components_stack 6-connected
1 parent c367072 commit bd2f271

1 file changed

Lines changed: 19 additions & 8 deletions

File tree

cc3d/__init__.py

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -433,14 +433,25 @@ def connected_components_stack(
433433
equivalences.makeset(u)
434434

435435
if connectivity == 6:
436-
for y in range(image.shape[1]):
437-
for x in range(image.shape[0]):
438-
if bottom_cc_labels[x,y] == 0 or top_cc_labels[x,y] == 0:
439-
continue
440-
if ((not binary_image and bottom_cc_img[x,y] == image[x,y,0])
441-
or (binary_image and bottom_cc_img[x,y] and image[x,y,0])):
442-
443-
equivalences.union(bottom_cc_labels[x,y], top_cc_labels[x,y])
436+
mask = (
437+
(bottom_cc_labels != 0)
438+
& (top_cc_labels != 0)
439+
)
440+
if not binary_image:
441+
mask &= (bottom_cc_img == image[:, :, 0])
442+
443+
bottom_labels = np.asarray(bottom_cc_labels)[mask]
444+
top_labels = np.asarray(top_cc_labels)[mask]
445+
del mask
446+
edges = np.column_stack((bottom_labels, top_labels))
447+
del bottom_labels
448+
del top_labels
449+
450+
edges = fastremap.unique(edges, sorted=False, axis=0)
451+
452+
for b, t in edges:
453+
equivalences.union(b, t)
454+
del edges
444455
else:
445456
for y in range(image.shape[1]):
446457
for x in range(image.shape[0]):

0 commit comments

Comments
 (0)