Label connected components in binary image using non-standard pixel connectivity

2 ビュー (過去 30 日間)
Assume I have the following binary image
BW = [ 1 0 0 0;
0 0 0 0;
0 1 1 0;
0 0 0 0;
0 0 0 0
0 1 1 0 ];
I would like to find all connected components in this binary image, where 2 points are connected if they are at most 2 rows and at most 1 column away from each other and both have the value 1. The function bwlabeln seems to be useful for this task, but it does not provide me the flexibility of specifying the desired pixel connectivity. My problem statement seems to translate to a 5-by-3 pixel connectivity of all ones. Is there any function that can help me here?
The desired labeling of connected components is
L = [ 1 0 0 0;
0 0 0 0;
0 1 1 0;
0 0 0 0;
0 0 0 0
0 2 2 0 ];
and the number of connected components is 2.

採用された回答

Sean de Wolski
Sean de Wolski 2020 年 3 月 23 日
編集済み: Sean de Wolski 2020 年 3 月 23 日
Call imdilate with a custom structuring element of your size. This will then expand the regions so they overlap and bwlabel can then do its thing.
After, do a logical index to remove the added values from imdilate.
Id = imdilate(I, custom_element)
L = bwlabel(Id);
L(~I) = 0;
  2 件のコメント
Jori Selen
Jori Selen 2020 年 3 月 23 日
編集済み: Jori Selen 2020 年 3 月 23 日
Thanks! I cannot seem to get this to work for the MWE I provided above. It is not clear to me what custom_element should be so that I get the 5-by-3 pixel connectivity.
Sean de Wolski
Sean de Wolski 2020 年 3 月 23 日
Hi,
This should work but I may be off by 1 (i.e. test it under other conditions!). More on structuring elements here https://www.mathworks.com/help/images/structuring-elements.html:
BW = [ 1 0 0 0;
0 0 0 0;
0 1 1 0;
0 0 0 0;
0 0 0 0
0 1 1 0 ];
Id = imdilate(BW, ones(2,1));
L = bwlabel(Id);
L(~BW) = 0

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeRead, Write, and Modify Image についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by