# Figure 3.11 - Labeling

## Description

The union of all connected components recovers the input image and the intersection of any two connected components is empty. The result of the labeling is usually stored in a numeric image with each pixel value associated to its connected component number.

Figure 3.11 illustrates the labeling algorithm based on reconstruction: (a) the first pixel as marker, (b) extraction of the first component, (c) the first pixel in the image without the first component, (d) second component extraction, and (e) labeled image in which each pixel has the value associated to the connected component identification. The labeling can also compute the number of connected components in the image. Particularly, it is common to implement the labeling using sequential values so that the largest label in the output image gives the number of components.

## Demo Script

``` 1 import numpy as np
2 import ia870 as MT
3 import ia636 as ia
4
6 C = MT.iaedgeoff(C)
7 P = MT.iaunion(C,uint8(1))
8
9 Bc = MT.iasecross()
10 A1 = MT.iasubm(C,C)
11 i = np.nonzero(C.flat)[0]
12 A1.flat[i]=1          #marker
14
15 D1 = MT.iainfrec(A1,C,Bc)
16 C1 = MT.iasubm(C,D1)
17 A2 = MT.iasubm(C,C)
18 i = np.nonzero(C1.flat)[0]
19 A2.flat[i]=1          #marker
21 D2 = MT.iainfrec(A2,C1,Bc)
22 C2 = MT.iasubm(C1,D1)
23 R = MT.ialabel(C,Bc)
24
25 a = MT.iaunion(MT.iagray(A1,'uint8',255),
26       MT.iagray(MT.iaintersec(C,P),'uint8', 150))
27
28 b = MT.iaunion(MT.iagray(D1,'uint8',255),
29       MT.iagray(MT.iaintersec(C1,P),'uint8',150))
30
31 c = MT.iaunion(MT.iagray(A2,'uint8',255),
32       MT.iagray(MT.iaintersec(C2,P),'uint8',150))
33
34 d = MT.iaunion(MT.iagray(D2,'uint8',255),
35       MT.iagray(MT.iaintersec(C2,P),'uint8',150))
36
37 e = R
38