-1

I got the following snippet from GitHub to compute the gray level co-occurrence matrix (GLCM) through OpenCV:

float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
int row=img.rows,col=img.cols;
Mat gl=Mat::zeros(256,256,CV_32FC1);

//creating glcm matrix with 256 levels,radius=1 and in the horizontal direction 
for(int i=0;i<row;i++)
   for(int j=0;j<col-1;j++)
       gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))=gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))+1;   

// normalizing glcm matrix for parameter determination
gl=gl+gl.t();            
gl=gl/sum(gl)[0];

The code above is in C++. I need to convert this into Java but I'm stuck in this line:

gl.at<float>(img.at<uchar>(i,j),img.at<uchar>(i,j+1))=gl.at<‌​float>(img.at<uchar>‌​(i,j),img.at<uchar>(‌​i,j+1))+1; 

Can someone help me out with this?

Tonechas
  • 13,398
  • 16
  • 46
  • 80
AciD IoN
  • 159
  • 1
  • 1
  • 10

1 Answers1

3

The calculation of a 256x256 symmetric gray level co-occurrence matrix of image img (of class Mat) corresponding to an offset "one pixel to the right" may be implemented in Java through OpenCV as follows:

Mat gl = Mat.zeros(256, 256, CvType.CV_64F);
Mat glt = gl.clone();

for (int y = 0; y < img.rows(); y++) {
    for (int x = 0; x < img.cols()-1; x++) {

        int i = (int) img.get(y, x)[0];
        int j = (int) img.get(y, x + 1)[0];

        double[] count = gl.get(i, j);
        count[0]++;
        gl.put(i, j, count);
    }
}

Core.transpose(gl, glt);
Core.add(gl, glt, gl);
Scalar sum = Core.sumElems(gl);
Core.divide(gl, sum, gl);

There is a good bunch of publicly available libraries to compute GLCMs and extract Haralick features from them in Java, for example GLCM2, JFeatureLib, etc.

Tonechas
  • 13,398
  • 16
  • 46
  • 80
  • gl = gl + gl.t(); gl = gl/Core.sumElems(gl)[0]; sir these two line gives me error can you plz help me the error is 1st line: The operator + is undefined for the argument type(s) org.opencv.core.Mat, org.opencv.core.Mat 2nd line: The type of the expression must be an array type but it resolved to Scalar – AciD IoN Apr 08 '17 at 15:00
  • Fixed and tested – Tonechas Apr 20 '17 at 02:40
  • Done Sir thank you sorry i was newbie to stack overflow i dint know about the green tick sir now i came to know its done sir thank you for sorting me out in this problem – AciD IoN Apr 22 '17 at 13:33