Color Modes

連結成分ラベリング

連結成分ラベリング(連結成分解析、ブロブ抽出、領域ラベリング、ブロブ検出、または領域抽出とも呼ばれます)は、画像内の連結成分を一意にラベル付けします。ラベリングプロセスは、連結したピクセル領域、すなわち同じ強度値のセットを共有する隣接ピクセルの領域を識別するために、左上から右下へ、ピクセル単位で画像をスキャンします。たとえば、この画像内のオブジェクトを見つけてみましょう。

    purse

この画像内のオブジェクトを識別するには、次のコマンドを使用します。

magick objects.gif -connected-components 4 -auto-level -depth 8 objects.png

検出されたオブジェクトには、一意のラベルが付けられます。自動レベル調整を使用して、検出されたオブジェクトを可視化します。

    Objects

オブジェクトの統計は、確認に役立ちます。それらを表示するには、次のコマンドを使用します。

magick objects.gif -define connected-components:verbose=true -connected-components 4 objects.png

ソース画像で、次の統計情報を持つ 5 つのオブジェクトが検出されました。

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 を使用して、4 つではなく 8 つの隣接ピクセルを訪問します。デフォルトでは、隣接ピクセルが同一の色である場合にのみ、一意のオブジェクトの一部と見なされます。色に近いピクセルをオブジェクトの一部として含めるには、-fuzz オプションを使用します。

小さなオブジェクトを、より大きな隣接オブジェクトとマージして削除したい場合があります。その場合は、次のコマンドを使用します。

magick objects.gif -define connected-components:area-threshold=410 -connected-components 4 \
  -auto-level objects.jpg

期待される結果は次のとおりです。小さなオブジェクトが背景とマージされていることに注目してください。

    Objects

2 つのオブジェクトがマージされ、3 つのオブジェクトが残っていることに注目してください。

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