bwlabel是用来标记二维的二值图像中的连通组的,简言之,就是黑背景下面有多少白的块,也就是从黑背景甄别白块块的。
L = bwlabel(BW, n) returns a matrix L, of the same size as BW, containing labels for the connected objects in BW. The variable 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.
[gpuarrayL, num] = bwlabel(gpuarrayBW, n) performs the labeling operation on a GPU. The input image and output image are gpuArrays. n can be a numeric array or a gpuArray.
在BW数组中,0代表黑背景,1代表白
用法:
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中连通区域的个数。
举例说明:
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