How can I get the elements (r,c) in the GLCM?

2 views (last 30 days)
Edgar Corzo
Edgar Corzo on 9 Mar 2014
Commented: Edgar Corzo on 10 Mar 2014
I am developing my thesis for my master degree and I am working with images in gray-levels
So, I know that using graycoprops I can get the properties of the GLCM.
"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". stats = graycoprops(glcm, properties)
I need to get for every property the elements (r,c) of the GLCM. As an output I have to get a list value for each property respect each gray-level.
I got a code from the web that in theory "do" the mentioned before, but this code was wrote in Matlab R14 , and when I try to run this code in matlab 7.8 R2009a , I get an error.
  • ??? Undefined function or method 'ParseInputs' for input arguments of type 'cell'. *
Following a part of the code:
function stats = GraycoProps(varargin) %%A lot of comments
allStats = {'Contrast','Correlation','Energy','Homogeneity'};
[glcm, requestedStats] = ParseInputs(allStats, varargin{:}); %THE ERROR IS HERE**
% Initialize output stats structure.
numStats = length(requestedStats);
numGLCM = size(glcm,3);
empties = repmat({zeros(1,numGLCM)},[numStats 1]);
stats = cell2struct(empties,requestedStats,1);
for p = 1 : numGLCM
if numGLCM ~= 1 %N-D indexing not allowed for sparse.
tGLCM = normalizeGLCM(glcm(:,:,p));
else
tGLCM = normalizeGLCM(glcm);
end
% Get row and column subscripts of GLCM. These subscripts correspond to the
% pixel values in the GLCM.
s = size(tGLCM);
[c,r] = meshgrid(1:s(1),1:s(2));
r = r(:);
c = c(:);
% Calculate fields of output stats structure.
for k = 1:numStats
name = requestedStats{k};
switch name
case 'Contrast'
stats.(name)(p) = calculateContrast(tGLCM,r,c);
case 'Correlation'
stats.(name)(p) = calculateCorrelation(tGLCM,r,c);
case 'Energy'
stats.(name)(p) = calculateEnergy(tGLCM);
case 'Homogeneity'
stats.(name)(p) = calculateHomogeneity(tGLCM,r,c);
end
end
end
  • How can I rewrite this code in my matlab version? *
  1 Comment
Edgar Corzo
Edgar Corzo on 10 Mar 2014
Of course! I called graycomatrix as you can see below...
However, I got the same error.
function stats = GraycoProps(varargin) %%A lot of comments
* *I = imread('I:\pos\TESIS\...\PSM2-74538-0.038.TIF');
GLCM = graycomatrix(I,'G',[1 256],'Numlevels',256,'Offset',[0 1;-1 1;-1 0;-1 -1]);* * *
allStats = {'Contrast','Correlation','Energy','Homogeneity'};
[glcm, requestedStats] = ParseInputs(allStats, varargin{:}); %THE ERROR IS HERE**
% Initialize output stats structure.
numStats = length(requestedStats);
numGLCM = size(glcm,3);
empties = repmat({zeros(1,numGLCM)},[numStats 1]);
stats = cell2struct(empties,requestedStats,1); ... ...
To explain me better, for my output I need to get a kind of file, may be .txt or .xls, with the following information:
gray-levels 'Contrast' 'Correlation' 'Energy' 'Homogeneity'
0 ValueCon(0) ValueCor(0) ValueEne(0) ValueHom(0)
1
2
...
255 ValueCon(255) ValueCor(255) ValueEne(255) ValueHom(255)
In order to generate a plot (X,Y,Z), for example contrast vs correlation vs gray-level.
Thank you in advanced.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 10 Mar 2014
You need to call graycomatrix() to get the glcm matrix. See attached demo, below in blue text.
  1 Comment
