I'm sure this is a very simple mistake by me somewhere! But when I use Matlab's graycomatrix
function, I don't get the expected result. Instead of a matrix output I expect, I always get an 8 x 8 (nearly) zero matrix with one entry in the bottom right - usually equal to 16. I haven't changed the default settings or used 'offset', so I'm not too sure what the problem is.
Asked
Active
Viewed 1,258 times
0

Tonechas
- 13,398
- 16
- 46
- 80

Mike Miller
- 253
- 6
- 18
-
How are you calling it, and what is the type (e.g. double) and range of values in your input matrix? – nkjt Feb 17 '15 at 12:42
-
@nkjt Tried it on a double image and simple example e.g. I = [1 1 2; 2 2 3; 1 2 5]; glcm = graycomatrix(I); – Mike Miller Feb 17 '15 at 12:45
-
2ah, I see Ander got there before me, but yes, scaling. It's almost always scaling. – nkjt Feb 17 '15 at 13:04
1 Answers
4
That's because your image is not normalized!
Your image should be range 0-1, so:
I = [1 1 2; 2 2 3; 1 2 5]; %or any other I
glcm = graycomatrix(I/max(I(:))); % or I/255 , but it would not work for this example
should do the job.
In your case, Matlab interprets that everything avobe 1 is 1, therefore the co-occurrence matrix gives you a unique value in the max position.

Ander Biguri
- 35,140
- 11
- 74
- 120
-
-
2In the case of `graycomatrix` you can also use `'G',[]` as an option to automatically scale. It's much like doing `imshow(img,[])` – nkjt Feb 17 '15 at 13:06
-
@nkjt ander Should the resultant matrix always be 8 x 8? From what I understood it should be as large as the amount of grey levels in your image. For example, I've just tried this on my image, But I now have an 8 x 8 matrix with 1139783 as entry (1,1) and zero everywhere else. That's not the way I understood this to work! – Mike Miller Feb 17 '15 at 13:11
-
1I suggest you read the help file for `graycomatrix` more carefully. The default number of levels is 8, therefore the output is 8 x 8 (all combinations of those levels, presuming no offsets). It is 8 as long as your input is numeric (not binary/logical), unless you set it to be something else. – nkjt Feb 17 '15 at 13:16
-
@nkjt How would you interpret such a small matrix, say if you had 256 grey levels though? Or would it be more advisable to set the number of levels to your number of grey levels so you can see relationships more clearly? Thanks for your help by the way, I'd +1 if I could. – Mike Miller Feb 17 '15 at 13:58
-
If you have something like 256 grey levels, you will most likely want to increase the size of your graycomatrix using the 'NumLevels' argument. Unless you have a strong meaning assigned to the different gray levels, a good rule of thumb is the square root of the number of gray levels (16 in this case)... setting the number of levels to 256 would create a co-occurence matrix that is very noisy and difficult to interpret. – Jim Quirk Feb 17 '15 at 15:51