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.
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.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.
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?
Cheers,from skimage.util import dtypedtype.log.disabled = True
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