python 三维连通域分析
发布时间:2020.09.25
在做材料缺陷分析的时候,会用刀三维连通域分析这个算法,作为一个不入流的JS码农,算法我是万万不会自己去写的,而且还是用python去写。不过好在,确实有人写出了很成功的库,可以得以引用,我这里就来重点介绍一个这个库。
Connected Components 3D
库的源地址:github.com/seung-lab/c… 库的安装:
#确保你的numpy 库版本是在1.16以上的
pip install connected-components-3d
样例:
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标记出来,然后就可以从中取得这些连通域,做后续的分析处理了。 三维数据一次类推。
转载自:https://juejin.cn/post/6876361710127710216