【图像算法】图像特征:GLCM

-------------------------------------------------------------------------------------------------------------------------------

【图像算法】图像特征:GLCM

      SkySeraph Aug 27th 2011  HQU

Email:zgzhaobo@gmail.com    QQ:452728574

Latest Modified Date:Aug 27th 2011 HQU

 图像算法系列: http://skyseraph.com/2011/08/27/CV/图像算法专题/ 

-------------------------------------------------------------------------------------------------------------------------------

一 原理

1 概念:GLCM,即灰度共生矩阵,GLCM是一个L*L方阵,L为源图像的灰度级

2 含义:描述的是具有某种空间位置关系的两个像素的联合分布,可看成两个像素灰度对的联合直方图,是一种二阶统计

3 常用的空间位置关系:有四种,垂直、水平、正负45°

4 常用的GLCM特征特征

(1)能量:  是灰度共生矩阵元素值的平方和,所以也称能量,反映了图像灰度分布均匀程度和纹理粗细度。
      如果共生矩阵的所有值均相等,则ASM值小;相反,如果其中一些值大而其它值小,则ASM值大。
      当共生矩阵中元素集中分布时,此时ASM值大。ASM值大表明一种较均一和规则变化的纹理模式。
(2)对比度:反映了图像的清晰度和纹理沟纹深浅的程度。纹理沟纹越深,其对比度越大,视觉效果越清晰;
      反之,对比度小,则沟纹浅,效果模糊。灰度差即对比度大的象素对越多,这个值越大。
      灰度公生矩阵中远离对角线的元素值越大,CON越大。
(3)相关:  它度量空间灰度共生矩阵元素在行或列方向上的相似程度,因此,相关值大小反映了图像中局部灰度相关性。
      当矩阵元素值均匀相等时,相关值就大;相反,如果矩阵像元值相差很大则相关值小。如果图像中有水平方向纹理,
      则水平方向矩阵的COR大于其余矩阵的COR值。
(4):  是图像所具有的信息量的度量,纹理信息也属于图像的信息,是一个随机性的度量,当共生矩阵中所有元素有最大的随机性、
      空间共生矩阵中所有值几乎相等时,共生矩阵中元素分散分布时,熵较大。它表示了图像中纹理的非均匀程度或复杂程度。
(5)逆差距:反映图像纹理的同质性,度量图像纹理局部变化的多少。其值大则说明图像纹理的不同区域间缺少变化,局部非常均匀。

5 原理理解

假设衣服图像的纹理矩阵P如下:

P = [ 0 1 2 0 1 2
   1 2 0 1 2 0
   2 0 1 2 0 1
   0 1 2 0 1 2
   1 2 0 1 2 0
   2 0 1 2 0 1
 ]

①相距为1(第一个参数),位置方向为0°第二个参数)的GLCM矩阵如下:

[ 0 10 10  

  10 0 10 

  10 10 0  

]

//解析:因为P中灰度级为3,故GLCM为3*3方阵

 

②相距为1(第一个参数),位置方向为正负45°第二个参数)的GLCM矩阵如下:

 

 [ 16  0  0
    0  16  0 
    0   0  18
]

 

-------------------------------------------------------------------------------------------------------------------------------

二 结果

 图像(lenna):

另附:关于lenna,风靡图像界这张图像,源原轶事:http://www.cs.cmu.edu/~chuck/lennapg/ ^_^

单个 GLCM以及4个方向的均值、方差GLCM特征:

 

 

-------------------------------------------------------------------------------------------------------------------------------

三 源码

类头文件

 

View Code
 1 // FeatureDetect.h: interface for the FeatureDetect class. 
