简体   繁体   中英

Gray Level CoOccurence Features Do Not Match Equations

Consider a simple glcm matrix.

glcm = [0 1 2 3;1 1 2 3;1 0 2 0;0 0 0 3]; 

Calculate the statistics using Matlab's built in features.

stats = graycoprops(glcm)

   Contrast: 2.8947
Correlation: 0.0783
     Energy: 0.1191
Homogeneity: 0.5658

Now, instead, calculate them manually using the definition of these equations as given at the bottom of this page: https://www.mathworks.com/help/images/ref/graycoprops.html

contrast = 0;
energy = 0
for i = 1:4
    for j = 1:4
        contrast = contrast + glcm(i,j)*(i-j)^2;
        energy = energy + glcm(i,j)^2;
    end
end

Which gives:

contrast =

   110


energy =

    43

What is the deal? Is some type of normalization being done? It is not simply dividing by the number of elements which is 16...

Any thoughts? Thanks!

As I told you already in my comment , the documentation for graycoprops clearly states that:

graycoprops normalizes the gray-level co-occurrence matrix (GLCM) so that the sum of its elements is equal to 1.

But if you want to find the inner workings of this function, the best thing you can do is delve into the function code itself and find out how it does what it does. This is done best by doing a step-by-step debugging of the code.

If you type edit graycoprops in the Command Window, you can access the source code of the function.

If you do that, you will be able to see that around line 84 there is an if statement that calls another function called normalizeGLCM . This last function lives on the same file, so if you scroll down, around line 119 you can find how it works:

function glcm = normalizeGLCM(glcm)

% Normalize glcm so that sum(glcm(:)) is one.
if any(glcm(:))
  glcm = glcm ./ sum(glcm(:));
end

So, essentially, if you add the above normalization to your code, it will generate the same results as the graycoprops function:

glcm = [0 1 2 3;1 1 2 3;1 0 2 0;0 0 0 3]; 

stats = graycoprops(glcm)

glcm_norm = glcm ./ sum(glcm(:)); % <-- Normalize.

contrast = 0;
energy = 0;
for i = 1:4
    for j = 1:4
        contrast = contrast + glcm_norm(i,j)*(i-j)^2;
        energy = energy + glcm_norm(i,j)^2;
    end
end

Compare the results of graycoprops :

>> stats

stats = 

  struct with fields:

       Contrast: 2.8947
    Correlation: 0.0783
         Energy: 0.1191
    Homogeneity: 0.5658

With your results:

>> contrast

contrast =

    2.8947

>> energy

energy =

    0.1191

They match perfectly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM