bwlabel函数(二值图像中元素标记)
|
help bwlabel,第一行如示:BWLABEL Label connected components in 2-D binary image.
这就是说bwlabel是用来标记二维的二值图像中的connected components的,简言之,就是黑背景下面有多少白的块(连通组件?真别扭),反正就是从黑背景甄别白块块的。就这么理解吧。
L = BWLABEL(BW,N) returns a matrix L, of the same size as BW, containing labels for the connected components in BW. N can have a value of either 4 or 8, where 4 specifies 4-connected objects and 8 specifies
8-connected objects; if the argument is omitted, it defaults to 8. The elements of L are integer values greater than or equal to 0. The pixels labeled 0 are the background. The pixels labeled 1 make up one
object, the pixels labeled 2 make up a second object, and so on.
[L,NUM] = BWLABEL(BW,N) returns in NUM the number of connected objects
found in BW.
就是说bwlabel能从一个读入二值图像后产生的BW数组(也可能自己创建,只要符合元素是0或者1就行)中,区别出其中的1有多少块(注:在BW数组中,0代表黑背景,1代表白)
比如
0 1 1 0 0 0 1
0 1 1 0 0 0 1
0 1 1 0 0 0 1
这样的数组中,显然在0背景上有两块1,于是,bwlabe之后返回的L数组是 :
0 1 1 0 0 0 2
0 1 1 0 0 0 2
0 1 1 0 0 0 2
(当然,这个我没有实际运行,但应该没问题。)这是什么意思呢?就是说返回的L里面通过1,2,3,。。。。。n来标识某一个位置(像素)属于这个二值图像的第几个connected components。
要更深入的清晰的理解,需要理解这里面联通的定义,实际上,有4-连通(上下左右)和8-连通(八方都算连通)(甚至还有不常用的自定义连通,以后help里看到CONN这样的输入参数的时候才有用)。
如果设两个返回参数,NUM可以返回有多少个区块。
另外,类似的还有一个函数叫bwlabeln,两者差别如下:
Note: Comparing BWLABEL and BWLABELN
------------------------------------
BWLABEL supports 2-D inputs only, whereas BWLABELN support any
input dimension. In some cases you might prefer to use BWLABELN even
for 2-D problems because it can be faster. If you have a 2-D input
whose objects are relatively "thick" in the vertical direction,
BWLABEL will probably be faster; otherwise BWLABELN will probably be
faster.
速度的差别把,函数实现时对某些特殊情形做了特殊的优化。bwlabel在区块垂直方向比较长的时候比较快,其他情况下,都是bwlabeln更快。另bwlabel接受的L还可以是多维的,而bwlabel只能接受二维的L。
具体的延伸应用会有很多的,以这个标记为起始,我们可以写一些自己的函数,当然MATLAB 图像处理工具箱里面的一些函数就可以和这个很好的配合实现一些很好的应用。
比如通过regionprops函数确定每一个区块的一些特性(如中心,面积等等)
REGIONPROPS Measure properties of image regions (blob analysis).
STATS = REGIONPROPS(L,PROPERTIES) measures a set of properties for each
labeled region in the label matrix L.
PROPERTIES可以使下面这些的全部,部分组合,或者默认的basic组合:
'Area' 'ConvexHull' 'EulerNumber'
'Centroid' 'ConvexImage' 'Extrema'
'BoundingBox' 'ConvexArea' 'EquivDiameter'
'SubarrayIdx' 'Image' 'Solidity'
'MajorAxisLength' 'PixelList' 'Extent'
'MinorAxisLength' 'PixelIdxList' 'FilledImage'
'Orientation' 'FilledArea'
'Eccentricity' 'Perimeter'
L = bwlabel(BW,n)
返回一个和BW大小相同的L矩阵,包含了标记了BW中每个连通区域的类别标签,这些标签的值为1、2、num(连通区域的个数)。n的值为4或8,表示是按4连通寻找区域,还是8连通寻找,默认为8。
四连通或八连通是图像处理里的基本感念:而8连通,是说一个像素,如果和其他像素在上、下、左、右、左上角、左下角、右上角或右下角连接着,则认为他们是联通的;4连通是指,如果像素的位置在其他像素相邻的上、下、左或右,则认为他们是连接着的,连通的,在左上角、左下角、右上角或右下角连接,则不认为他们连通。
[L,num] = bwlabel(BW,n)
这里num返回的就是BW中连通区域的个数。
补充:我听说过16连通,这应该是在三维空间里的概念了吧。
举例说明:
BW =
1 1 1 0 0 0 0 0
1 1 1 0 1 1 0 0
1 1 1 0 1 1 0 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 0 1 0
1 1 1 0 0 1 1 0
1 1 1 0 0 0 0 0
按4连通计算,方形的区域,和翻转的L形区域,有用是对角连接,不属于连通,所以分开标记,连通区域个数为3
L = bwlabel(BW,4)
结果如下:
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 0 3 0
1 1 1 0 0 3 3 0
1 1 1 0 0 0 0 0
而8连通标记,它们是连通的:
[L, num] = bwlabel(BW,8)
L =
1 1 1 0 0 0 0 0
1 1 1 0 2 2 0 0
1 1 1 0 2 2 0 0
1 1 1 0 0 0 2 0
1 1 1 0 0 0 2 0
1 1 1 0 0 0 2 0
1 1 1 0 0 2 2 0
1 1 1 0 0 0 0 0
这里
num =
2