back to iatexture

graycomatrix

C functions

- glcm2d - glcm3d

2D offsets

  • offset = [0 D] -> zero degrees, distance D
  • offset = [D 0] -> 90 degrees, distance D
  • offset = [D D] -> 135 degrees, distance D
  • offset = [D -D] -> 45 degrees, distance D

3D offsets

-standard 2D directions:

[0 1 0] 0 degrees
[0 1 -1] 45 degrees
[0 1 0] 90 degrees
[0 1 1] 135 degrees
  • additional 9 directions
[1 0 -1] 0 degrees 45 degrees
[1 0 0] straight up  
[1 0 1] 0 degree 135 degrees
[1 1 0] 90 degrees 45 degrees
[1 -1 0] 90 degrees 135 degrees
[1 1 -1] 45 degrees 45 degrees
[1 -1 1] 45 degrees 135 degrees
[1 1 1] 135 degrees 45 degrees
[1 -1 -1] 135 degrees 135 degrees

Function code

01. from iatexture.glcm3d import glcm3d_c
02. from iatexture.glcm2d import glcm2d_c
03. import numpy as np
04. 
05. def glcm(levels,offsets,img):
06.    result = np.zeros((levels,levels), dtype = np.int32)
07.    if len(img.shape) ==2:
08.        if offsets == []:
09.            offsets = np.array([0,1], dtype = np.int32)
10.        glcm2d_c(levels, img, offsets, result)
11.    elif len(img.shape) ==3:
12.        if offsets == []:
13.            offsets = np.array([0,0,1], dtype = np.int32)
14.        glcm3d_c(levels, img, offsets, result)
15.    else:
16.        print 'Invalid input. Please use another array'
17.    return result

Examples

2D examples

1. import numpy as np
2. from iatexture.graycomatrix import glcm
3. 
4. #levels = 3
5. arr = np.array([[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]], dtype = np.uint8)
6. levels = int(arr.max()+1)
01. offset = np.array([0,1], dtype = np.int32) # orientation 0 degrees
02. result = glcm(levels, offset, arr)
03. print result
04. print
05. offset = np.array([1,-1], dtype = np.int32) # orientation 45 degrees
06. result = glcm(levels, offset, arr)
07. print result
08. print
09. offset = np.array([1,0], dtype = np.int32) # orientation 90 degrees
10. result = glcm(levels, offset, arr)
11. print result
12. print
13. offset = np.array([1,1], dtype = np.int32) # orientation 135 degrees
14. result = glcm(levels, offset, arr)
15. print result
16. print
[[5 2 1]
 [1 4 4]
 [2 1 5]]

[[0 4 2]
 [1 4 1]
 [5 0 3]]

[[0 3 4]
 [2 5 2]
 [5 1 2]]

[[1 2 4]
 [2 3 2]
 [3 1 2]]

3D examples

1. import numpy as np
2. 
3. arr = zeros((3,5,6), dtype=np.uint8)
4. #arr = np.array([[[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]],
5. #       [[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]]], dtype=np.uint8)
6. arr = np.array([[[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]],
7.        [[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]],
8.        [[1,2,2,0,0,1],[0,0,1,2,2,1],[1,1,0,0,0,2],[1,1,1,2,2,2],[1,1,2,2,0,0]]], dtype=np.uint8)
9. levels = int(arr.max()+1)

Standard directions

01. offset = np.array([0,0,1], dtype = np.int32) # orientation 0 degrees (example same slices: equal to Nslices*0degree 2D case)
02. result = glcm(levels, [], arr)
03. print result
04. print
05. offset = np.array([0,1,-1], dtype = np.int32) # orientation 45 degrees (example same slices: equal to Nslices*45degree 2D case)
06. result = glcm(levels, offset, arr)
07. print result
08. print
09. offset = np.array([0,1,0], dtype = np.int32) # orientation 90 degrees (example same slices: equal to Nslices*90degree 2D case)
10. result = glcm(levels, offset, arr)
11. print result
12. print
13. offset = np.array([0,1,1], dtype = np.int32) # orientation 135 degrees (example same slices: equal to Nslices*135degree 2D case)
14. result = glcm(levels, offset, arr)
15. print result
16. print
[[15  6  3]
 [ 3 12 12]
 [ 6  3 15]]

[[ 0 12  6]
 [ 3 12  3]
 [15  0  9]]

[[ 0  9 12]
 [ 6 15  6]
 [15  3  6]]

[[ 3  6 12]
 [ 6  9  6]
 [ 9  3  6]]

Addiction 9 directions

01. offset = np.array([1,0,-1], dtype = np.int32) # 0 degrees/45 degrees (example same slices: equal to (Nslices-1)*0degree 2D case)
02. result = glcm(levels, offset, arr)
03. print result
04. print
05. offset = np.array([1,0,0], dtype = np.int32) # straight up (example same slices: equal to np.unique())
06. result = glcm(levels, offset, arr)
07. print result
08. print
09. offset = np.array([1,0,1], dtype = np.int32) # 0 degree/135 degrees (example same slices: equal to (Nslices-1)*transpose of 0degree 2D case)
10. result = glcm(levels, offset, arr)
11. print result
12. print
13. offset = np.array([1,1,0], dtype = np.int32) # 90 degrees/45 degrees (example same slices: equal to (Nslices-1)*90 degree 2D case)
14. result = glcm(levels, offset, arr)
15. print result
16. print
17. 
18. offset = np.array([1,-1,0], dtype = np.int32) # 90 degrees/135 degrees (example same slices: equal to (Nslices-1)*transpose of 90 degree 2D case)
19. result = glcm(levels, offset, arr)
20. print result
21. print
22. offset = np.array([1,1,-1], dtype = np.int32) # 45 degrees/45 degrees (example same slices: equal to (Nslices-1)*45 degree 2D case)
23. result = glcm(levels, offset, arr)
24. print result
25. print
26. offset = np.array([1,-1,1], dtype = np.int32) # 45 degree/135 degrees (example same slices: equal to (Nslices-1)*transpose of 45 degree 2D case)
27. result = glcm(levels, offset, arr)
28. print result
29. print
30. offset = np.array([1,1,1], dtype = np.int32) # 135 degrees/45 degrees (example same slices: equal to (Nslices-1)*135 degree 2D case)
31. result = glcm(levels, offset, arr)
32. print result
33. print
34. offset = np.array([1,-1,-1], dtype = np.int32) # 135 degrees/135 degrees (example same slices: equal to (Nslices-1)*transpose of 135 degree 2D case)
35. result = glcm(levels, offset, arr)
36. print result
37. print
[[10  2  4]
 [ 4  8  2]
 [ 2  8 10]]

[[18  0  0]
 [ 0 22  0]
 [ 0  0 20]]

[[10  4  2]
 [ 2  8  8]
 [ 4  2 10]]

[[ 0  6  8]
 [ 4 10  4]
 [10  2  4]]

[[ 0  4 10]
 [ 6 10  2]
 [ 8  4  4]]

[[ 0  8  4]
 [ 2  8  2]
 [10  0  6]]

[[ 0  2 10]
 [ 8  8  0]
 [ 4  2  6]]

[[2 4 8]
 [4 6 4]
 [6 2 4]]

[[2 4 6]
 [4 6 2]
 [8 4 4]]