GLCM with scikit-image

Neil yager.neil at gmail.com
Mon Jul 8 03:56:23 EDT 2013


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

On Sunday, 7 July 2013 00:42:38 UTC+1, ely... at mail.com wrote:
>
> 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.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20130708/6288daef/attachment.html>


More information about the scikit-image mailing list