1

I'm trying to calculate various image features from the Gray-Level Co-occurence Matrix (GLCM) in MatLab using the graycomatrix function.

Example using graycomatrix

I = [0 0 1 1; 0 0 1 1; 0 2 2 2; 2 2 3 3];
glcm = graycomatrix(I, 'GrayLimits', [0 3], 'NumLevels', 4, 'Symmetric', true);

How can I apply this function to a series of sub-windows assuming a larger image (640x480 for example) than the trivial example and a sliding window size of 5x5?

Tonechas
  • 13,398
  • 16
  • 46
  • 80
user3851917
  • 41
  • 1
  • 5
  • Please provide some more information than: please help I don't know how. Did you read the discription and the help files in the link at http://www.mathworks.com/matlabcentral/fileexchange/22354-glcm-features4-m--vectorized-version-of-glcm-features1-m--with-code-changes-/content/GLCM_Features1.m? – EJG89 Jul 18 '14 at 07:41
  • yes I've read this article ,I want to apply this function on each moving window of image for all of pixels. – user3851917 Jul 18 '14 at 08:58
  • I have an 319x453 image that i want to extract GLCM featuresfrom this ,my sub-window is [5 5] ,it's a moving window that move pixel by pixel and calculate these features for each window that is 2 neighborhood of everey pixel and these features will be collect for each pixel – user3851917 Jul 18 '14 at 09:12
  • @EJG89 I've improved the question to address some people's desire to close it. Could you please vote to reopen? I would like to answer this question without starting a new one. – Cameron Lowell Palmer Sep 13 '15 at 15:35

2 Answers2

1

I'm not sure a filtering approach is possible. I think you mean sth like this (let im be your grayscale image):

for ii=3:size(im,1)-2
    for jj=3:size(im,2)-2  % a loop for every pixel that a 5x5 window can be defined
        temp_im=im(ii-2:ii+2,jj-2:jj+2);  % crop a small window around pixel
        glcm=graycomatrix(temp_im);  % default parameters
        glcm_feat_struct=graycoprops(glcm);
        % extract features from struct and do anything you want
    end
end
fyts
  • 493
  • 5
  • 12
  • 1
    Can I use the nlfilter function? – user3851917 Jul 19 '14 at 07:49
  • In your case nlfilter cannot be used, because the inner function must return a scalar. Have a look at [blockproc](http://www.mathworks.com/help/images/ref/blockproc.html). Not having used it, I cannot provide an example, but it seems to be the most appropriate choice. – fyts Jul 19 '14 at 10:14
  • How can i save the feature vector for each pixel by these loop? – user3851917 Aug 02 '14 at 10:55
  • Let's say your image is M by N pixels and you want to extract 4 features, as provided by graycoprops (Contrast etc). Then, you can store the features for every pixel in a matrix, let's say: 'feat=zeros(M,N,4); % create it before the double loop' and then, replace the comment inside the loop "extract features..." with: 'feat(ii,jj,1)=glcm_feat_struct.Contrast;' 'feat(ii,jj,2)=glcm_feat_struct.Correlation;' and so on. – fyts Aug 13 '14 at 07:48
0

Using nlfilter

I found this particular formulation and experimented with it. It has some limitations, but it isn't a terrible starting point.

function [ s ] = glcm_contrast(NHOOD)
%GLCM_CONTRAST
    glcm = graycomatrix(NHOOD, 'Offset', [0 1], 'GrayLimits', [0 3], 'NumLevels', 4, 'Symmetric', true);
    stats = graycoprops(glcm, 'contrast');
    s = stats.Contrast;
end

window_size = 5;
I = imread('yourImage.tif');
glcmfunc = @(x) glcm_contrast(x);
gray_limits = [min(I(:)), max(I(:))];
B = nlfilter(I, [window_size, window_size], glcmfunc);

Custom Matlab code

You might also want to take a look at my implementation of Sliding GLCM and see if you might want to expand upon that.

Cameron Lowell Palmer
  • 21,528
  • 7
  • 125
  • 126