2 //
3 ///////////////////////////////////////////////////////////////////////////////////////
4 /* Author: skyseraph/zhaobo 2011/4 zgzhaobo@gmal.com
5 */
6 ///////////////////////////////////////////////////////////////////////////////////////
7
8 #include <math.h>
9 #include "windows.h"
10 #include "iostream"
11 usingnamespace std;
12
13 typedef struct glcmFeature
14 {
15 double dCorrelation;
16 double dEnergy;
17 double dEntropy;
18 double dInertiaQuadrature;
19 double dLocalCalm;
20 }glcmFeature;
21
22 typedef struct glcmFeatureVar
23 {
24 double dAveCorrelation;
25 double dAveEnergy;
26 double dAveEntropy;
27 double dAveInertiaQuadrature;
28 double dAveLocalCalm;
29 double dVarCorrelation;
30 double dVarEnergy;
31 double dVarEntropy;
32 double dVarInertiaQuadrature;
33 double dVarLocalCalm;
34 }glcmFeatureVar;
35
36 class ZBGLCM
37 {
38 public:
39 ZBGLCM();
40 ~ZBGLCM();
41
42 void ComputeMatrix(BYTE **LocalImage, int LocalImageWidth);
43 void ComputeFeature(double&FeatureEnergy, double&FeatureEntropy,
44 double&FeatureInertiaQuadrature, double&FeatureCorrelation,
45 double&FeatureLocalCalm, int** pMatrix, int dim);
46 glcmFeature pGLCMF;
47 glcmFeatureVar pGLCMFVar;
48 glcmFeature GLCMFeature(BYTE* ImageArray,long ImageWidth,long ImageHeight,int FilterWindowWidth,int dir);
49 glcmFeatureVar GLCMFeatureVar(BYTE* ImageArray,long ImageWidth,long ImageHeight,int FilterWindowWidth);
50
51
52 public:
53 double FeatureLocalCalmRD;
54 double FeatureLocalCalmLD;
55 double FeatureLocalCalmV;
56 double FeatureLocalCalmH;
57 double FeatureCorrelationRD;
58 double FeatureCorrelationLD;
59 double FeatureCorrelationV;
60 double FeatureCorrelationH;
61 double FeatureInertiaQuadratureRD;
62 double FeatureInertiaQuadratureLD;
63 double FeatureInertiaQuadratureV;
64 double FeatureInertiaQuadratureH;
65 double FeatureEntropyRD;
66 double FeatureEntropyLD;
67 double FeatureEntropyV;
68 double FeatureEntropyH;
69 double FeatureEnergyRD;
70 double FeatureEnergyLD;
71 double FeatureEnergyV;
72 double FeatureEnergyH;
73
74 int FilterWindowWidth;
75 int distance;
76 int GrayLayerNum;
77 int L;
78 int** PMatrixRD;
79 int** PMatrixLD;
80 int** PMatrixV;
81 int** PMatrixH;
82 };

类源文件-1:初始化和资源释放

View Code
 1 //////////////////////////////////////////////////////////////////////
2 // Construction/Destruction
3 //////////////////////////////////////////////////////////////////////
4 ZBGLCM::ZBGLCM()
5 {
6 PMatrixRD = NULL;
7 PMatrixLD = NULL;
8 PMatrixV = NULL;
9 PMatrixH = NULL;
10
11 distance =5;
12 FilterWindowWidth =16;
13 GrayLayerNum =8;
14 L=8;
15
16 int i;
17 PMatrixH =newint*[GrayLayerNum];
18 PMatrixLD=newint*[GrayLayerNum];
19 PMatrixRD=newint*[GrayLayerNum];
20 PMatrixV =newint*[GrayLayerNum];
21
22 for(i=0; i<GrayLayerNum; i++)
23 {
24 PMatrixH[i] =newint[GrayLayerNum];
25 PMatrixLD[i]=newint[GrayLayerNum];
26 PMatrixRD[i]=newint[GrayLayerNum];
27 PMatrixV[i] =newint[GrayLayerNum];
28 }
29 }
30
31 ZBGLCM::~ZBGLCM()
32 {
33 if(PMatrixH !=NULL)
34 {
35 for(int i=0; i<GrayLayerNum; i++)
36 {
37 delete [] PMatrixH[i];
38 PMatrixH[i] = NULL; //已析构了,后不再加
39 }
40 delete [] PMatrixH;
41 PMatrixH = NULL;
42 }
43 if(PMatrixLD !=NULL)
44 {
45 for(int i=0; i<GrayLayerNum; i++)
46 {
47 delete[] PMatrixLD[i];
48 }
49 delete [] PMatrixLD;
50 }
51 if(PMatrixRD !=NULL)
52 {
53 for(int i=0; i<GrayLayerNum; i++)
54 delete [] PMatrixRD[i];
55 delete [] PMatrixRD;
56 }
57 if(PMatrixV !=NULL)
58 {
59 for(int i=0; i<GrayLayerNum; i++)
60 delete [] PMatrixV[i];
61 delete [] PMatrixV;
62 }
63 }

