[mmblob] [Up] [mmlabelflat] Measurements

mmgrain
Gray-scale statistics for each labeled region.

Synopsis

y = mmgrain( fr, f, measurement, option = "image" )

Implemented in Python.

Input

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

Labeled image, to define the regions. Label 0 is the background region.

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

To extract the measuremens.

measurement String

Choose the measure to compute: 'max', 'min', 'median', 'mean', 'sum', 'std', 'std1'.

option String

Output format: 'image': results as a gray-scale mosaic image (uint16); 'data': results a column vector of measurements (double).

Default: "image"

Output

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

Or a column vector (double) with gray-scale statistics per region.

Description

Computes gray-scale statistics of each grain in the image. The grains regions are specified by the labeled image fr and the gray-scale information is specified by the image f. The statistics to compute is specified by the parameter measurement, which has the same options as in function mmstats. The parameter option defines: ('image') if the output is an uint16 image where each label value is changed to the measurement value, or ('data') a double column vector. In this case, the first element (index 1) is the measurement of region 1. The region with label zero is not measure as it is normally the background.

Examples

Numerical Example
>>> f=uint8([range(6),range(6),range(6)])

              
>>> fr=mmlabelflat(f)

              
>>> mmgrain(fr,f,'sum','data')
array([[  0.],
       [  3.],
       [  6.],
       [  9.],
       [ 12.],
       [ 15.]])
>>> mmgrain(fr,f,'sum')
array([[ 0,  3,  6,  9, 12, 15],
       [ 0,  3,  6,  9, 12, 15],
       [ 0,  3,  6,  9, 12, 15]],'w')
Watershed Mosaic
The catchment basins of the watershed of the gradient from markers have all its pixels with the mean gray level within its catchment basin.
>>> f=mmreadgray('astablet.tif')

              
>>> g=mmgradm(f)

              
>>> marker=mmregmin(mmclose(g))

              
>>> ws=mmcwatershed(g,marker,mmsebox(),'regions')

              
>>> g=mmgrain(ws,f,'mean')

              
>>> mmshow(f)

              
>>> mmshow(g)

            
f g

Source Code

def mmgrain(fr, f, measurement, option="image"):
    from Numeric import NewAxis, ravel, zeros, sum, nonzero, put, take, array
    from MLab import mean, std
    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 == 'MAX':
        for i in range(1,n+1):
            aux = fr==i
            val = max(ravel(aux*f))
            if option == 'DATA': y.append(val)
            else               : put(ravel(y), nonzero(ravel(aux)), val)
    elif measurement == 'MIN':
        for i in range(1,n+1):
            aux = fr==i
            lin = ravel(aux*f)
            ind = nonzero(ravel(aux))
            val = min(take(lin,ind))
            if option == 'DATA': y.append(val)
            else               : put(ravel(y), ind, val)
    elif measurement == 'SUM':
        for i in range(1,n+1):
            aux = fr==i
            val = sum(ravel(aux*f))
            if option == 'DATA': y.append(val)
            else               : put(ravel(y), nonzero(ravel(aux)), val)
    elif measurement == 'MEAN':
        for i in range(1,n+1):
            aux = fr==i
            ind = nonzero(ravel(aux))
            val = mean(take(ravel(aux*f), ind))
            if option == 'DATA': y.append(val)
            else               : put(ravel(y), ind, val)
    elif measurement == 'STD':
        for i in range(1,n+1):
            aux = fr==i
            ind = nonzero(ravel(aux))
            v   = take(ravel(aux*f), ind)
            if len(v) < 2: val = 0
            else         : val = std(v)
            if option == 'DATA': y.append(val)
            else               : put(ravel(y), ind, val)
    elif measurement == 'STD1':
        print "'STD1' is not implemented"
    else:
        print "Measurement should be 'MAX', 'MIN', 'MEAN', 'SUM', 'STD', 'STD1'."
    if option == 'DATA':
        y = array(y)
        if len(y.shape) == 1: y = y[:,NewAxis]
    return y
    

See also

mmlabel Label a binary image.
mmblob Blob measurements from a labeled image.
mmlabelflat Label the flat zones of gray-scale images.
mmstats Find global image statistics.
[mmblob] [Up] [mmlabelflat] Python