MATLAB: Why is graycomatrix output zeros

glcmgraycomatrixImage Processing Toolbox

I am using graycomatrix to generate glcm on a gray image I. The array is of type double. max(max(I)) = 255 and min(min(I)) = 0
if I do histogram on the image with 8 bins h = h.histogram(I, 8) h.Values = 900094 59443 20835 8553 3528 1667 1140 932 these shows that pixel values are not just 0 and 255 rather there are enough pixels that have values between 0 and 255
yet the glcm = graycomatrix(I) produces this glcm =
684391 0 0 0 0 0 0 12927
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0
13342 0 0 0 0 0 0 284668
Why all entries in glcm except the corner 4 are 0s? I expect many entries in the glcm to be non zero.

Best Answer

The GLCM says what gray levels are NEXT TO other gray levels. For a double image, it expects values in the range 0-1, NOT 0-255. So all your values more than 1 are essentially considered as being 1. To fix, cast it back to uint8, or else cast your double to the range 0-1 with mat2gray() or im2double().