类源文件-2:计算纹理特征

View Code
  1 void ZBGLCM::ComputeFeature(double&FeatureEnergy, double&FeatureEntropy, 
2 double&FeatureInertiaQuadrature, double&FeatureCorrelation,
3 double&FeatureLocalCalm, int** pMatrix, int dim)
4 {
5 int i,j;
6 double**pdMatrix;
7 pdMatrix =newdouble*[dim];
8 for(i=0; i<dim; i++)
9 pdMatrix[i] =newdouble[dim];
10
11 int total =0;
12 for(i=0; i<dim; i++)
13 {
14 for(j=0; j<dim; j++)
15 {
16 total += pMatrix[i][j];
17 }
18 }
19 for(i=0; i<dim; i++)
20 {
21 for(j=0; j<dim; j++)
22 {
23 pdMatrix[i][j] = (double)pMatrix[i][j]/(double)total;
24 }
25 }
26
27 FeatureEnergy =0.0;
28 FeatureEntropy =0.0;
29 FeatureInertiaQuadrature =0.0;
30 FeatureLocalCalm =0.0;
31
32 for(i=0; i<dim; i++)
33 {
34 for(j=0; j<dim; j++)
35 {
36 FeatureEnergy += pdMatrix[i][j]*pdMatrix[i][j];
37 if(pdMatrix[i][j]>1e-12)
38 {
39 FeatureEntropy -= pdMatrix[i][j]*log(pdMatrix[i][j]);
40 }
41 FeatureInertiaQuadrature += (double)(i-j)*(double)(i-j)*pdMatrix[i][j];
42 FeatureLocalCalm += pdMatrix[i][j]/(1+(double)(i-j)*(double)(i-j));
43 }
44 }
45
46 double ux =0.0;
47 double localtotal =0.0;
48 for(i=0; i<dim; i++)
49 {
50 localtotal =0.0;
51 for(j=0; j<dim; j++)
52 {
53 localtotal += pdMatrix[i][j];
54 }
55 ux += (double)i * localtotal;
56 }
57
58 double uy =0.0;
59 for(j=0; j<dim; j++)
60 {
61 localtotal =0.0;
62 for(i=0; i<dim; i++)
63 {
64 localtotal += pdMatrix[i][j];
65 }
66 uy += (double)j * localtotal;
67 }
68
69 double sigmax =0.0;
70 for(i=0; i<dim; i++)
71 {
72 localtotal =0.0;
73 for(j=0; j<dim; j++)
74 {
75 localtotal += pdMatrix[i][j];
76 }
77 sigmax += (double)(i-ux) * (double)(i-ux) * localtotal;
78 }
79
80 double sigmay =0.0;
81 for(j=0; j<dim; j++)
82 {
83 localtotal =0.0;
84 for(i=0; i<dim; i++)
85 {
86 localtotal += pdMatrix[i][j];
87 }
88 sigmay += (double)(j-uy) * (double)(j-uy) * localtotal;
89 }
90
91 FeatureCorrelation =0.0;
92 for(i=0; i<dim; i++)
93 {
94 for(j=0; j<dim; j++)
95 {
96 FeatureCorrelation += (double)(i-ux) * (double)(j-uy) * pdMatrix[i][j];
97 }
98 }
99 if(sigmax !=0&& sigmay !=0)
100 {
101 FeatureCorrelation /= sigmax;
102 FeatureCorrelation /= sigmay;
103 }
104 else
105 FeatureCorrelation =8;
106
107 if(pdMatrix !=NULL)
108 {
109 for(i=0; i<dim; i++)
110 {
111 delete [] pdMatrix[i];
112 pdMatrix[i] = NULL;
113 }
114 delete [] pdMatrix;
115 pdMatrix = NULL;
116 }
117 }

