Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

GLCM texture analysis using OpenCV C++

I want to make a project to do texture analysis on an image, as per now I have coded to display the glcm features - Energy, Contrast, homogeneity, IDM, entropy, mean. Now I want to display these features in the form of an image as output. The piece of code is here

void glcm(cv::Mat &img)
{   
 float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
 int row=img.rows,col=img.cols;
 cv::Mat gl=cv::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];


      for(int i=0;i<256;i++)
      for(int j=0;j<256;j++)
    {
    energy=energy+gl.at<float>(i,j)*gl.at<float>(i,j);            //finding parameters
    contrast=contrast+(i-j)*(i-j)*gl.at<float>(i,j);
    homogenity=homogenity+gl.at<float>(i,j)/(1+abs(i-j));
    if(i!=j)
      IDM=IDM+gl.at<float>(i,j)/((i-j)*(i-j));                      //Taking k=2;
    if(gl.at<float>(i,j)!=0)
      entropy=entropy-gl.at<float>(i,j)*log10(gl.at<float>(i,j));
    mean1=mean1+0.5*(i*gl.at<float>(i,j)+j*gl.at<float>(i,j));
    }
        cout<<"energy="<<energy<<endl;
          cout<<"contrast="<<contrast<<endl;
        cout<<"homogenity="<<homogenity<<endl;
        cout<<"IDM="<<IDM<<endl;
          cout<<"entropy="<<entropy<<endl;
       cout<<"mean="<<mean1<<endl;
         }

         void MainWindow::on_pushButton_2_clicked()
          {
    void glcm(cv::Mat &);
        cv::Mat img=cv::imread("C:\\Users\\trainee2017233\\Desktop\\pre-
         post\\raster_image.png",CV_LOAD_IMAGE_UNCHANGED);            //input image
if(img.empty())
{

}

glcm(img);                                                                      //call to glcm function
cv::namedWindow("Image",CV_WINDOW_AUTOSIZE);
imshow("Image",img);
cv::waitKey(0);


   }

Please help to display the output image.

GLCM texture analysis using OpenCV C++

I want to make a project to do texture analysis on an image, as per now I have coded to display the glcm features - Energy, Contrast, homogeneity, IDM, entropy, mean. Now I want to display these features in the form of an image as output. The piece of code is here

void glcm(cv::Mat &img)
{   
 float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
 int row=img.rows,col=img.cols;
 cv::Mat gl=cv::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;

          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];


      for(int i=0;i<256;i++)
      for(int j=0;j<256;j++)
     {
     energy=energy+gl.at<float>(i,j)*gl.at<float>(i,j);            //finding parameters
     contrast=contrast+(i-j)*(i-j)*gl.at<float>(i,j);
     homogenity=homogenity+gl.at<float>(i,j)/(1+abs(i-j));
     if(i!=j)
       IDM=IDM+gl.at<float>(i,j)/((i-j)*(i-j));                      //Taking k=2;
     if(gl.at<float>(i,j)!=0)
       entropy=entropy-gl.at<float>(i,j)*log10(gl.at<float>(i,j));
     mean1=mean1+0.5*(i*gl.at<float>(i,j)+j*gl.at<float>(i,j));
     }
        cout<<"energy="<<energy<<endl;
        cout<<"contrast="<<contrast<<endl;
        cout<<"homogenity="<<homogenity<<endl;
        cout<<"IDM="<<IDM<<endl;
        cout<<"entropy="<<entropy<<endl;
       cout<<"mean="<<mean1<<endl;
    }

     void MainWindow::on_pushButton_2_clicked()
      {
    void glcm(cv::Mat &);
     cv::Mat img=cv::imread("C:\\Users\\trainee2017233\\Desktop\\pre-
      post\\raster_image.png",CV_LOAD_IMAGE_UNCHANGED);            //input image
 if(img.empty())
 {

 }

 glcm(img);                          //call to glcm function
 cv::namedWindow("Image",CV_WINDOW_AUTOSIZE);
 imshow("Image",img);
 cv::waitKey(0);
     }

Please help to display the output image.

GLCM texture analysis using OpenCV C++

I want to make a project to do texture analysis on an image, as per now I have coded to display the glcm features - Energy, Contrast, homogeneity, IDM, entropy, mean. Now I want to display these features convert my resultant matrix in the form of an image as output. The piece of code and hence display could someone suggest me a way to display the 2D matrix in the Mat form. Here is here my code

 void glcm(cv::Mat &img)
{   
    {
     float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
  int row=img.rows,col=img.cols;
  cv::Mat gl=cv::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;

     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];


      for(int i=0;i<256;i++)
       for(int j=0;j<256;j++)
        {
        energy=energy+gl.at<float>(i,j)*gl.at<float>(i,j);    energy=gl.at<float>(i,j)*gl.at<float>(i,j);
        a[i][j]=energy;
//        cout<<energy;
        //finding parameters
        contrast=contrast+(i-j)*(i-j)*gl.at<float>(i,j);
        homogenity=homogenity+gl.at<float>(i,j)/(1+abs(i-j));
        if(i!=j)
          IDM=IDM+gl.at<float>(i,j)/((i-j)*(i-j));                      //Taking k=2;
        if(gl.at<float>(i,j)!=0)
         entropy=entropy-gl.at<float>(i,j)*log10(gl.at<float>(i,j));
       mean1=mean1+0.5*(i*gl.at<float>(i,j)+j*gl.at<float>(i,j));
        }
 
        for(int i=0;i<256;i++)
        {
          for(int j=0;j<256;j++)
           cout<<a[i][j]<<"\t";
           cout<<endl;
         }






     cout<<"energy="<<energy<<endl;
      cout<<"contrast="<<contrast<<endl;
      cout<<"homogenity="<<homogenity<<endl;
      cout<<"IDM="<<IDM<<endl;
      cout<<"entropy="<<entropy<<endl;
      cout<<"mean="<<mean1<<endl;
    }

    void MainWindow::on_pushButton_2_clicked()
     {
     void glcm(cv::Mat &);
     cv::Mat img=cv::imread("C:\\Users\\trainee2017233\\Desktop\\pre-
     post\\raster_image.png",CV_LOAD_IMAGE_UNCHANGED); 
      img=cv::imread("C:\\Users\\trainee2017233\\Desktop\\prepost\\raster_image.png",CV_LOAD_IMAGE_UNCHANGED);            
           //input image
      if(img.empty())
      {

       }

     glcm(img);                          //call to glcm function
     cv::namedWindow("Image",CV_WINDOW_AUTOSIZE);
    imshow("Image",img);
     cv::waitKey(0);
 cv::Mat d=cv::Mat(256, 256, CV_8U, &a,2); //this is just a try to convert a as mat
     cv::namedWindow("Result",CV_WINDOW_AUTOSIZE);
      cv::imshow("Result",d);

    cv::waitKey(1);
    }

Please help I want to display this a matrix in the output image.form of image please tell me some function or some method to do so

GLCM texture analysis using OpenCV C++

I want to convert my resultant matrix in the form of image and hence display could someone suggest me a way to display the 2D matrix in the Mat form. Here is my code

    void glcm(cv::Mat &img)
    {
     float energy=0,contrast=0,homogenity=0,IDM=0,entropy=0,mean1=0;
     int row=img.rows,col=img.cols;
     cv::Mat gl=cv::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];


      for(int i=0;i<256;i++)
         for(int j=0;j<256;j++)
        {
        energy=gl.at<float>(i,j)*gl.at<float>(i,j);
        a[i][j]=energy;
//        cout<<energy;
        //finding parameters
        contrast=contrast+(i-j)*(i-j)*gl.at<float>(i,j);
        homogenity=homogenity+gl.at<float>(i,j)/(1+abs(i-j));
        if(i!=j)
          IDM=IDM+gl.at<float>(i,j)/((i-j)*(i-j));                      //Taking k=2;
        if(gl.at<float>(i,j)!=0)
          entropy=entropy-gl.at<float>(i,j)*log10(gl.at<float>(i,j));
        mean1=mean1+0.5*(i*gl.at<float>(i,j)+j*gl.at<float>(i,j));
        }

        for(int i=0;i<256;i++)
        {
          for(int j=0;j<256;j++)
           cout<<a[i][j]<<"\t";
           cout<<endl;
         }






     cout<<"energy="<<energy<<endl;
     cout<<"contrast="<<contrast<<endl;
     cout<<"homogenity="<<homogenity<<endl;
     cout<<"IDM="<<IDM<<endl;
     cout<<"entropy="<<entropy<<endl;
     cout<<"mean="<<mean1<<endl;
     }

    void MainWindow::on_pushButton_2_clicked()
    {
      void glcm(cv::Mat &);
      cv::Mat 
      img=cv::imread("C:\\Users\\trainee2017233\\Desktop\\prepost\\raster_image.png",CV_LOAD_IMAGE_UNCHANGED);            
           //input image
      if(img.empty())
     {

      }

    glcm(img);                                                                      //call to glcm function
    cv::namedWindow("Image",CV_WINDOW_AUTOSIZE);
    imshow("Image",img);
    cv::Mat d=cv::Mat(256, 256, CV_8U, &a,2); //this is just a try to convert a as mat
     cv::namedWindow("Result",CV_WINDOW_AUTOSIZE);
      cv::imshow("Result",d);

    cv::waitKey(1);
    }

I want to display this a matrix in the form of image please tell me some function or some method to do so