-2

I try to implement the GLCM method with the formula from wikipedia, but I have problems to fill my GLCM due to indices problems with matlab.

I have also used NitdepthQuantisation to reduce the number of Gray Levels, but for now I use the full 8 bit.

function [C] = GLCM(img, level, theta, delta)

 % Quantisation of the input Image to desired value
 imgQ = ImageQuantisation(img, level);
 [m n] = size(imgQ);

 % Get the number of gray levels
 maxGV = max(img(:));


 % Create GLCM initial Matrix
 C = zeros(maxGV, maxGV);


 % Positions
 delta_x = ceil(delta*cos(theta));
 delta_y = ceil(delta*sin(theta));

 %% Find Occurences
 for i = delta_x+1:m-delta_x
   for j = delta_y+1:n-delta_y
     if(imgQ(i, j) == imgQ(i+delta_x, j+delta_y))

        C(, ) = C(, ) + 1;
     end

   end
  end



end
Tonechas
  • 13,398
  • 16
  • 46
  • 80
Xeno1987
  • 75
  • 8

1 Answers1

0

The answer can be found by ensuring the inner nested double for loops have the correct indices to access the image. They were using the outer most pair of for loops for indices rather than the inner ones. The OP has commented that this has slight differences between what MATLAB gives to calculate the GLCM but it is good enough for the OP to overlook:

 for o = 1:maxGV
    for p = 1:maxGV
        if(imgQ(i, j) == o & imgQ(i+delta_x, j+delta_y) == p)
            C(o, p) = C(o, p) + 1;
        end
    end
 end
rayryeng
  • 102,964
  • 22
  • 184
  • 193