类源文件-3:计算共生矩阵

View Code
 1 void ZBGLCM::ComputeMatrix(BYTE **LocalImage, int LocalImageWidth)
2 {
3 int i,j;
4
5 BYTE **NewImage;
6 NewImage =new BYTE*[LocalImageWidth];
7 if(NewImage==NULL)
8 return;
9 for(i=0; i<LocalImageWidth; i++)
10 {
11 NewImage[i] =new BYTE[LocalImageWidth];
12 if(NewImage[i]==NULL)
13 return;
14 }
15
16
17 for(i=0; i<LocalImageWidth; i++)
18 {
19 for(j=0; j<LocalImageWidth; j++)
20 {
21 NewImage[i][j] = LocalImage[i][j] / (256/GrayLayerNum);
22 }
23 }
24
25 for(i=0; i<GrayLayerNum; i++)
26 {
27 for(j=0; j<GrayLayerNum; j++)
28 {
29 PMatrixH[i][j] =0;
30 PMatrixLD[i][j] =0;
31 PMatrixRD[i][j] =0;
32 PMatrixV[i][j] =0;
33 }
34 }
35
36 for(i=0; i<LocalImageWidth; i++)
37 {
38 for(j=0; j<LocalImageWidth-distance; j++)
39 {
40 PMatrixH[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[i][j+distance]] +=1;
41 PMatrixH[(unsigned int)NewImage[i][j+distance]][(unsigned int)NewImage[i][j]] +=1;
42 }
43 }
44
45 for(i=0; i<LocalImageWidth-distance; i++)
46 {
47 for(j=0; j<LocalImageWidth; j++)
48 {
49 PMatrixV[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[i+distance][j]] +=1;
50 PMatrixV[(unsigned int)NewImage[i+distance][j]][(unsigned int)NewImage[i][j]] +=1;
51 }
52 }
53
54 for(i=0; i<LocalImageWidth-distance; i++)
55 {
56 for(j=0; j<LocalImageWidth-distance; j++)
57 {
58 int newi, newj;
59 newi = i+distance;
60 newj = j+distance;
61 PMatrixLD[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[newi][newj]] +=1;
62 PMatrixLD[(unsigned int)NewImage[newi][newj]][(unsigned int)NewImage[i][j]] +=1;
63 }
64 }
65
66 for(i=distance; i<LocalImageWidth; i++)
67 {
68 for(j=0; j<LocalImageWidth-distance; j++)
69 {
70 int newi, newj;
71 newi = i-distance;
72 newj = j+distance;
73 PMatrixRD[(unsigned int)NewImage[i][j]][(unsigned int)NewImage[newi][newj]] +=1;
74 PMatrixRD[(unsigned int)NewImage[newi][newj]][(unsigned int)NewImage[i][j]] +=1;
75 }
76 }
77
78 if(NewImage !=NULL)
79 {
80 for(i=0; i<LocalImageWidth; i++)
81 {
82 delete [] NewImage[i];
83 NewImage[i] = NULL;
84 }
85 delete [] NewImage;
86 NewImage = NULL;
87 }
88 }

 类源文件-4:计算GLCM特征 

View Code
 1 glcmFeature ZBGLCM::GLCMFeature(BYTE* ImageArray,long ImageWidth,long ImageHeight,int FilterWindowWidth,int dir)
