<< bwdist SIP - Scilab Image Processing Toolbox curvature >>

SIP - Scilab Image Processing Toolbox >> SIP - Scilab Image Processing Toolbox > bwlabel

bwlabel

Connected component labeling

Calling Sequence

[L, n] = bwlabel(img [, nhood])

Parameters

img
A binary image, where 0 stands for background.
nhood
A scalar. The connectivity to consider in the algorithm. May be 4 or 8. Defaults to 8.

Parameters

L
A matrix of the same size as img, with the pixels of each connected object having the same number. The numbers vary from 1 to N, where N is the number of connected objects. The background is numbered 0.
n
The number of connected components. Equals to max(L).

Description

Function bwlabel numbers all the objects in a binary image. One common application is to filter out objects that have less than a certain ammount of pixels. See the examples.

You can use the Scilab find function in conjunction with bwlabel to return vectors of indices for the pixels that make up a specific object. For example, to return the coordinates for the pixels in object 3:

[r,c] = find(bwlabel(BW)==3)

Examples

//
// EXAMPLE 1
//

Img =[0     0     0     0     0     1     1
      0     1     1     0     0     1     1
      0     1     1     0     0     1     1
      0     0     0     1     0     1     1
      0     0     0     1     0     1     1
      0     0     0     1     0     1     1
      0     0     1     1     0     1     1
      0     0     0     0     0     1     1];

L = bwlabel(Img,4)

// Objects 2 and 3 are connected if 8-connectivity is used:

L = bwlabel(Img) // default: 8-connectivity

[r,c] = find(L==2);

rc = [r' c']     // coordinates of object 2!

//
// EXAMPLE 2
//
xset('auto clear', 'on');

a = gray_imread(SIPDIR + 'images/disks.bmp');

// Add some noise
//
a = imnoise(a,'salt & pepper'); 
a = 1-a;
imshow(a,2);  // convention: objects are white(1)

// Label every connected component with a unique number.
//
[L, n] = bwlabel(a);

// Shows each component with a different color
//
imshow(L+1, rand(n+1,3));

// Get one specific region (probably a single noise point)
reg = (L == 300);
imshow(reg*1, 2);

// Eliminate regions smaller than 100 pixels (noise)
// and those larger than 1000 pixels (cluttered disks)
for i=1:n
   f = find(L==i);      // linear coordinates of i-th region
   reg_size = size(f,'*');
   if reg_size < 100 | reg_size > 1000
      L(f) = 0;         // merge small regions with the background
   end
end

imshow(L+1, rand(n+1,3));   // note how the small regions are gone

// Just as a side-activity, let's fill the unwanted holes:

bw = 1*(L>0);  // binarize the image
imshow(bw,2)
bw = dilate(bw);
bw = erode(bw);
imshow(bw,2);  // every hole is now filled

xset('auto clear', 'off');

Bibliography

We use a simple stack-based flooding implementation written in C, but there exist many faster algorithms. The flood/fill region growing process may be found in most books of imaging science. For instance:

"Shape Analysis and Classification", L. da F. Costa and R. M. Cesar Jr., CRC Press, pp. 335-347.

Example of fast algorithm (not implemented):

Haralick, Robert M., and Linda G. Shapiro, Computer and Robot Vision, Volume I, Addison-Wesley, 1992, pp. 28-48.

Authors

Availability

The latest version of the Scilab Image Processing toolbox can be found at

http://siptoolbox.sourceforge.net

See Also


<< bwdist SIP - Scilab Image Processing Toolbox curvature >>