[mmlabel] [Up] [mmgrain] Measurements

mmblob
Blob measurements from a labeled image.

Synopsis

y = mmblob( fr, measurement, option = "image" )

Implemented in Python.

Input

fr Image Gray-scale (uint8 or uint16) image.

Labeled image.

measurement String

Choice from 'AREA', 'CENTROID', or 'BOUNDINGBOX'.

option String

Output format: 'image': results as a binary image; 'data': results a column vector of measurements (double).

Default: "image"

Output

y Image Gray-scale (uint8 or uint16) or binary image.

Description

Take measurements from the labeled image fr. The measurements are: area, centroid, or bounding rectangle. The parameter option controls the output format: 'IMAGE': the result is an image; 'DATA': the result is a double column vector with the measurement for each blob. The region with label zero is not measured as it is normally the background. The measurement of region with label 1 appears at the first row of the output.

Examples

>>> fr=uint8([
   [1,1,1,0,0,0],
   [1,1,1,0,0,2],
   [1,1,1,0,2,2]])

              
>>> f_area=mmblob(fr,'area')

              
>>> print f_area
[[9 9 9 0 0 0]
 [9 9 9 0 0 3]
 [9 9 9 0 3 3]]
>>> f_cent=mmblob(fr,'centroid')

              
>>> print f_cent
[[0 0 0 0 0 0]
 [0 1 0 0 1 0]
 [0 0 0 0 0 0]]
>>> f_bb=mmblob(fr,'boundingbox')

              
>>> print f_bb
[[1 1 1 0 0 0]
 [1 0 1 0 1 1]
 [1 1 1 0 1 1]]
>>> d_area=mmblob(fr,'area','data')

              
>>> print d_area
[[9]
 [3]]
>>> d_cent=mmblob(fr,'centroid','data')

              
>>> print d_cent
[[1 1]
 [4 1]]
>>> d_bb=mmblob(fr,'boundingbox','data')

              
>>> print d_bb
[[0 0 2 2]
 [4 1 5 2]]
Area Transform
>>> f=mmreadgray('blob3.tif')

              
>>> fr=mmlabel(f)

              
>>> g=mmblob(fr,'area')

              
>>> mmshow(f)

              
>>> mmshow(g)

            
f g
Centroids
>>> f=mmreadgray('blob3.tif')

              
>>> fr=mmlabel(f)

              
>>> centr=mmblob(fr,'centroid')

              
>>> mmshow(f,mmdil(centr))

            
f,mmdil(centr)
Bounding Box
>>> f=mmreadgray('blob3.tif')

              
>>> fr=mmlabel(f)

              
>>> box=mmblob(fr,'boundingbox')

              
>>> mmshow(f,box)

            
f,box

Source Code

def mmblob(fr, measurement, option="image"):
    from Numeric import NewAxis, ravel, zeros, sum, nonzero, sometrue, array
    from string import upper
    measurement = upper(measurement)
    option      = upper(option)
    if len(fr.shape) == 1: fr = fr[NewAxis,:]
    n = max(ravel(fr))
    if option == 'DATA': y = []
    else               : y = zeros(fr.shape)
    if measurement == 'AREA':
        for i in range(1,n+1):
            aux  = fr==i
            area = sum(ravel(aux))
            if option == 'DATA': y.append(area)
            else               : y = y + area*aux
    elif measurement == 'CENTROID':
        for i in range(1,n+1):
            aux  = fr==i
            ind  = nonzero(ravel(aux))
            indx = ind / fr.shape[1]
            indy = ind % fr.shape[1]
            centroid = [sum(indx)/len(ind), sum(indy)/len(ind)]
            if option == 'DATA': y.append([centroid[1],centroid[0]])
            else               : y[centroid] = 1
    elif measurement == 'BOUNDINGBOX':
        for i in range(1,n+1):
            aux = fr==i
            aux1, aux2 = sometrue(aux,0), sometrue(aux,1)
            col , row  = nonzero(aux1)  , nonzero(aux2)
            if option == 'DATA': y.append([col[0],row[0],col[-1],row[-1]])
            else:
                y[row[0]:row[-1],col[0] ] = 1
                y[row[0]:row[-1],col[-1]] = 1
                y[row[0], col[0]:col[-1]] = 1
                y[row[-1],col[0]:col[-1]] = 1
    else:
        print "Measurement option should be 'AREA','CENTROID', or 'BOUNDINGBOX'."
    if option == 'DATA':
        y = array(y)
        if len(y.shape) == 1: y = y[:,NewAxis]
    return y
    

See also

mmlabel Label a binary image.
mmgrain Gray-scale statistics for each labeled region.
mmlabelflat Label the flat zones of gray-scale images.
mmstats Find global image statistics.
mmdrawv Superpose points, rectangles and lines on an image.
[mmlabel] [Up] [mmgrain] Python