2 {
3 assert(ImageHeight>FilterWindowWidth && ImageWidth > FilterWindowWidth);
4
5 double dEnergy =0.0;
6 double dEntropy =0.0;
7 double dInertiaQuadrature =0.0;
8 double dLocalCalm =0.0;
9 double dCorrelation =0.0;
10 double dEnergy1 =0.0;
11 double dEntropy1 =0.0;
12 double dInertiaQuadrature1=0.0;
13 double dLocalCalm1 =0.0;
14 double dCorrelation1 =0.0;
15
16 int rolltimeH = ImageHeight/FilterWindowWidth;
17 int rolltimeW = ImageWidth /FilterWindowWidth;
18 int i,j;
19 int p,q;
20
21 unsigned char** arLocalImage;
22 arLocalImage=(unsigned char**)calloc((unsigned)FilterWindowWidth,sizeof(unsigned char*));
23 for( i=0;i<FilterWindowWidth;i++)
24 {
25 arLocalImage[i]=(unsigned char*)calloc((unsigned)FilterWindowWidth,sizeof(unsigned char));
26 }
27
28 for(i=0; i< rolltimeH; i++)
29 {
30 for(j=0; j<rolltimeW; j++)
31 {
32 for(p=0; p<FilterWindowWidth; p++)
33 {
34 for(q=0; q<FilterWindowWidth; q++)
35 {
36 arLocalImage[p][q] =*((char*)ImageArray+(ImageHeight-1-(i*FilterWindowWidth+p))*ImageWidth+j*FilterWindowWidth+q);
37 }
38 }
39 ComputeMatrix(arLocalImage, FilterWindowWidth);
40 switch (dir)
41 {
42 case0:
43 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixH, GrayLayerNum);
44 break;
45 case1:
46 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixRD, GrayLayerNum);
47 break;
48 case2:
49 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixV, GrayLayerNum);
50 break;
51 case3:
52 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixLD, GrayLayerNum);
53 break;
54 default:
55 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixH, GrayLayerNum);
56 break;
57 }
58 dEnergy += dEnergy1;
59 dEntropy += dEntropy1;
60 dInertiaQuadrature += dInertiaQuadrature1;
61 dCorrelation += dCorrelation1;
62 dLocalCalm += dLocalCalm1;
63 }
64 }
65 dEnergy /= (rolltimeH*rolltimeW);
66 dEntropy /= (rolltimeH*rolltimeW);
67 dInertiaQuadrature /= (rolltimeH*rolltimeW);
68 dCorrelation /= (rolltimeH*rolltimeW);
69 dLocalCalm /= (rolltimeH*rolltimeW);
70
71 pGLCMF.dEnergy = dEnergy ;
72 pGLCMF.dEntropy = dEntropy;
73 pGLCMF.dInertiaQuadrature = dInertiaQuadrature;
74 pGLCMF.dCorrelation = dCorrelation;
75 pGLCMF.dLocalCalm = dLocalCalm;
76
77 for(i=0; i<FilterWindowWidth; i++)
78 {
79 free(arLocalImage[i]) ;
80 arLocalImage[i] = NULL;
81 }
82 free(arLocalImage);
83 arLocalImage = NULL;
84
85
86 return pGLCMF;
87 }

类源文件-5:计算GLCM特征均值和方差 

View Code
  1 glcmFeatureVar ZBGLCM::GLCMFeatureVar(BYTE* ImageArray,long ImageWidth,long ImageHeight,int FilterWindowWidth)
