4,445
社区成员
发帖
与我相关
我的任务
分享
/*--------------------------------------------------------------------------
3.计算四个共生矩阵P,取距离为1,角度分别为0,45,90,135
--------------------------------------------------------------------------*/
CvMat* mat_co[4];
for (dim=0;dim<4;dim++)
{
mat_co[dim] = cvCreateMat(16,16,CV_64FC1);
cvZero(mat_co[dim]);
}
for(m=0;m<16;m++)//灰度级
{
for(n=0;n<16;n++)
{
for(j=0;j<nH;j++)//图像大小尺寸
{
for(i=0;i<nW;i++)
{
uchar temp = CV_MAT_ELEM(*mat_low_uchar,uchar,j,i);
double tempmatvalue;
if (i<(nW-1)&&temp==m&&(CV_MAT_ELEM(*mat_low_uchar,uchar,j,i+1)==n))
{
tempmatvalue = cvGetReal2D(mat_co[0],m,n);
cvSetReal2D(mat_co[0],m,n,(tempmatvalue+1.0));
cvSetReal2D(mat_co[0],n,m,(tempmatvalue+1.0));
}
if (j>0&&i<(nW-1)&&temp==m&&(CV_MAT_ELEM(*mat_low_uchar,uchar,j-1,i+1))==n)
{
tempmatvalue = cvGetReal2D(mat_co[1],m,n);
cvSetReal2D(mat_co[1],m,n,(tempmatvalue+1.0));
cvSetReal2D(mat_co[1],n,m,(tempmatvalue+1.0));
}
if (j<(nH-1)&&temp==m&&(CV_MAT_ELEM(*mat_low_uchar,uchar,j+1,i))==n)
{
tempmatvalue = cvGetReal2D(mat_co[2],m,n);
cvSetReal2D(mat_co[2],m,n,(tempmatvalue+1.0));
cvSetReal2D(mat_co[2],n,m,(tempmatvalue+1.0));
}
if (j<(nH-1)&&i<(nW-1)&&temp==m&&(CV_MAT_ELEM(*mat_low_uchar,uchar,j+1,i+1))==n)
{
tempmatvalue = cvGetReal2D(mat_co[3],m,n);
cvSetReal2D(mat_co[3],m,n,(tempmatvalue+1.0));
cvSetReal2D(mat_co[3],n,m,(tempmatvalue+1.0));
}
}
}
if(m==n)
for (dim=0;dim<4;dim++)
{
double tempmatvalue1 = cvGetReal2D(mat_co[dim],m,n);
cvSetReal2D(mat_co[dim],m,n,(tempmatvalue1*2));
}
}
}
int delta[4][2] = {{0,1}, {-1,1}, {1,0}, {1,1}};
int p = 0;
int x, y;
for (j = 0; j < nH; j++) {
for (i = 0; i < nW; i++) {
x = CV_MAT_ELEM(*mat_low_uchar, uchar, j, i);
for (p = 0; p < 4; p++) {
if (j + delta[p][0] < nH && j + delta[p][0] >= 0 && i + delta[p][1] < nW && i + delta[p][1] >= 0) {
y = CV_MAT_ELEM(*mat_low_uchar, uchar, j + delta[p][0], i + delta[p][1]);
cvSetReal2D(mat_co[p], x, y, cvGetReal2D(mat_co[p], x, y) + 1.0);
cvSetReal2D(mat_co[p], y, x, cvGetReal2D(mat_co[p], y, x) + 1.0);
}
}
}
}