# Synopse

Gray-scale statistics for each labeled region.

• y = iagrain(fr, f, measurement, option="image")
• y: Image
• fr: Image Labeled image, to define the regions. Label 0 is the background region.
• f: Image To extract the measurements.
• 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).
```01. import numpy as np
02. from string import upper
03.
04. def iagrain(fr, f, measurement, option="image"):
05.
06.     measurement = upper(measurement)
07.     option      = upper(option)
08.     if fr.ndim == 1: fr = fr[newaxis,:]
09.     n = fr.max()
10.     if option == 'DATA': y = np.empty((n,),np.float)
11.     else               : y = np.zeros(fr.shape)
12.     if measurement == 'MAX':
13.         for i in range(1,n+1):
14.             val = f[fr==i].max()
15.             if option == 'DATA': y[i-1] = val
16.             else               : y[fr==i] = val
17.     elif measurement == 'MIN':
18.         for i in range(1,n+1):
19.             val = f[fr==i].min()
20.             if option == 'DATA': y[i-1] = val
21.             else               : y[fr==i] = val
22.     elif measurement == 'SUM':
23.         for i in range(1,n+1):
24.             val = f[fr==i].sum()
25.             if option == 'DATA': y[i-1] = val
26.             else               : y[fr==i] = val
27.     elif measurement == 'MEAN':
28.         for i in range(1,n+1):
29.             val = f[fr==i].mean()
30.             if option == 'DATA': y[i-1] = val
31.             else               : y[fr==i] = val
32.     elif measurement == 'STD':
33.         for i in range(1,n+1):
34.             v = f[fr==i].std()
35.             if len(v) < 2: val = 0
36.             else         : val = v.std()
37.             if option == 'DATA': y[i-1] = val
38.             else               : y[fr==i] = val
39.     elif measurement == 'STD1':
40.         print "'STD1' is not implemented"
41.     else:
42.         print "Measurement should be 'MAX', 'MIN', 'MEAN', 'SUM', 'STD', 'STD1'."
43.     return y```

# 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 None. 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

## Example 1

```01. from ia870 import iagrain
02. from ia870 import ialabelflat
03. import ia636
04. import numpy as np
05.
06. f =array([[ 1, 2, 3, 4,5],
07.           [ 6, 7, 8, 9,10],
08.           [11,12,13,14,15]],np.uint8)
09. fr=array([[ 1, 1, 1, 3, 3],
10.           [ 1, 1, 1, 3, 3],
11.           [ 2, 2, 2, 2, 3]],np.uint8)
12. print iagrain(fr,f,'max','data')
13. print iagrain(fr,f,'sum')```
```[  8.  14.  15.]
[[ 27.  27.  27.  43.  43.]
[ 27.  27.  27.  43.  43.]
[ 50.  50.  50.  50.  43.]]
```