2 {
3 assert(ImageHeight>FilterWindowWidth && ImageWidth > FilterWindowWidth);
4
5 double dEnergy =0.0;
6 double dEntropy =0.0;
7 double dInertiaQuadrature =0.0;
8 double dLocalCalm =0.0;
9 double dCorrelation =0.0;
10
11 double dEnergy1 =0.0;
12 double dEntropy1 =0.0;
13 double dInertiaQuadrature1=0.0;
14 double dLocalCalm1 =0.0;
15 double dCorrelation1 =0.0;
16
17 double dEnergy2 =0.0;
18 double dEntropy2 =0.0;
19 double dInertiaQuadrature2=0.0;
20 double dLocalCalm2 =0.0;
21 double dCorrelation2 =0.0;
22
23 double dEnergy3 =0.0;
24 double dEntropy3 =0.0;
25 double dInertiaQuadrature3=0.0;
26 double dLocalCalm3 =0.0;
27 double dCorrelation3 =0.0;
28
29 double dEnergy4 =0.0;
30 double dEntropy4 =0.0;
31 double dInertiaQuadrature4=0.0;
32 double dLocalCalm4 =0.0;
33 double dCorrelation4 =0.0;
34
35 double dEnergy11 =0.0;
36 double dEntropy11 =0.0;
37 double dInertiaQuadrature11=0.0;
38 double dLocalCalm11 =0.0;
39 double dCorrelation11 =0.0;
40
41 double dEnergy22 =0.0;
42 double dEntropy22 =0.0;
43 double dInertiaQuadrature22=0.0;
44 double dLocalCalm22 =0.0;
45 double dCorrelation22 =0.0;
46
47 double dEnergy33 =0.0;
48 double dEntropy33 =0.0;
49 double dInertiaQuadrature33=0.0;
50 double dLocalCalm33 =0.0;
51 double dCorrelation33 =0.0;
52
53 double dEnergy44 =0.0;
54 double dEntropy44 =0.0;
55 double dInertiaQuadrature44=0.0;
56 double dLocalCalm44 =0.0;
57 double dCorrelation44 =0.0;
58
59 int rolltimeH = ImageHeight/FilterWindowWidth;
60 int rolltimeW = ImageWidth /FilterWindowWidth;
61 int i,j;
62 int p,q;
63
64 unsigned char** arLocalImage;
65 arLocalImage=(unsigned char**)calloc((unsigned)FilterWindowWidth,sizeof(unsigned char*));
66 for( i=0;i<FilterWindowWidth;i++)
67 {
68 arLocalImage[i]=(unsigned char*)calloc((unsigned)FilterWindowWidth,sizeof(unsigned char));
69 }
70
71 for(i=0; i< rolltimeH; i++)
72 {
73 for(j=0; j<rolltimeW; j++)
74 {
75 for(p=0; p<FilterWindowWidth; p++)
76 {
77 for(q=0; q<FilterWindowWidth; q++)
78 {
79 arLocalImage[p][q] =*((char*)ImageArray+(ImageHeight-1-(i*FilterWindowWidth+p))*ImageWidth+j*FilterWindowWidth+q);
80 }
81 }
82 ComputeMatrix(arLocalImage, FilterWindowWidth);
83 ComputeFeature(dEnergy1, dEntropy1, dInertiaQuadrature1, dCorrelation1, dLocalCalm1, PMatrixH, GrayLayerNum);
84 dEnergy += dEnergy1;
85 dEntropy += dEntropy1;
86 dInertiaQuadrature += dInertiaQuadrature1;
87 dCorrelation += dCorrelation1;
88 dLocalCalm += dLocalCalm1;
89 dEnergy11 += dEnergy1;
90 dEntropy11 += dEntropy1;
91 dInertiaQuadrature11 += dInertiaQuadrature1;
92 dCorrelation11 += dCorrelation1;
93 dLocalCalm11 += dLocalCalm1;
94 ComputeMatrix(arLocalImage, FilterWindowWidth);
95 ComputeFeature(dEnergy2, dEntropy2, dInertiaQuadrature2, dCorrelation2, dLocalCalm2, PMatrixRD, GrayLayerNum);
96 dEnergy += dEnergy2;
97 dEntropy += dEntropy2;
98 dInertiaQuadrature += dInertiaQuadrature2;
99 dCorrelation += dCorrelation2;
100 dLocalCalm += dLocalCalm2;
101 dEnergy22 += dEnergy2;
102 dEntropy22 += dEntropy2;
103 dInertiaQuadrature22 += dInertiaQuadrature2;
104 dCorrelation22 += dCorrelation2;
105 dLocalCalm22 += dLocalCalm2;
106 ComputeMatrix(arLocalImage, FilterWindowWidth);
107 ComputeFeature(dEnergy3, dEntropy3, dInertiaQuadrature3, dCorrelation3, dLocalCalm3, PMatrixV, GrayLayerNum);
108 dEnergy += dEnergy3;
109 dEntropy += dEntropy3;
110 dInertiaQuadrature += dInertiaQuadrature3;
111 dCorrelation += dCorrelation3;
112 dLocalCalm += dLocalCalm3;
113 dEnergy33 += dEnergy3;
114 dEntropy33 += dEntropy3;
115 dInertiaQuadrature33 += dInertiaQuadrature3;
116 dCorrelation33 += dCorrelation3;
117 dLocalCalm33 += dLocalCalm3;
118 ComputeMatrix(arLocalImage, FilterWindowWidth);
119 ComputeFeature(dEnergy4, dEntropy4, dInertiaQuadrature4, dCorrelation4, dLocalCalm4, PMatrixLD, GrayLayerNum);
120 dEnergy += dEnergy4;
121 dEntropy += dEntropy4;
122 dInertiaQuadrature += dInertiaQuadrature4;
123 dCorrelation += dCorrelation4;
124 dLocalCalm += dLocalCalm4;
125 dEnergy44 += dEnergy4;
126 dEntropy44 += dEntropy4;
127 dInertiaQuadrature44 += dInertiaQuadrature4;
128 dCorrelation44 += dCorrelation4;
129 dLocalCalm44 += dLocalCalm4;
130
131 }
132 }
133 dEnergy /= (rolltimeH*rolltimeW);
134 dEntropy /= (rolltimeH*rolltimeW);
135 dInertiaQuadrature /= (rolltimeH*rolltimeW);
136 dCorrelation /= (rolltimeH*rolltimeW);
137 dLocalCalm /= (rolltimeH*rolltimeW);
138
139 dEnergy11 /= (rolltimeH*rolltimeW);
140 dEntropy11 /= (rolltimeH*rolltimeW);
141 dInertiaQuadrature11 /= (rolltimeH*rolltimeW);
142 dCorrelation11 /= (rolltimeH*rolltimeW);
143 dLocalCalm11 /= (rolltimeH*rolltimeW);
144
145 dEnergy22 /= (rolltimeH*rolltimeW);
146 dEntropy22 /= (rolltimeH*rolltimeW);
147 dInertiaQuadrature22 /= (rolltimeH*rolltimeW);
148 dCorrelation22 /= (rolltimeH*rolltimeW);
149 dLocalCalm22 /= (rolltimeH*rolltimeW);
150
151 dEnergy33 /= (rolltimeH*rolltimeW);
152 dEntropy33 /= (rolltimeH*rolltimeW);
153 dInertiaQuadrature33 /= (rolltimeH*rolltimeW);
154 dCorrelation33 /= (rolltimeH*rolltimeW);
155 dLocalCalm33 /= (rolltimeH*rolltimeW);
156
157 dEnergy44 /= (rolltimeH*rolltimeW);
158 dEntropy44 /= (rolltimeH*rolltimeW);
159 dInertiaQuadrature44 /= (rolltimeH*rolltimeW);
160 dCorrelation44 /= (rolltimeH*rolltimeW);
161 dLocalCalm44 /= (rolltimeH*rolltimeW);
162
163 pGLCMFVar.dAveEnergy = dEnergy/4 ;
164 pGLCMFVar.dAveEntropy = dEntropy/4;
165 pGLCMFVar.dAveInertiaQuadrature = dInertiaQuadrature/4;
166 pGLCMFVar.dAveCorrelation = dCorrelation/4;
167 pGLCMFVar.dAveLocalCalm = dLocalCalm/4;
168
169 pGLCMFVar.dVarEnergy=((dEnergy11-pGLCMFVar.dAveEnergy)*(dEnergy11-pGLCMFVar.dAveEnergy)
170 +(dEnergy22-pGLCMFVar.dAveEnergy)*(dEnergy22-pGLCMFVar.dAveEnergy)
171 +(dEnergy33-pGLCMFVar.dAveEnergy)*(dEnergy33-pGLCMFVar.dAveEnergy)
172 +(dEnergy44-pGLCMFVar.dAveEnergy)*(dEnergy44-pGLCMFVar.dAveEnergy))/4;
173 pGLCMFVar.dVarEntropy=((dEntropy11-pGLCMFVar.dAveEntropy)*(dEntropy11-pGLCMFVar.dAveEntropy)
174 +(dEntropy22-pGLCMFVar.dAveEntropy)*(dEntropy22-pGLCMFVar.dAveEntropy)
175 +(dEntropy33-pGLCMFVar.dAveEntropy)*(dEntropy33-pGLCMFVar.dAveEntropy)
176 +(dEntropy44-pGLCMFVar.dAveEntropy)*(dEntropy44-pGLCMFVar.dAveEntropy))/4;
177 pGLCMFVar.dVarInertiaQuadrature=((dInertiaQuadrature11-pGLCMFVar.dAveInertiaQuadrature)*(dInertiaQuadrature11-pGLCMFVar.dAveInertiaQuadrature)
178 +(dInertiaQuadrature22-pGLCMFVar.dAveInertiaQuadrature)*(dInertiaQuadrature22-pGLCMFVar.dAveInertiaQuadrature)
179 +(dInertiaQuadrature33-pGLCMFVar.dAveInertiaQuadrature)*(dInertiaQuadrature33-pGLCMFVar.dAveInertiaQuadrature)
180 +(dInertiaQuadrature44-pGLCMFVar.dAveInertiaQuadrature)*(dInertiaQuadrature44-pGLCMFVar.dAveInertiaQuadrature))/4;
181 pGLCMFVar.dVarCorrelation=((dCorrelation11-pGLCMFVar.dAveCorrelation)*(dCorrelation11-pGLCMFVar.dAveCorrelation)
182 +(dCorrelation22-pGLCMFVar.dAveCorrelation)*(dCorrelation22-pGLCMFVar.dAveCorrelation)
183 +(dCorrelation33-pGLCMFVar.dAveCorrelation)*(dCorrelation33-pGLCMFVar.dAveCorrelation)
184 +(dCorrelation44-pGLCMFVar.dAveCorrelation)*(dCorrelation44-pGLCMFVar.dAveCorrelation))/4;
185 pGLCMFVar.dVarLocalCalm=((dLocalCalm11-pGLCMFVar.dAveLocalCalm)*(dLocalCalm11-pGLCMFVar.dAveLocalCalm)
186 +(dLocalCalm22-pGLCMFVar.dAveLocalCalm)*(dLocalCalm22-pGLCMFVar.dAveLocalCalm)
187 +(dLocalCalm33-pGLCMFVar.dAveLocalCalm)*(dLocalCalm33-pGLCMFVar.dAveLocalCalm)
188 +(dLocalCalm44-pGLCMFVar.dAveLocalCalm)*(dLocalCalm44-pGLCMFVar.dAveLocalCalm))/4;
189
190
191 for(i=0; i<FilterWindowWidth; i++)
192 {
193 free(arLocalImage[i]) ;
194 arLocalImage[i] = NULL;
195 }
196 free(arLocalImage);
197 arLocalImage = NULL;
198
199
200 return pGLCMFVar;
201 }

 