Edgar Corzo
Edgar Corzo on 10 Mar 2014
Thank you for your response
Of course! I called graycomatrix as you can see below...
However, I got the same error.
function stats = GraycoProps(varargin) %%A lot of comments
* *I = imread('I:\pos\TESIS\...\PSM2-74538-0.038.TIF');
GLCM = graycomatrix(I,'G',[1 256],'Numlevels',256,'Offset',[0 1;-1 1;-1 0;-1 -1]);* * *
allStats = {'Contrast','Correlation','Energy','Homogeneity'};
[glcm, requestedStats] = ParseInputs(allStats, varargin{:}); %THE ERROR IS HERE**
% Initialize output stats structure.
numStats = length(requestedStats);
numGLCM = size(glcm,3);
empties = repmat({zeros(1,numGLCM)},[numStats 1]);
stats = cell2struct(empties,requestedStats,1);
for p = 1 : numGLCM
if numGLCM ~= 1 %N-D indexing not allowed for sparse.
tGLCM = normalizeGLCM(glcm(:,:,p));
else
tGLCM = normalizeGLCM(glcm);
end
% Get row and column subscripts of GLCM. These subscripts correspond to the
% pixel values in the GLCM.
s = size(tGLCM);
[c,r] = meshgrid(1:s(1),1:s(2));
r = r(:);
c = c(:);
% Calculate fields of output stats structure.
for k = 1:numStats
name = requestedStats{k};
switch name
case 'Contrast'
stats.(name)(p) = calculateContrast(tGLCM,r,c);
case 'Correlation'
stats.(name)(p) = calculateCorrelation(tGLCM,r,c);
case 'Energy'
stats.(name)(p) = calculateEnergy(tGLCM);
case 'Homogeneity'
stats.(name)(p) = calculateHomogeneity(tGLCM,r,c);
end
end
end
To explain me better, for my output I need to get a kind of file, may be .txt or .xls, with the following information:
gray-levels 'Contrast' 'Correlation' 'Energy' 'Homogeneity'
0 ValueCon(0) ValueCor(0) ValueEne(0) ValueHom(0)
1
2
...
255 ValueCon(255) ValueCor(255) ValueEne(255) ValueHom(255)
In order to generate a plot (X,Y,Z), for example contrast vs correlation vs gray-level.
Thank you in advanced.

Sign in to comment.


Edgar Corzo
Edgar Corzo on 10 Mar 2014
Thank you for your response
of course! I called graycomatrix as you can see below...
However, I got the same error.
function stats = GraycoProps(varargin) %%A lot of comments
  • * *I = imread('I:\pos\TESIS\...\PSM2-74538-0.038.TIF');
GLCM = graycomatrix(I,'G',[1 256],'Numlevels',256,'Offset',[0 1;-1 1;-1 0;-1 -1]);* * *
allStats = {'Contrast','Correlation','Energy','Homogeneity'};
[glcm, requestedStats] = ParseInputs(allStats, varargin{:}); %THE ERROR IS HERE**
% Initialize output stats structure.
numStats = length(requestedStats);
numGLCM = size(glcm,3);
empties = repmat({zeros(1,numGLCM)},[numStats 1]);
stats = cell2struct(empties,requestedStats,1);
for p = 1 : numGLCM
if numGLCM ~= 1 %N-D indexing not allowed for sparse.
tGLCM = normalizeGLCM(glcm(:,:,p));
else
tGLCM = normalizeGLCM(glcm);
end
% Get row and column subscripts of GLCM. These subscripts correspond to the
% pixel values in the GLCM.
s = size(tGLCM);
[c,r] = meshgrid(1:s(1),1:s(2));
r = r(:);
c = c(:);
% Calculate fields of output stats structure.
for k = 1:numStats
name = requestedStats{k};
switch name
case 'Contrast'
stats.(name)(p) = calculateContrast(tGLCM,r,c);
case 'Correlation'
stats.(name)(p) = calculateCorrelation(tGLCM,r,c);
case 'Energy'
stats.(name)(p) = calculateEnergy(tGLCM);
case 'Homogeneity'
stats.(name)(p) = calculateHomogeneity(tGLCM,r,c);
end
end
end
To explain me better, for my output I need to get a kind of file, may be .txt or .xls, with the following information:
gray-levels 'Contrast' 'Correlation' 'Energy' 'Homogeneity'
0 ValueCon(1) ValueCor(1) ValueEne(1) ValueHom(1)
1
2
...
255 ValueCon(255) ValueCor(255) ValueEne(255) ValueHom(255)
In order to generate a plot (X,Y,Z), for example contrast vs correlation vs gray-level.
Thank you in advanced.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!