There's a typo in the example included in the documentation of greycomatrix
(emphasis mine):
Examples
Compute 2 GLCMs: One for a 1-pixel offset to the right, and one for a 1-pixel offset upwards.
>>> image = np.array([[0, 0, 1, 1],
... [0, 0, 1, 1],
... [0, 2, 2, 2],
... [2, 2, 3, 3]], dtype=np.uint8)
>>> result = greycomatrix(image, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4],
... levels=4)
Indeed, result
actually contains four different GLCM's rather than two. These four matrices correspond to the possible combinations of one distance and four angles. To calculate the GLCM corresponding to "1-pixel offset to the right" the distance and angle values should be 1
and 0
, respectively:
result = greycomatrix(image, distances=[1], angles=[0], levels=4)
whereas to calculate the GLCM corresponding to "1-pixel offset upwards" the parameters should be 1
and np.pi/2
:
result = greycomatrix(image, distances=[1], angles=[np.pi/2], levels=4)
In the example, distances=[1]
and angles=[0, np.pi/4, np.pi/2, 3*np.pi/4]
. To select a particular GLCM one has to specify the appropriate indices for angles
and distances
. Thus, the 1-pixel to the right GLCM is result[:, :, 0, 0]
and the 1-pixel upwards GLCM is result[:, :, 0, 2]
.
Finally, if you wish to calculate the "1-pixel offset downwards" GLCM (↓) you just have to transpose the "1-pixel offset upwards" GLCM (↑). It is important to note that in most cases both GLCM's are very similar. In fact, you can ignore the order of the co-occurring intensities by setting parameter symmetric
to True
in the call to greycomatrix
. By doing so, al the GLCM's returned by greycomatrix
are symmetric.