GLCM features¶
| Fact | Value |
|---|---|
| Also called | Gray-level co-occurrence matrix, Haralick texture features, Co-occurrence matrix |
| Status | Implemented and tested |
| Family | Texture, corners, blobs and features |
| Engine | Science engine |
| Type | Nonlinear, statistical, co-occurrence statistics |
| Output | Table |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/texture.py, glcm |
| Operator code | engine/src/pixelnexus_engine/filters/texture.py, glcm_representation |
| Packages | scikit-image, NumPy |
Overview¶
The gray-level co-occurrence matrix counts pairs of pixels at a fixed offset, such as one pixel to the right. It tells how often each gray level has each other level as its partner. Haralick, Shanmugam and Dinstein turned this matrix into texture measures, such as contrast, homogeneity, energy and correlation. Smooth areas put most counts on the diagonal of the matrix, where both levels are the same. Rough areas spread them away from it. The filter gives a table of all measures for the whole image in the Features panel. Or it maps one measure in a window around every pixel. It runs in the PixelNexus engine, with scikit-image.
How it works¶
- Quantize the 8-bit luma to the chosen number of levels.
- Count the pairs of levels at the chosen offset, in the image or in each window.
- Normalize the counts to the matrix P.
- Compute the properties, as a table, or one property as a map.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(q\): The quantized luma
- \(d\): The offset of the pair
- \(P\): The normalized co-occurrence matrix
The luma is quantized to q, from 0 to levels - 1. For an offset d, the gray-level co-occurrence matrix P counts how often level i has level j at the offset d. It is divided by the number of pairs. Symmetric counting adds each pair both ways. Each property sums P with a weight. Contrast weights the squared difference of levels, so it is high on rough texture. Dissimilarity uses the absolute difference. Homogeneity weights 1 / (1 + (i - j)^2), high where neighbors match. The angular second moment sums P^2, and energy is its square root, high where few pairs dominate. Correlation measures how linearly neighboring levels depend on each other. Entropy is high where many pairs are equally common. All follow scikit-image's graycoprops. The map computes P of the window around each pixel, counting the pairs with both pixels inside it, and stretches the property to the full gray range.
The matrix has one row and one column for each gray level, so the luma is first quantized to a few levels. With 8 levels, each level covers 32 gray values, and the matrix has 64 entries. A window of 15 x 15 pixels has 210 pairs at distance 1 to fill them.
The offset has a distance and a direction. Texture with a direction gives different matrices at different angles. Vertical stripes change from pixel to pixel along a row, but not along a column, so their contrast is high at 0 degrees and 0 at 90 degrees. Symmetric counting adds each pair in both orders. Then the matrix equals its transpose, and the angles 0 and 180 degrees give the same values.
The other measures, in the form of scikit-image's graycoprops:
- Dissimilarity: the sum of P (i, j) times the absolute difference of i and j.
- Homogeneity: the sum of P (i, j) / (1 + (i - j)^2), high where neighbors have the same level.
- The angular second moment, ASM: the sum of P (i, j)^2, high where a few pairs dominate. Energy is its square root.
- Correlation: the sum of P (i, j) (i - mu_i) (j - mu_j) / (sigma_i sigma_j). Here mu and sigma are the means and standard deviations of the rows and columns of P. Then scikit-image gives 1 where sigma is 0, as in a flat area.
- Entropy: minus the sum of P (i, j) ln P (i, j), high where many pairs are equally common.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages with the current settings. They are the quantization, the matrix with its offset in x and y, and the properties of the table or the map.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Table for the whole image | Table for the whole image, Map of one property | All properties for the whole image as a table, or one property in a sliding window as an image. |
Gray levels levels |
8 | 2 to 16 | The luma is quantized to this many levels, so the matrix has levels x levels entries. |
Distance distance |
1 | 1 to 20 px | How far apart the two pixels of a pair are. The table also lists distance 1. |
Angle angle |
0 degrees (right) | 0 degrees (right), 45 degrees (up right), 90 degrees (up), 135 degrees (up left) | The direction from the first pixel of a pair to the second, counterclockwise from the right. Used by the map. The table lists all four. |
Symmetric symmetric |
on | On or off | Count each pair both ways, so the matrix is symmetric and opposite directions give the same values. |
Map property property |
Contrast | Contrast, Dissimilarity, Homogeneity, Angular second moment, Energy, Correlation, Entropy | The property the map shows. |
Window window |
15 | 3 to 63, odd px | The side of the square window of the map. |
Implementation¶
glcm in engine/src/pixelnexus_engine/filters/texture.py quantizes the 8-bit luma to q = floor(levels Y / 256). Angles count counterclockwise from the x axis, so 90 degrees pairs a pixel with the one above it. Here scikit-image's graycomatrix counts its angles the other way, so it gets the negated angle.
The table calls graycomatrix and graycoprops for the angles 0, 45, 90 and 135 degrees, at distance 1 and at the chosen distance. At 45 degrees, the offset is the distance times the cosine and sine, rounded, so distance 1 reaches the diagonal neighbor.
The map computes the matrix of the window around every pixel at once. For each pair of levels i and j, it marks the pixels of level i whose partner has level j. A window sum on an integral image then counts the marks, so its cost does not depend on the window size. Only pairs with both pixels in the window count, which is what graycomatrix counts on the window cut out. Each window therefore gives the same values as scikit-image. Windows are cut at the image edge. The map is stretched from its smallest to its largest value.
Worked example¶
| Quarter | Angle (deg) | Contrast | Homogeneity | Energy | Correlation |
|---|---|---|---|---|---|
| Stripes | 0 | 1.722 | 0.428 | 0.354 | 0.687 |
| Stripes | 90 | 0.000 | 1.000 | 0.468 | 1.000 |
| Fine noise | 0 | 3.020 | 0.469 | 0.225 | 0.002 |
| Fine noise | 90 | 2.998 | 0.472 | 0.225 | 0.007 |
| Coarse | 0 | 0.222 | 0.889 | 0.404 | 0.912 |
| Coarse | 90 | 0.195 | 0.902 | 0.415 | 0.921 |
| Flat | 0 | 0.000 | 1.000 | 1.000 | 1.000 |
| Flat | 90 | 0.000 | 1.000 | 1.000 | 1.000 |
The table output with the defaults, 8 levels and distance 1, for each quarter of the test image of the figure, 80 x 80, on its own. The table lists four angles. Two are shown here.
The stripes have contrast at 0 degrees and none at 90 degrees, since they are vertical. The noise quarters give about the same values at both angles. The fine noise has the highest contrast and almost no correlation, since neighbors are independent. The coarse noise has high homogeneity and correlation, since neighbors are alike. The flat quarter stays inside one level, so its matrix has a single entry: no contrast, and energy and homogeneity of 1.
Complexity¶
- Time: \(O(N) for the table, O(L^2 N) for the map\)
- Memory: \(O(N)\)
N is the number of pixels and L the number of levels. The map counts each pair of levels with window sums on an integral image.
Artifacts and limitations¶
Limitations
- Few levels lose detail, many levels need large windows to fill the matrix.
- The map has at most 16 levels, since it counts each pair of levels separately.
- The map is stretched to the full gray range, so its gray values are only comparable within one image.
Artifacts
- Soft borders in the map, about half a window wide.
Tests¶
The offsets must count counterclockwise with y down. Each table row must equal graycoprops of a matrix counted pair by pair with our offsets, with and without symmetry. Distance 1 must be listed once. For every property, the map at sample pixels, the corners included, must equal graycoprops of the window cut out. The map must be stretched to the full range, and a flat image must give a black map. On the Siemens star, the mean contrast in rings around the center must fall from the center outward. The engine API test checks the JSON table.
References¶
- Haralick, R. M., Shanmugam, K., and Dinstein, I. Textural features for image classification. IEEE Transactions on Systems, Man, and Cybernetics SMC-3(6), 610 to 621, 1973. DOI 10.1109/TSMC.1973.4309314.
- scikit-image documentation, skimage.feature.graycomatrix and graycoprops.