GLCM calculation using scikit-learn. Error when using greycomatrix

1,294 views
Skip to first unread message

ioannis...@gmail.com

unread,
Sep 17, 2016, 2:57:15 PM9/17/16
to scikit-image
Hello everyone,

I am using a SAR image (16-bit) and trying to implement GLCM algorithm using sciki-learn. When trying to calculate the GLCM using greycomatrix i get the following error:
assert image.max() < levels. It says that the maximum value of the image intensity must be less than the number of grey levels.
Because the SAR image is really big, i want to reduce the calculation time by reducing the levels to 8.
Even if i remove the parameter 'level=8' when using greycomatrix, still gives me the same error

My code is the following:

from skimage.feature import greycomatrix, greycoprops
import numpy as np
from skimage import data
import rasterio

path = 'C:\Users\GLCM_implementation\glasgow.tif'

with rasterio.open(path, 'r') as src:
import_file = src.read()
img = import_file[0,:,:] #i need only the two dimentions (height, width)
print img.shape


#calculate the GLCM specifying the distance, direction(4 directions) and number of grey levels
GLCM = greycomatrix(img, [1], [0, np.pi/4, np.pi/2, 3*np.pi/4],levels=8, symmetric=False, normed=True)
#list(GLCM[:,:,0,2])


#Calculate texture statistics
contrast = greycoprops(GLCM, 'contrast')

dissimilarity = greycoprops(GLCM, 'dissimilarity')

homogeneity = greycoprops(GLCM, 'homogeneity')

energy = greycoprops(GLCM, 'energy')

correlation = greycoprops(GLCM, 'correlation')

ASM = greycoprops(GLCM, 'ASM')



Error message:

101 image = np.ascontiguousarray(image) 102 assert image.min() >= 0 --> 103 assert image.max() < levels 104 image = image.astype(np.uint8) 105 distances = np.ascontiguousarray(distances, dtype=np.float64) AssertionError:


I would appreciate any help.
Thank you in advance

Ioannis

Juan Nunez-Iglesias

unread,
Sep 18, 2016, 8:03:45 PM9/18/16
to scikit...@googlegroups.com
Hi Ioannis,

Unfortunately the levels keyword is used as a hint to the function about the number of levels when the image is uint16, because the possible number of levels is huge. But if you want to convert the image to those levels, you have to do it manually. I suggest you look at the "rescale_intensity" function:


and process your image before passing it to the glcm function.

I hope this helps! Keep pinging if you have more questions. =)

Juan.

--
You received this message because you are subscribed to the Google Groups "scikit-image" group.
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image+unsubscribe@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/520f5f2b-4750-4b56-a40b-28b938b750d8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ioannis...@gmail.com

unread,
Sep 19, 2016, 10:41:07 AM9/19/16
to scikit-image
Thank you very much Juan for your quick reply.
That was helpful :)

Ioannis
To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image...@googlegroups.com.

ioannis...@gmail.com

unread,
Oct 1, 2016, 3:33:28 PM10/1/16
to scikit-image
Hello Juan,

I want to thank you for your quick reply to my question the last time and i do not want to bother you with my questions. I always try to solve my problems before posting my question here. Sometime, i just cannot figure out how to fix the error.

What i want to do is to create a GLCM image using different  grey levels (8-bit, 16-bit and 32-bit) and compare the results. The type of the image i am using is a float32. I used the '' rescale intensity'' as you suggested, i converted the image to 8-bit and it worked. When i try to rescale the image in 16-bit and 32-bit for some reason it doesn't work.

i get the error:


AssertionError                            Traceback (most recent call last)
<ipython-input-16-ca616e8222fb> in <module>()
     22 
     23     for j in xrange(img.shape[1] ):
---> 24         glcm = greycomatrix(rescale, [1], [0],  symmetric = True, normed = True )
     25 
     26         testraster1[i,j] = greycoprops(glcm, 'contrast')

C:\Anaconda2\lib\site-packages\skimage\feature\texture.pyc in greycomatrix(image, distances, angles, levels, symmetric, normed)

    101     image = np.ascontiguousarray(image)
    102     assert image.min() >= 0
--> 103     assert image.max() < levels

    104     image = image.astype(np.uint8)
    105     distances = np.ascontiguousarray(distances, dtype=np.float64)

AssertionError: 


It seems that i am allowed only to use an 8-bit image for the GLCM calculation.
So, does this code in scikit-image work only for 8-bit images? is there another way to solve my problem?
I will keep trying to find a solution but if you have an idea on how to solve this problem, please tell me


Thank you in advance
Ioannis

Juan Nunez-Iglesias

unread,
Oct 1, 2016, 10:41:10 PM10/1/16
to scikit...@googlegroups.com
Hi Ioannis,

So, the size of the GLCM for a 32-bit image that uses all possible intensity levels is 2**64, way too big to fit in memory. The key is:

- rescale the image to be in [0, max_value-1] (for some integer max_value that you determine)
- call greycomatrix on this rescaled image with the keyword argument `levels=max_value`.

That should work. However, note that the size of the matrix will be `4 * max_value ** 2` or `8 * max_value ** 2`, and multiple temporary matrices of the same size might be created for processing. So, you need to make sure that your computer has enough memory for that. Typically, this will require your image to have much fewer levels than even 16-bit.

Additionally, I think this feature requires the development version of scikit-image. You can install that with "pip install git+https://github.com/scikit-image/scikit-image.git". (I think that's the command anyway...)

Juan.

To unsubscribe from this group and stop receiving emails from it, send an email to scikit-image+unsubscribe@googlegroups.com.

To post to this group, send email to scikit...@googlegroups.com.

ioannis...@gmail.com

unread,
Oct 2, 2016, 4:40:43 AM10/2/16
to scikit-image

Thank you very much for your quick reply, i appreciate it. It seems you always have a good answer to give me.

Ioannis
Reply all
Reply to author
Forward
0 new messages