说明:

参考了 《VisualC++数字图像模式识别技术详解》、《数字图像处理与机器视觉-VisualC++与Matlab实现》等书,此类为本文作者原创,可直接调用,转载/引用请注明出处。

-------------------------------------------------------------------------------------------------------------------------------

四 参考资料

 

GLCM Texture Tutorial
Gray-level Co-occurrence Matrix(灰度共生矩阵) _ 丕子

灰度共生矩阵 - tyut - 博客园

使用OpenCv的cvGLCM报错

灰度共发矩阵专题_百度文库

灰度共生矩阵VC++实现_百度文库

图像的灰度共生矩阵_百度文库

灰度共生矩阵_百度文库

提取共生矩阵特征 - wqvbjhc的专栏 - CSDN博客

基于灰度共生矩阵的纹理特征提取 - docin.com豆丁网

基于灰度共生矩阵的图像分割方法研究_百度文库

一个使用GLCM的例子.(修改了CvTexture的bug)

《VisualC++数字图像模式识别技术详解》

《数字图像处理与机器视觉-VisualC++与Matlab实现》

 

More in  http://skyseraph.com/2011/08/27/CV/图像算法专题/ 

------------------------------------------------------------------------------------------------------------------------------

 

Author:         SKySeraph

 

Email/GTalk: zgzhaobo@gmail.com    QQ:452728574

 

From:         http://www.cnblogs.com/skyseraph/

 

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.

 

 -----------------------------------------------------------------------------------------------------------------------------

posted @ 2011-08-27 21:02 SkySeraph 阅读(...) 评论(...) 编辑 收藏