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

How to remove small object [3d volume] using cc3d.connected_components ? #21

Closed
selvakarna opened this issue Jun 18, 2019 · 5 comments
Closed
Labels
question Further information is requested

Comments

@selvakarna
Copy link

How to remove small object [3d volume] using cc3d.connected_components ?

@william-silversmith william-silversmith added the question Further information is requested label Jun 18, 2019
@william-silversmith
Copy link
Contributor

william-silversmith commented Jun 18, 2019

This snippet should do it for you. :)

cc_labels = cc3d.connected_components(img)
cc_segids, pxct = np.unique(cc_labels, return_counts=True)
mask_ids = [ sid for sid, ct in zip(cc_segids, pxct) if ct < dust_threshold and sid != 0 ]
cc_labels[ np.in(cc_labels, mask_ids) ] = 0

If you're willing to download another dependency I also write a package called fastremap that can do it like so, which may be slightly faster or less memory intensive:

cc_labels = cc3d.connected_components(img)
cc_segids, pxct = np.unique(cc_labels, return_counts=True)
mask_ids = [ sid for sid, ct in zip(cc_segids, pxct) if ct < dust_threshold and sid != 0 ]
cc_labels = fastremap.mask(cc_labels, mask_ids)

@selvakarna
Copy link
Author

Thanks william-silversmith.

@william-silversmith
Copy link
Contributor

You're welcome!

@selvakarna
Copy link
Author

selvakarna commented Jul 4, 2019

from this above code:

cc_labels = cc3d.connected_components(img)
cc_segids, pxct = np.unique(cc_labels, return_counts=True)
mask_ids = [ sid for sid, ct in zip(cc_segids, pxct) if ct < dust_threshold and sid != 0 ]
cc_labels[ np.in(cc_labels, mask_ids) ] = 0

when i use volume image to this above code, nothing 3d small object removed ? so how to remove small 3d volume object? i set dust_threshold= 50, but its not working[ small object cannot removed] ?
@william-silversmith

@william-silversmith
Copy link
Contributor

william-silversmith commented Dec 23, 2021

For anyone reading this thread these days, cc3d.dust is available as of 3.7.0 which will remove components smaller than a specified number of voxels. If you want to do something fancy like "as a fraction of the size of the largest shape" you can do a pass with cc3d.statistics to compute it.

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

No branches or pull requests

2 participants