python 三维连通域分析

作材料缺陷分析的时候,会用刀三维连通域分析这个算法,做为一个不入流的JS码农,算法我是万万不会本身去写的,并且仍是用python去写。不过好在,确实有人写出了很成功的库,能够得以引用,我这里就来重点介绍一个这个库。python

Connected Components 3D

库的源地址:github.com/seung-lab/c…
库的安装:git

#确保你的numpy 库版本是在1.16以上的
pip install connected-components-3d
复制代码

样例:github

import cc3d
import numpy as np

labels_in = np.ones((512, 512, 512), dtype=np.int32)
labels_out = cc3d.connected_components(labels_in) # 26-connected

connectivity = 6 # only 26, 18, and 6 are allowed
labels_out = cc3d.connected_components(labels_in, connectivity=connectivity)

# You can adjust the bit width of the output to accomodate
# different expected image statistics with memory usage tradeoffs.
# uint16, uint32 (default), and uint64 are supported.
labels_out = cc3d.connected_components(labels_in, out_dtype=np.uint16)

# You can extract individual components like so:
N = np.max(labels_out)
for segid in range(1, N+1):
  extracted_image = labels_out * (labels_out == segid)
  process(extracted_image)

# We also include a region adjacency graph function 
# that returns a set of undirected edges.
graph = cc3d.region_graph(labels_out, connectivity=connectivity) 
复制代码

更多的说明:
能够经过二位连通域算法所获得的数据结果来理解三维连通域分析。(能够参考opencv connectComponentsWithStats 这个算法)算法

0 0 0 0 0 0 0 0 0 0                      0 0 0 0 0 0 0 0 0 0  
0 1 1 0 0 0 0 0 0 0                      0 1 1 0 0 0 0 0 0 0  
0 1 1 0 0 0 1 1 1 0                      0 1 1 0 0 0 3 3 3 0  
0 0 0 0 0 0 1 1 1 0                      0 0 0 0 0 0 3 3 3 0
0 1 1 1 1 0 0 0 0 0                      0 2 2 2 2 0 0 0 0 0                      
0 0 0 0 0 0 0 0 0 0                      0 0 0 0 0 0 0 0 0 0
复制代码

对二维数据进行连通域分析的时候,算法就是将连通域经过 不一样的数字0(0表示为背景),1,2,3标记出来,而后就能够从中取得这些连通域,作后续的分析处理了。 三维数据一次类推。post

喜欢Python的能够添加个人学习交流群,里面赠送全套学习资料,更多萌新大佬在一块儿探讨:【python交流群】学习

参考网址:https://juejin.im/post/6876361710127710216ui

相关文章
相关标签/搜索