Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, here is the code I would use. It takes 16x16 pixel subimages and calculates the Haralick features for each subimage. Then, it displays the repartition of a given feature on the dest image.

Mat im=imread("lena.jpg",CV_LOAD_IMAGE_GRAYSCALE); //!!!! GLCM works ONLY on grayscale images
Mat dest(im.rows,im.cols,CV_64F);
for(int y=8;y<im.rows-8;y++)
    for(int x=8;x<im.cols-8;x++){
        Mat subimg=Mat(img,Rect(x-8,y-8,16,16));  //take a 16x16 subimage
        double feature=GLCM(subimg);  //get the energy (or other feature) for this window
        dest.at<double>(y,x)=feaure;
    }

You have to change the GLCM fuction:

double GLCM(Mat &image)
{
    ....
    return energy; //or any other feature
 }

I didn't check all the GLCM code, but as I said, there is a bug in the energy calculation:

double asm=0;  //angular second moment
...
for (int i = 0; i<256; i++)
    for (int j = 0; j<256; j++)
    {
        asm = asm + gl.at<float>(i, j)*gl.at<float>(i, j); //!!!!!
        ...
    }
energy=sqrt(asm); //!!!!!

As I said earlier, it's also a good idea to reduce the number of gray levels to 16 for much faster calculation.

Also note that the gl matrix is integer type, not float.