1

I have a function show below glcmcontrast which is applied to an image in a sliding window operation using nlfilter.

function s = glcmcontrast(subI)
glcm = graycomatrix(subI,'Offset',[0 1],'NumLevels',64,'Symmetric',true);
stats = graycoprops(glcm,'contrast');
s=stats.Contrast;

This returns a scalar value for each sub image (the window, as passed by nlfilter). To use this with nlfilter I have it as an anonymous function handle, and its usage is shown below.

glcmanon = @(x)glcmcontrast(x);
tic; B = nlfilter(image,[3 3],glcmanon); toc;

This code quickly becomes very slow for images over 1024 x 1024. It is noted in the documentation that colfilt can be used with the 'sliding' argument to perform the same operations. My question is how I would use the function glcmcontrast with colfilt. Running it returns the error:

tic; B = colfilt(A,[3 3],'sliding',glcmcontrast); toc;
'Error using reshape
To RESHAPE the number of elements must not change.

Error in colfilt (line 183)
                b(i*mb+brows,j*nb+bcols) = ...'

I know from the documentation that colfilt reshapes the array using im2col into an (m*n)-by-((i-m+1)*(j-n+1)) where the window size is [m n] and the image size is [i j]. The question is how do I rewrite my glcmcontrast function into a format that works with colfilt? The line from colfilt that actually applies the function to the reshaped array is

b = reshape(feval(fun,x,params{:}), size(a));

but I am completely lost as to how to apply my function to this. I think the problem something to do with my function outputting a scalar value, but I'm not sure..

Tonechas
  • 13,398
  • 16
  • 46
  • 80
MBL
  • 1,197
  • 2
  • 11
  • 17
  • It may be off topic, but did you check the [vectorized version of GLCM](http://www.mathworks.com/matlabcentral/fileexchange/22354-glcmfeatures4-m-vectorized-version-of-glcmfeatures1-m-with-code-changes)? – zenpoy Mar 12 '12 at 16:29
  • Yes I have. The actual calculation of the GLCM using the matlab function [graycomatrix](http://www.mathworks.co.uk/help/toolbox/images/ref/graycomatrix.html) is the point at which my code slows down. The code you linked to is for calculating features from the GLCM, after the GLCM has been calculated using graycomatrix. – MBL Mar 13 '12 at 12:35

1 Answers1

0

According to the documentation of colfilt:

'sliding' Rearranges each m-by-n sliding neighborhood of A into a column in a temporary matrix, and then applies the function fun to this matrix. fun must return a row vector containing a single value for each column in the temporary matrix. (Column compression functions such as sum return the appropriate type of output.) colfilt then rearranges the vector returned by fun into a matrix the same size as A.

So you should change glcmcontrast to be able to receive a m-by-n matrix 'A' and to return 1-by-n row where the i'th cell is the output of the glcmcontrast on the i'th column in A. Similar to the way sum , mean and median functions work.

Not all functions can be rewritten to support this kind of operations.

zenpoy
  • 19,490
  • 9
  • 60
  • 87
  • I think, due to the nature of the way the GLCM is calculated, is is not possible to use the function in conjunction with colfilt, as the spatial orientation of the pixels needs to be retained. Thanks for the info though! – MBL Mar 13 '12 at 12:36