Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Potentially Faster CCL Extraction #3

Closed
william-silversmith opened this issue Aug 17, 2022 · 1 comment
Closed

Potentially Faster CCL Extraction #3

william-silversmith opened this issue Aug 17, 2022 · 1 comment

Comments

@william-silversmith
Copy link

Hi!

I'm the author of cc3d and from time to time I search GitHub to see how people are using the library. I noticed that there are a few places that could potentially see some improvement even though you clearly made use of many of cc3d's tricks.

def remove_small_connected_object(npy_mask, area_least, out_mask, out_label):
labels_out = cc3d.connected_components(npy_mask, connectivity=26)
for label, extracted in cc3d.each(labels_out, binary=True, in_place=True):
area = np.sum(extracted)
if area > area_least:
out_mask[labels_out == int(label)] = out_label
def extract_topk_largest_candidates(npy_mask: np.array, out_num_label: List, area_least: int) -> np.array:
mask_shape = npy_mask.shape
out_mask = np.zeros([mask_shape[1], mask_shape[2], mask_shape[3]], np.uint8)
for i in range(mask_shape[0]):
t_mask = npy_mask[i].copy()
keep_topk_largest_connected_object(t_mask, out_num_label[i], area_least, out_mask, i+1)
return out_mask
def keep_topk_largest_connected_object(npy_mask, k, area_least, out_mask, out_label):
labels_out = cc3d.connected_components(npy_mask, connectivity=26)
areas = {}
for label, extracted in cc3d.each(labels_out, binary=True, in_place=True):
areas[label] = fastremap.foreground(extracted)
candidates = sorted(areas.items(), key=lambda item: item[1], reverse=True)
for i in range(min(k, len(candidates))):
if candidates[i][1] > area_least:
out_mask[labels_out == int(candidates[i][0])] = out_label

cc3d has some very efficient functions for performing these two tasks. cc3d.dust and cc3d.largest_k. You might be surprised at the improvement.

Anywho, if this isn't helpful to your project feel free to ignore these tips. Thanks for using cc3d!

Will

@Shanghai-Aitrox-Technology
Copy link
Owner

@william-silversmith 3Q for your comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants