GLCM with scikit-image

4,441 views
Skip to first unread message

ely...@mail.com

unread,
Jul 6, 2013, 7:42:38 PM7/6/13
to scikit...@googlegroups.com

Hi all,

 

I was using until now Matlab and its about time for me to move to scikit-image as it provide me more flexibility and a lot of benefits.

 

Normally, I used Matlab to calculate the properties of gray-level co-occurrence matrix as shown in this link (http://www.mathworks.co.uk/help/images/ref/graycoprops.html) mainly with this simple script:

 

clc;

 Img = imread('C:\Users\dell\Desktop\ImgTemp\python.jpg');

I=rgb2gray(Img);

% Photo downloaded from “http://i425.photobucket.com/albums/pp337/jewarmy/python.jpg

GLCM2 = graycomatrix(I);

allst = graycoprops(GLCM2,'all');

 

contrastInfo = allst.Contrast;

display(contrastInfo)

energyInfo =  allst.Energy;

display(energyInfo)

homogeneityInfo = allst.Homogeneity;

display(homogeneityInfo)

correlationInfo = allst.Correlation;

display(correlationInfo)

 

With the following output:

contrastInfo =

    0.2516

energyInfo =

    0.1094

homogeneityInfo =

    0.8959

 correlationInfo =

    0.9672

 

 

While I was trying to do it with scikit-image using this script:

 

import numpy as np

from skimage.io import imread

from skimage.feature import greycomatrix, greycoprops

 

image=imread('C:/Users/dell/Desktop/ImgTemp/python.jpg', as_grey=True)

g = greycomatrix(image, [0, 1], [0, np.pi/2], levels=256)

 

contrast = greycoprops(g, 'contrast')

print('contrast is: ',  contrast)

 

energy = greycoprops(g, 'energy')

print('energy is: ',  energy)

 

homogeneity = greycoprops(g, 'homogeneity')

print('homogeneity is: ',  homogeneity)

 

correlation = greycoprops(g, 'correlation')

print('correlation is: ',  correlation)

 

dissimilarity = greycoprops(g, 'dissimilarity')

print('dissimilarity is: ',  dissimilarity)

 

ASM = greycoprops(g, 'ASM')

print('ASM is: ',  ASM)

 

I get these results:

 

contrast is:  [[0 0]

 [0 0]]

energy is:  [[ 40007.37212065  40007.37212065]

 [ 38525.88698525  38017.06358992]]

homogeneity is:  [[ 165440.  165440.]

 [ 165088.  164970.]]

correlation is:  [[ 1.  1.]

 [ 1.  1.]]

dissimilarity is:  [[0 0]

 [0 0]]

ASM is:  [[1600589824 1600589824]

 [1484243968 1445297124]]

 

I do not understand why there are differences and for sure I miss something. Can someone please explain me what is wrong (I am using python 3.2 and scikit-image 0.8.3).

 

Thanks a lot in advance.

Neil

unread,
Jul 8, 2013, 3:56:23 AM7/8/13
to scikit...@googlegroups.com
I don't have matlab, so I can't say for sure what is going on. I can see a number of potential reasons for discrepancy. 

1. There may be a slight difference between the RGB to grayscale conversion formulas used by matlab and skimage. It you want identical results, you'll have to confirm this.
2. Based on the matlab documentation, it looks like the image is quantized based on the NumLevels parameter. The default is 8. Once again, in order to get the same results, you'll need to make sure this scaling is the same.
3. It looks like matlab normalizes the GLCM matrix (although the docs don't say).
4. By default, it looks like matlab only computes the GLCM for one offset. Also, there is a difference between the way the offsets are specified: skimage defines the offset by distance & angle, while matlab specifies it by row and column offset. 

The following doesn't give the same answer at matlab, but they are in the right ballpark:

import skimage.io
import skimage.feature
im = skimage.io.imread('python.jpg', as_grey=True)
im = skimage.img_as_ubyte(im)
im /= 32
g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True)
print skimage.feature.greycoprops(g, 'contrast')[0][0]
print skimage.feature.greycoprops(g, 'energy')[0][0]
print skimage.feature.greycoprops(g, 'homogeneity')[0][0]
print skimage.feature.greycoprops(g, 'correlation')[0][0]

with the results:

0.301505863539
0.29090192313
0.883493603794
0.971610338356

To dig deeper I would need matlab. However, hopefully this is enough to get you started. You might want to try some simple/small images, and look at the actual GLCM matrices.

Neil

Stéfan van der Walt

unread,
Jul 8, 2013, 5:55:35 AM7/8/13
to scikit-image
On Mon, Jul 8, 2013 at 9:56 AM, Neil <yager...@gmail.com> wrote:
> 1. There may be a slight difference between the RGB to grayscale conversion
> formulas used by matlab and skimage. It you want identical results, you'll
> have to confirm this.

That's true. The Matlab conversion is probably done as follows:

image = np.uint8(0.2989 * python[..., 0] + 0.5870 * python[..., 1] +
0.1140 * python[..., 2])

I'm not sure whether they round or discard the fraction.

> 3. It looks like matlab normalizes the GLCM matrix (although the docs don't
> say).

This should probably raise a warning in graycoprops--it doesn't make
much sense to calculate these without normalization.

Stéfan
Message has been deleted

Stéfan van der Walt

unread,
Jul 8, 2013, 8:53:52 AM7/8/13
to scikit-image
On Mon, Jul 8, 2013 at 2:45 PM, <ely...@mail.com> wrote:
> - Does the "skimage.dtype_converter: WARNING: Possible precision loss when
> converting from float64 to uint8" means that the output values are wrong?

Not wrong, just that there was a conversion between floating point and
integer values, which means it had to drop some precision along the
way.

Stéfan
Message has been deleted

Neil

unread,
Jul 8, 2013, 9:21:38 AM7/8/13
to scikit...@googlegroups.com
I wouldn't worry too much about the warning. Whenever you convert an image from floating point (64 bits per pixel) to bytes (8 bits per pixel), there may be a loss of precision.

The lines:

im = skimage.img_as_ubyte(im)
im /= 32

are just a quick way to scale the pixel intensities to have 8 different levels. The first step converts the image from floats to bytes (this is where you get the warning). We now have an image with 256 levels (from 0 to 255). After dividing by 32, there are only 8 possible levels (from 0 to 7). It is likely that matlab use a different procedure for converting from a floating point image to an image with only 8 levels.

The [0][0] is to specify which offset you want. The first 0 gives the index for the distances, and the second is the index for the angles. In this example, we are only computing one offset (distance=1 and angle=0). However, in many real-life applications you will want to compute the statistics for a number of offsets to capture differences in scale and orientation.

Neil

On Monday, 8 July 2013 14:00:11 UTC+1, ely...@mail.com wrote:
Hi,

Thanks a lot for the reply.

Indeed graycoprops normalizes the gray-level co-occurrence matrix:

“graycoprops normalizes the gray-level co-occurrence matrix (GLCM) so that the sum of its elements is equal to 1. Each element (r,c) in the normalized GLCM is the joint probability occurrence of pixel pairs with a defined spatial relationship having gray level values r and c in the image. graycoprops uses the normalized GLCM to calculate properties.” (http://www.mathworks.co.uk/help/images/ref/graycoprops.html)

The modified script:
import skimage

from skimage.io import imread
from skimage.feature import greycomatrix
from skimage.feature import greycoprops

im = imread('C:/Users/Asher_dell/Desktop/ImgTemp/python.jpg', as_grey=True)

im = skimage.img_as_ubyte(im)
im /= 32
g = skimage.feature.greycomatrix(im, [1], [0], levels=8, symmetric=False, normed=True)

contrast= skimage.feature.greycoprops(g, 'contrast')[0][0]
energy= skimage.feature.greycoprops(g, 'energy')[0][0]
homogeneity= skimage.feature.greycoprops(g, 'homogeneity')[0][0]
correlation=skimage.feature.greycoprops(g, 'correlation')[0][0]
dissimilarity=skimage.feature.greycoprops(g, 'dissimilarity')[0][0]
ASM=skimage.feature.greycoprops(g, 'ASM')[0][0]


print('contrast is: ', contrast)
print('energy is: ', energy)
print('homogeneity is: ', homogeneity)
print('correlation is: ', correlation)
print('dissimilarity is: ', dissimilarity)
print('ASM is: ', ASM)


Output:
skimage.dtype_converter: WARNING: Possible precision loss when converting from float64 to uint8
contrast is:  0.301542207792
energy is:  0.29069020973
homogeneity is:  0.883463991917
correlation is:  0.971624675221
dissimilarity is:  0.243464091878
ASM is:  0.0845007980331


Based on the output I have few more questions on the modified script:

- Does the "skimage.dtype_converter: WARNING: Possible precision loss when converting from float64 to uint8" means that the output values are wrong?
- Can you please explain me why you use these lines:

im = skimage.img_as_ubyte(im)
im /= 32

- Why do you use the [0][0] when you call skimage.feature.greycoprops (e.g.,skimage.feature.greycoprops(g, 'contrast')[0][0])?

Thanks a lot again.

ely...@mail.com

unread,
Jul 8, 2013, 10:15:10 AM7/8/13
to scikit...@googlegroups.com
Cheers and thanks a lot for the help.

wis...@walla.co.il

unread,
Jul 16, 2013, 2:49:11 AM7/16/13
to scikit...@googlegroups.com
Is there a way/command to avoiding printing out the warning message "skimage.dtype_converter: WARNING: Possible precision loss when converting from float64 to uint8" in the output?

wis...@walla.co.il

unread,
Jul 17, 2013, 5:56:24 AM7/17/13
to scikit...@googlegroups.com
How can I cancel the "skimage.dtype_converter: WARNING: Possible precision loss when converting from float64 to uint8" warning in the output?, is it possible?

Tony Yu

unread,
Jul 18, 2013, 10:12:00 AM7/18/13
to scikit...@googlegroups.com
On Wed, Jul 17, 2013 at 4:56 AM, <wis...@walla.co.il> wrote:
How can I cancel the "skimage.dtype_converter: WARNING: Possible precision loss when converting from float64 to uint8" warning in the output?, is it possible?


The warning messages are a bit annoying. We're discussing ways of making it less annoying here:


For now you can just disable that logger:

from skimage.util import dtype
dtype.log.disabled = True

Cheers,
-Tony

Message has been deleted

ely...@mail.com

unread,
Jun 5, 2015, 3:41:11 AM6/5/15
to scikit...@googlegroups.com

I  moved to python 3.4 and scikit_image‑0.11.3 amd when I am running a script the:

from skimage.util import dtype

dtype.log.disabled = True


I get this error:
Traceback (most recent call last):
  File "C:/Users/ZDesktop/untitled/d.py", line 16, in <module>
    dtype.log.disabled = True
AttributeError: 'module' object has no attribute 'log'

 

If I remove the “dtype.log.disabled = True”  script line I get this message:

C:\Python34\lib\site-packages\skimage\util\dtype.py:111: UserWarning: Possible precision loss when converting from float64 to uint8

  "%s to %s" % (dtypeobj_in, dtypeobj))

 

Therefore, how can I use the dtype and remove the warning message?

 

Thanks a lot

Stefan van der Walt

unread,
Jun 5, 2015, 2:41:03 PM6/5/15
to scikit...@googlegroups.com
On 2015-06-05 00:35:54, ely...@mail.com wrote:
> I moved to python 3.4 and scikit_image‑0.11.3 amnd when I am
> running a script the: from skimage.util import dtype
> dtype.log.disabled = True
>
> I get this error: Traceback (most recent call last):
> File "C:/Users/ZDesktop/untitled/d.py", line 16, in <module>
> dtype.log.disabled = True
> AttributeError: 'module' object has no attribute 'log'

The package has changed in the mean time--we no longer use a
logging mechanism and you can disable the warning using the
standing Python warnings module.

Stéfan
Reply all
Reply to author
Forward
0 new messages