//SetImageROI函数用起来可能有问题。当把一个区域定义为ROI后,如果在另一个函数中直接把这个ROI当做图片来处理而不加以特别对待会出问题。
int GetGLCM(const IplImage * img,double * result)
{
if(NULL == img || NULL == result)
return 1;
int width = img->width;
int height = img->height;
if(img->roi!=NULL)
{
cout<<"ROI!!!!!!!!!!!!!!!"<<endl;
width = img->roi->width;
height = img->roi->height;
}
int * mark = new int[width * height]; //保存灰度级
if(NULL == mark)
return 2;
int step = img->widthStep/sizeof(uchar);
uchar * data = (uchar *)img->imageData;
//划分灰度级
if(img->roi!=NULL)
{
cout<<"ROI!!!!!!!!!!!!!!!!!!!!!!!!!"<<endl;
for (int i = 0;i < img->roi->height;++i)
{
for (int j = 0;j < img->roi->width;++j)
{
mark[i * width + j] = int(GLCM_CLASS * (data[(i + img->roi->yOffset) * img->width + j+img->roi->xOffset]) * 1.0 /256);
}
}
}
else
{
for (int i = 0;i < height;++i)
{
for (int j = 0;j < width;++j)
{
mark[i * width + j] = int(GLCM_CLASS * (data[i * width + j]) * 1.0 /256);
}
}
}
//计算四个方向的灰度共生矩阵,获取纹理特征
//固定距离d = 1
if(GetGLCMForOneDisOneTheta(mark,height,width,1,0,result) > 0)
{
for(int i = 0;i < 4;i++)
result[i] = 0;
cout<<"dayu 0"<<endl;
}
if(GetGLCMForOneDisOneTheta(mark,height,width,0,1,&result[4]) > 0)
{
for(int i = 0;i < 4;i++)
result[4 + i] = 0;
cout<<"dayu 0"<<endl;
}
if(GetGLCMForOneDisOneTheta(mark,height,width,1,1,&result[8]) > 0)
{
for(int i = 0;i < 4;i++)
result[8 + i] = 0;
cout<<"dayu 0"<<endl;
}
if(GetGLCMForOneDisOneTheta(mark,height,width,-1,1,&result[12]) > 0)
{
for(int i = 0;i < 4;i++)
result[12 + i] = 0;
cout<<"dayu 0"<<endl;
}
delete[] mark;
return 0;
}