連通元件標記(或稱連通元件分析、斑點提取、區域標記、斑點發現或區域提取)在影像中唯一標記連通元件。標記過程從左上到右下逐像素掃描影像,以識別連通像素區域,即共享相同強度值集的相鄰像素區域。例如,讓我們在這張影像中找到物件
要識別此影像中的物件,請使用以下命令
magick objects.gif -connected-components 4 -auto-level -depth 8 objects.png
偵測到的物件會被唯一標記。使用自動色階調整來顯示偵測到的物件
物件統計資訊很有用。要顯示它們,請使用以下命令
magick objects.gif -define connected-components:verbose=true -connected-components 4 objects.png
在來源影像中偵測到五個物件,其統計資訊如下
Objects (id: bounding-box centroid area mean-color): 0: 256x171+0+0 119.2,80.8 33117 srgb(0,0,0) 2: 120x135+104+18 159.5,106.5 8690 srgb(255,255,255) 3: 50x36+129+44 154.2,63.4 1529 srgb(0,0,0) 4: 21x23+0+45 8.8,55.9 409 srgb(255,255,255) 1: 4x10+252+0 253.9,4.1 31 srgb(255,255,255)
新增 -define connected-components:exclude-header=true 以顯示不含標題行的物件。新增 -define connected-components:exclude-ids=true。使用 -define connected-components:sort=area | width | height | x | y 對詳細的連通元件物件進行排序。預設情況下,物件按面積降序排列。新增 -define connected-components:sort-order=increasing | decreasing 指定排序順序。
使用 -connected-components 8 訪問 8 個鄰居而不是 4 個。預設情況下,鄰居顏色必須完全相同才能成為唯一物件的一部分。使用 -fuzz 選項將顏色接近的像素包含在物件中。
您可能希望透過將小物體與其較大的鄰居合併來消除它們。如果是這樣,請使用以下命令
magick objects.gif -define connected-components:area-threshold=410 -connected-components 4 \ -auto-level objects.jpg
以下是預期結果。請注意,小物體現在如何與背景合併。
請注意,其中兩個物件如何合併,剩下三個物件
Objects (id: bounding-box centroid area mean-color): 0: 256x171+0+0 118.0,80.4 33557 srgb(0,0,0) 2: 120x135+104+18 159.5,106.5 8690 srgb(255,255,255) 3: 50x36+129+44 154.2,63.4 1529 srgb(0,0,0)
預設情況下,標記影像為灰階。您可以改為使用來源影像中的平均顏色替換標記影像中的物件顏色。只需在您的命令列中新增此設定,-define connected-components:mean-color=true。
閾值可以選擇性地包含範圍,例如 -define connected-components:area-threshold=410-1600。要保留背景物件,請使用 -define connected-components:background-id=object-id 識別它。預設的背景物件是面積最大的物件。
除了面積之外,還支援以下閾值指標
- connected-components:angle-threshold(來自等效橢圓)
- connected-components:circularity-threshold (4*pi*area/perimeter^2)
- connected-components:diameter-threshold (sqrt(4*area/pi))
- connected-components:eccentricity-threshold(來自等效橢圓)
- connected-components:major-axis-threshold(來自等效橢圓的直徑)
- connected-components:minor-axis-threshold(來自等效橢圓的直徑)
- connected-components:perimeter-threshold
您可能想要移除某些物件。使用 -define connected-components:remove-ids=list-of-ids(例如 -define connected-components:remove-ids=2,4-5)。或者使用 -define connected-components:keep-ids=list-of-ids 保留這些物件並合併所有其他物件。為了方便起見,您可以使用以下選項保留頂部物件:-define connected-components:keep-top=number-of-objects。您可以移除或保留由其顏色識別的物件,而不是物件 ID,例如 -define connected-components:keep-colors=red;green;blue。
影像中的物件可能看起來很均勻,但顏色值略有不同。預設情況下,只有完全匹配的像素才會被視為特定物件的一部分。對於物件中顏色的細微變化,請使用 -fuzz。例如,
magick star-map.png -fuzz 5% -define connected-components:verbose=true \ -define connected-components:mean-color=true -connected-components 4 stars.gif
連通元件
此演算法以常見的行-列順序遍歷組件的像素,尋找上方或左側的組件。對於左上角的組件,上方或左側沒有可以合併的組件。因此,在某些特殊情況下,您需要旋轉、重複連通組件,然後再旋轉回來。例如:
magick \ objects.gif \ -define connected-components:verbose=true \ -define connected-components:area-threshold=6000 \ -virtual-pixel None \ -connected-components 4 -rotate 180 \ -connected-components 4 -rotate -180 \ objects.png