1

I'd some trouble understanding the angle parameter of greycomatrix in skimage. In the example mentioned in the documentation to compute GLCM's for the pixel to the right and up, they mention 4 angles. And they get 4 GLCM's.

>>> 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)

What should be the parameters for the pixel to the right and down?

Tonechas
  • 13,398
  • 16
  • 46
  • 80

1 Answers1

1

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.

Community
  • 1
  • 1
Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • awesome answer. Im currently trying to figure this out. Could you expand on the 'levels' parameter also? – GeoMonkey Apr 06 '17 at 21:03
  • According to skimage [documentation](http://scikit-image.org/docs/dev/api/skimage.feature.html#skimage.feature.greycomatrix): *The input image should contain integers in [0, levels-1], where levels indicate the number of grey-levels counted (typically 256 for an 8-bit image). This argument is required for 16-bit images or higher and is typically the maximum of the image. As the output matrix is at least levels x levels, it might be preferable to use binning of the input image rather than large values for levels.* The value of `levels` determines the size of the gray level co-occurrence matrix. – Tonechas Apr 06 '17 at 21:27
  • You may want to take a look into [this answer](http://stackoverflow.com/questions/34365953/black-line-in-glcm-result/43036067#43036067) to gain a deeper insight into the `levels` parameter. – Tonechas Apr 06 '17 at 21:39
  • Thanks - really useful. Im getting a lot of black space in my image and I'm not entirely sure whats going on so I'll post a new question on this – GeoMonkey Apr 06 '17 at 21:51
  • I posted this: http://stackoverflow.com/questions/43266623/black-space-in-glcm-image - if you are able to take a look – GeoMonkey Apr 06 '17 at 22:19