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
do you REALLY need that button ? (it's more a logic problem, event based programming follows a completely different path than a console prog.)
@berak I don't need any kind of button I have calculated the glcm features now I just want to display the output image in which texture features are analysed.
then just do your imshow(), where the image is
@berak as you can see in the piece of code provided that I am unable to form an image with these features , so could you plz suggest how to display features in an image
First, when computing the glcm matrix, reduce the number of shades (colors) to 16 or 32.
Then, calculate the haralick features on a small (sliding) window of your image.
Create new images for every feature and set the value of every pixel to the resulting value of the analysis of the corresponding window.
@kbarni thanks for the reply, could you please provide some reference or some piece of code for the same as I am new to OpenCV and image processing so I am finding this difficult to understand and implement.
btw, the energy is the sum of the squared glcm values, and your "a" matrix does not make any sense.
https://dsp.stackexchange.com/questio...
yes you are right energy is a number i.e sum of squared glcm values but as it happens in software like ENVI in which when we have to do texture analysis than what we input is an image and we get the output as an image it can be energy image contrast image etc, hence I feel that if an image is being displayed then there must be some matrix present and the corresponding pixel by pixel value of energy is calculated and hence displayed as an image in output. This is what I want to do @berak
yes, so follow kbarni's idea and the link above. subsample a glcm feature, like energy, from a 5x5 window of your image, then take the energy as the pixel value for the top-left corner of that patch. move your patch by 1 pixel, and repeat. in the end, you'll have an image with the glcm energy features from your original img.
@berak I was trying to follow what kbarni said but I am finding it very difficult to divide it into a window and all could you please provide some reference. And one more thing the concept of window is what we implement in advanced versions but I don't want to do that, for now, I just want that value of energy must be calculated corresponding to every pixel and hence a new energy matrix must be formed then, for now, I just want to display that matrix in the form of an image.