To establish whether two grayscale images contain a similar texture, you can compare their cooccurrence matrix statistics. You use these statistics to characterize the texture; you use them to provide a signature for a given texture. Traditionally, these secondorder statistics are used to discern differences in the patterns (textures) in small images (such as tears in fabric). Taking the same statistics from a large series of images allows you to see trends that characterize the pattern/texture. With MIL, you can compute the cooccurrence matrix of a grayscale image and compute statistics from this matrix, using MimStatCalculate() with an M_STATISTICS_CONTEXT context that has some M_STAT_GLCM... enabled (MimControl()). The cooccurrence matrix is a normalized version of the frequency distribution of two grayscale values occurring at a specified pixel offset from each other. A cooccurrence matrix (and its corresponding statistics) can be calculated for the entire image or for the neighborhood of every n pixels.
MIL builds the grayscale cooccurrence matrix (GLCM) for the specified pixelpair offset, set using MimControl() with M_GLCM_PAIR_OFFSET_X and M_GLCM_PAIR_OFFSET_Y. It establishes how often each possible combination of two grayscale values occur at this offset from each other, regardless of the order of the values. For example, to evaluate the frequency that two grayscale values occur next to each other in X, set M_GLCM_PAIR_OFFSET_X to 1 and M_GLCM_PAIR_OFFSET_Y to 0. If one pixel pair is (3,0), and another pixel pair is (0,3), both are recorded in the cooccurrence matrix as (0,3). To establish the pair of a pixel on the border, mirror overscan is used.
The following table shows an example of how a source image is used to build a cooccurrence matrix.
Original source image 
Building the cooccurrence matrix for a pixel pair offset of (X + 1,Y + 0) 
The cooccurrence matrix showing the frequency of each pair of values with an offset of (X + 1,Y + 0) 




^{1} In the case where the first digit is higher than the second in a pixel pair (for example, (3,0)), the frequency of this pixel pair is counted such that the first digit is always lower than the second (so, a pixel pair of (3,0) is recorded as (0,3) in the cooccurrence matrix).
^{2} In this case, mirror overscan is used for pixel pairs that fall outside of the source image, if using the entire image. The last pixel of each row and column is mirrored.
Using cooccurrence statistics to discern discrepancies works best on small areas. As such, MIL allows you to calculate the cooccurrence matrix (and corresponding statistics) for the neighborhood of every n th pixel. Each of these neighborhoods would then have their own cooccurrence statistic results available for comparison.
Each neighborhood is considered a tile. Set the tile size using MimControl() with M_TILE_SIZE_X and M_TILE_SIZE_Y. Set the distance between the tiles (step) using M_STEP_SIZE_X and M_STEP_SIZE_Y. If the step size and tile size force tiles to overlap, they will. If some part of these tiles falls outside the image buffer, buffer overscan is used to make up the difference. It uses transparent overscan if the buffer is a child buffer and there are underlying parent buffer pixels; otherwise it uses mirror overscan. MIL tries to equalize the number of columns or rows of overscan. For example, if 2 columns of overscan would be required, one will be placed before the first column, and one after the last column. This can result in the first pixel being an overscan pixel. If the image buffer has an overscan region, it is used. If the overscan region is too small to fit the overflow, MIL will internally extend the image buffer's overscan region.
The 8 x 7 image below is divided into six 4x4 tiles that overlap in X by 1 column. To configure this example, the tile size X and Y are set to 4 (M_TILE_SIZE_X, M_TILE_SIZE_Y). The step size is set to (3,4) ( M_STEP_SIZE_X, M_STEP_SIZE_Y).
MIL supports the following six different cooccurrence matrix statistics. Note that, in the following calculations, i represents the grayscale value of the first pixel, while j represents the grayscale value of the paired pixel. P_{ij} represents the value of the normalized cooccurrence matrix of the given pixel pair. Mu _{i} (µ_{i} ) represents the weighted average index of the row (i) and Mu _{j} (µ_{j} ) represents the weighted average index of the column (j). Sigma _{i} (ơ_{i} ) represents the standard deviation of the row and Sigma _{j} (ơ_{j} ) represents the standard deviation of the column.
To enable a cooccurrence matrix statistic, use MimControl() with M_STAT_GLCM... set to M_ENABLE. To perform the operation, call MimStatCalculate().
Contrast (M_STAT_GLCM_CONTRAST). A measure of the difference between paired pixel values, whereby the difference is squared so that the calculated value grows more rapidly the further paired pixel values are from the diagonal of the cooccurrence matrix (the more the values of paired pixels are different from each other).
Dissimilarity (M_STAT_GLCM_DISSIMILARITY). A measure very similar to contrast except that the distances are not squared so that the calculated value grows linearly the further pairs of pixel values are from the diagonal of the cooccurrence matrix.
Pearson's coefficient correlation (M_STAT_GLCM_CORRELATION). A measures of how close your data comes to forming a straight line in the cooccurrence matrix. The line's orientation (diagonal or not) is not specified but the result is dependent on the slope of the line.
Energy (M_STAT_GLCM_ENERGY). A measure of the concentration of the same pixel pairs occurring in the target area. This value is high when the cooccurrence matrix has few entries of large magnitude.
Entropy (M_STAT_GLCM_ENTROPY). A measure of the irregularity of the same pixel pairs occurring in the target area. The calculated entropy value is highest when the values of the cooccurrence matrix are quite uniformly distributed through the matrix. This occurs when the area has no pairs of pixels that occur more often than others.
Homogeneity (M_STAT_GLCM_HOMOGENEITY). A measure of how similar are the values of paired pixels (how close to the diagonal are the values of paired pixels to the diagonal of the cooccurrence matrix). Homogeneity is the inverse of contrast. Homogeneity is a value between 0 and 1.
The following example calculates several cooccurrence statistics for two images of similar pieces of textile. The results are returned and drawn for comparison.
The two different images produce slightly different cooccurrence results that are visible, when drawn:
Description 
Fabric without defects 
Fabric with defects 
Source images 


Drawn version of the cooccurrence matrix 


Statistical results 
Energy: 0.025895 Contrast: 251.493018 Entropy: 7.865064 Dissimilarity: 11.860745 Homogeneity: 0.092092 Correlation: 0.938123 
Energy: 0.024421 Contrast: 305.347826 Entropy: 7.996176 Dissimilarity: 13.070599 Homogeneity: 0.084361 Correlation: 0.944868 
The cooccurrence statistic operations require that your source image is at most 10 bits deep. When performing a cooccurrence statistics operation using a 32bit buffer, MIL rescales the pixel values of the source image to fit within an unsigned integer buffer. That resulting buffer is then bitshifted to fit within the bitdepth limitation, as set by M_GLCM_QUANTIFICATION.
In the case where the source image was grabbed into a buffer with a greater bitdepth (such as, grabbing a 10bit image and storing it in a 16bit buffer), use MbufControl() with M_MAX and M_MIN to limit the maximum and minimum pixel values of the buffer. For example, a 10bit image, stored in a 16bit buffer, will be a 10bit image with the additional bits being zero or signextended. Due to the positioning of the zero or signextended bits within the 16bit image (specifically the 6 MSB are set to zero, for an unsigned buffer, or 1, for a signed buffer), your cooccurrence matrix would end up using these zero or the signextended bits in its calculations. To avoid this situation, set M_MAX to 1023 (which is 2 ^{10} 1) and set M_MIN to 0. This results in the zero or signextended bits being ignored, and the preceding MSB being used to build the cooccurrence matrix.