Demonstrate blob measurements and display.
In this example, a binary image with many blobs is processed. The objective is to measure the area of each blob. First the blobs are labeled. To display the correspondent label number of each blob, a trick is used to dilate each labeled centroid by a structuring element formed by the number of the blob label. At final, the area of each blob is plotted against its label number.
Image reading and labeling
The binary image is read and then labeled. The number of blobs is measured as the maximum label value. Both images are displayed.
01. from ia870 import ialabel 02. from ia870 import iaglblshow 03. from ia870 import iastats 04. 05. 06. f = adread('blob3.tif') 07. fr = ialabel(f) 08. adshow(f) 09. adshow(iaglblshow(fr,'border')) 10. nblobs=iastats(fr,'max') 11. print 'nblos=',nblobs
The centroids are computed from the labeled image. After, the centroid image is labeled, so that each centroid point has a label value varying from 1 to the maximum number of blobs. For display illustration, the centroids are overlayed on the original blob image on the left and the labeled centroids are enlarged and displayed on the right.
1. from ia870 import iablob, iadil, iagshow 2. 3. c = iablob(fr,'centroid') 4. cr = ialabel(c) 5. adshow(iagshow(f,c)) 6. adshow(iaglblshow(iadil(cr)))
Placing a number on a particular blob
To place a particular number on a particular blob, a number image is generated using the iatext function and converted to a structuring element. A particular centroid is selected by comparing the image with the labeled number. This output image is a binary image with a single point at that centroid. Dilating this image by the structuring element will stamp the structuring element on the centroid.
1. from ia870 import iatext, iadil, iagshow 2. 3. fbin = (cr == 5) 4. b5 = iatext('5') 5. fb5 = iadil(fbin,b5) 6. adshow( iadil(fbin)) 7. adshow(iagshow(f,fb5))
Placing a label number to each labeled blob
To automate the process just described, a loop scans every label value and stamp its number in a final image. The stamps are accumulated with the iaunion function. The area is computed and plotted against each label blob number.
01. from ia870 import iasubm 02. from ia870 import iaunion 03. 04. facc=iasubm(f,f) 05. for i in range(1,int(nblobs+1)): 06. fbin = (cr == i) 07. bi = iatext(str(i)) 08. fbi = iadil(fbin,bi) 09. facc = iaunion(facc,fbi) 10. adshow(iagshow(f,facc)) 11. darea = iablob(fr,'area','data') 12. mmplot([[, darea, '', 'impulses']])