How to roll a numpy 2D array to create grey level co-occurrence matrices, calculate dissimilarity

153 views
Skip to first unread message

Matteo

unread,
Dec 16, 2016, 1:13:20 PM12/16/16
to scikit-image
I am very new to GLCM.

I have some 1D array I would like to calculate dissimilarity in the vertical direction, so I am trying to trick scikit-image by converting my 1D array into 2D.

Say I have an 16x5 2D array. I make 5 copies of it to get a 16x5 array. I want to pass rows 0-4, then rows 5-9 and so on to run greycomatrix, by just lookign at the vertical direction,  append to output, and ultimately calculate dissimilarity.

Example array:

    x =np.array([7, 2, 0, 8, 8, 3, 2, 2, 0, 5, 3, 3, 4, 8, 7, 0])
    w
=5
    s
= np.array([x,]*w,dtype=np.uint8).transpose()


array
([
 
[7, 7, 7, 7, 7],
 
[2, 2, 2, 2, 2],
 
[0, 0, 0, 0, 0],
 
[8, 8, 8, 8, 8],
 
[8, 8, 8, 8, 8],
 
[3, 3, 3, 3, 3],
 
[2, 2, 2, 2, 2],
 
[2, 2, 2, 2, 2],
 
[0, 0, 0, 0, 0],
 
[5, 5, 5, 5, 5],
 
[3, 3, 3, 3, 3],
 
[3, 3, 3, 3, 3],
 
[4, 4, 4, 4, 4],
 
[8, 8, 8, 8, 8],
 
[7, 7, 7, 7, 7],
 
[0, 0, 0, 0, 0]], dtype=uint8)

Below I test rolling:

test = np.roll(s[:, :], -5)
test

array
([[2, 2, 2, 2, 2],
       
[0, 0, 0, 0, 0],
       
[8, 8, 8, 8, 8],
       
[8, 8, 8, 8, 8],
       
[3, 3, 3, 3, 3],
       
[2, 2, 2, 2, 2],
       
[2, 2, 2, 2, 2],
       
[0, 0, 0, 0, 0],
       
[5, 5, 5, 5, 5],
       
[3, 3, 3, 3, 3],
       
[3, 3, 3, 3, 3],
       
[4, 4, 4, 4, 4],
       
[8, 8, 8, 8, 8],
       
[7, 7, 7, 7, 7],
       
[0, 0, 0, 0, 0],
       
[7, 7, 7, 7, 7]], dtype=uint8)


Now I roll and pass top 5 rows to greycomatrix:

levels = 9# this rolls the 2D array up one row at a time, each time calculating GLCM on the top 5x5 elements
out = []
counter
= 0
while counter < np.shape(s)[0]:
 s
= np.roll(s[:, :], -5)
 glcm
= greycomatrix(s[:5,:], [1], [np.pi/2], levels = levels, symmetric = True, normed = True)
 
out.append(glcm)
 counter
+=1


But unfortunately output is a 5D array:

print np.shape(out)
(16, 9, 9, 1, 1)



I tried to recast into a 4D array to pass to greycoprops in different ways, but unsuccessfully.

With: 
out1 = np.array(out).reshape((16, 9, 9, 1))
np
.shape(out1)
(16, 9, 9, 1)

diss = greycoprops(out1, 'dissimilarity')

I get this error:

---------------------------------------------------------------------------
AssertionError Traceback (most recent call last)
<ipython-input-15-a88f05b0e3e2> in <module>()
----> 1 diss = greycoprops(out1, 'dissimilarity')
 
2 diss

/Users/matteoniccoli/anaconda2/lib/python2.7/site-packages/skimage/feature/texture.pyc in greycoprops(P, prop)
 
187
 
188 (num_level, num_level2, num_dist, num_angle) = P.shape
--> 189 assert num_level == num_level2
 
190 assert num_dist > 0
 
191 assert num_angle > 0

AssertionError:


It may be that I do not really understand the shape of the output, and the requirements of greycoprops.

print np.shape(np.array(out))
print np.shape(np.array(out[0][:][:][:][:]))
print np.shape(np.array(out[:][0][:][:][:]))
print np.shape(np.array(out[:][:][0][:][:]))
print np.shape(np.array(out[:][:][:][0][:]))
print np.shape(np.array(out[:][:][:][:][0]))


(16, 9, 9, 1, 1)
(9, 9, 1, 1)
(9, 9, 1, 1)
(9, 9, 1, 1)
(9, 9, 1, 1)
(9, 9, 1, 1)

Can anybody suggest a way to make this work?

Thanks

Matteo

unread,
Dec 16, 2016, 1:15:00 PM12/16/16
to scikit-image
Sorry, below I meant to write:"Say I have an 16x1 1D array. I make 5 copies of it to get a 16x5 2D array..."

Matteo

unread,
Dec 16, 2016, 10:11:20 PM12/16/16
to scikit-image
My apologies.
It is clear now that the issue was not in the idea for the implementation, or the code, but clearly my incomplete understanding of graycomatrix and graycoprops.
Since I am sliding along a 16x5 array, with 9 levels, I should get a 16x9x9x1x1 5D array. Now that I understand this better, it occurs to me that I just need to slide over this array also, passing each of the 16  9x9x1x1 4D partial array to greycopros, to get 16 values of dissimilarity. Does that make sense?

diss = []
for i in np.arange(np.shape(out)[0]):
    ds
= greycoprops(out[i], 'dissimilarity')
    diss
.append(ds)
diss  

will give:
[array([[ 3.75]]),
 array
([[ 3.5]]),
 array
([[ 1.5]]),
 array
([[ 2.]]),
 array
([[ 2.]]),
 array
([[ 2.25]]),
 array
([[ 2.25]]),
 array
([[ 2.]]),
 array
([[ 1.75]]),
 array
([[ 1.5]]),
 array
([[ 3.25]]),
 array
([[ 4.75]]),
 array
([[ 5.]]),
 array
([[ 5.25]]),
 array
([[ 5.5]]),
 array
([[ 3.75]])]

Thank you
Reply all
Reply to author
Forward
0 new messages