作材料缺陷分析的時候,會用刀三維連通域分析這個算法,做爲一個不入流的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