灰度共生矩阵GLCM及纹理特征影像生成
实现类似于滤波过程中的5*5窗体移动,形成子图像的过程,这里的方法边界的象元,滑动窗口元素补0:
IDL代码
1 Pro Texture 2 ;针对灰度影像 3 file=Dialog_Pickfile(Filter='*.bmp',/Must_exist) 4 image=Read_Bmp(file) 5 sz=size(image) 6 m=sz[1] 7 n=sz[2] 8 ;影像压缩成16个灰度级 9 dimage=uintarr(m,n) 10 for i=0,m-1 do begin 11 for j=0,n-1 do begin 12 dimage[i,j]=image[i,j] * 16 / 256 13 endfor 14 endfor 15 ;构造子影像,窗口大小5X5 16 subimage=uintarr(5,5) 17 e=0 18 for i=0,m-1 do begin 19 for j=0,n-1 do begin 20 ;填充窗口内的值 21 for k=-2,2,1 do begin 22 t=i+k 23 if t Lt 0 then begin 24 Continue 25 endif 26 if t ge m then begin 27 Break 28 Endif 29 for l=-2,2,1 do begin 30 if(j+l) lt 0 then begin 31 continue; 32 endif 33 if(j+l) lt n then begin 34 ;给子影像赋值 35 subimage[k+2,l+2]=dimage[i+k,j+l]; 36 endif 37 endfor 38 endfor 39 ; test=uintarr(5,5); 40 ; for r=0,4 do begin 41 ; for s=0,4 do begin 42 ; test[r,s]=subimage[r,s] 43 ; Endfor 44 ; endfor 45 ;应用子影像进行计算 46 47 endfor 48 endfor 49 TV,image,True =1 50 end
下面是glcm计算的第一个版本,直接用IDL编程有两个问题需要注意,一个是逻辑运算符,IDL中的>和<符号不表示比较,而是表示取大和取消,逻辑运算采用字母表示,如ge,gt等;二是两个整数相除,得到的结果仍是整数,需要将被除数类型转换。
GLCM IDL代码
1 Function Glcm,subImage,d 2 sz=size(subImage) 3 m=sz[1] 4 n=sz[2] 5 glcmH=lonarr(16,16) 6 glcmEN=lonarr(16,16) 7 glcmV=lonarr(16,16) 8 glcmWN=lonarr(16,16) 9 10 ;0度 11 for i= 0,m-1 do begin 12 for j=0,n-d-1 do begin 13 glcmH[subImage[i,j],subImage[i,j+d]]=glcmH[subImage[i,j],subImage[i,j+d]]+1;%是共生矩阵0度的计算式 14 glcmH[subImage[i,j+d],subImage[i,j]]=glcmH[subImage[i,j+d],subImage[i,j]]+1 15 endFor 16 endFor 17 ;45度 18 for ii= 0,m-d-1 do begin 19 for jj=0,n-d-1 do begin 20 glcmEN[subImage[ii,jj],subImage[ii+d,jj+d]]=glcmEN[subImage[ii,jj],subImage[ii+d,jj+d]]+1;%是共生矩阵45度的计算式 21 glcmEN[subImage[ii+d,jj+d],subImage[ii,jj]]=glcmEN[subImage[ii+d,jj+d],subImage[ii,jj]]+1 22 endFor 23 endFor 24 ;90度 25 for iii= 0,m-d-1 do begin 26 for jjj=0,n-1 do begin 27 glcmV[subImage[iii,jjj],subImage[iii+d,jjj]]=glcmV[subImage[iii,jjj],subImage[iii+d,jjj]]+1;%是共生矩阵90度的计算式 28 glcmV[subImage[iii+d,jjj],subImage[iii,jjj]]=glcmV[subImage[iii+d,jjj],subImage[iii,jjj]]+1 29 endFor 30 endFor 31 ;135度 32 for iiii= d,m-1 do begin 33 for jjjj=0,n-d-1 do begin 34 glcmWN[subImage[iiii,jjjj],subImage[iiii-d,jjjj+d]]=glcmWN[subImage[iiii,jjjj],subImage[iiii-d,jjjj+d]]+1;%是共生矩阵135度的计算式 35 glcmWN[subImage[iiii-d,jjjj+d],subImage[iiii,jjjj]]=glcmWN[subImage[iiii-d,jjjj+d],subImage[iiii,jjjj]]+1 36 endFor 37 endFor 38 ;归一化 39 ;sumH=0L 40 ;sumEN=0L 41 ;sumV=0L 42 ;sumWN=0L 43 ;for k=0,15 do begin 44 ; for l=0,15 do begin 45 ; sumH=sumH+glcmH[k,l] 46 ; sumEN=sumEN+glcmEN[k,l] 47 ; sumV=sumV+glcmV[k,l] 48 ; sumWN=sumWN+glcmWN[k,l] 49 ; endfor 50 ;endfor 51 glcmHU=dblarr(16,16) 52 glcmENU=dblarr(16,16) 53 glcmVU=dblarr(16,16) 54 glcmWNU=dblarr(16,16) 55 for kk=0,15 do begin 56 for ll=0,15 do begin 57 glcmHU[kk,ll]=double(glcmH[kk,ll])/(2*m*(n-d)) 58 glcmENU[kk,ll]=double(glcmEN[kk,ll])/(2*(m-d)*(n-d)) 59 glcmVU[kk,ll]=double(glcmV[kk,ll])/(2*(m-d)*n) 60 glcmWNU[kk,ll]=double(glcmWN[kk,ll])/(2*(m-d)*(n-d)) 61 endfor 62 endfor 63 ;求取特征值 64 e=dblarr(4) 65 H=dblarr(4) 66 In=dblarr(4) 67 Ux=dblarr(4) 68 Uy=dblarr(4) 69 deltaX=dblarr(4) 70 deltaY=dblarr(4) 71 C=dblarr(4) 72 for i1=0,15 do begin 73 for j1=0,15 do begin 74 e[0]=e[0]+glcmHU[i1,j1]*glcmHU[i1,j1] 75 e[1]=e[1]+glcmENU[i1,j1]*glcmENU[i1,j1] 76 e[2]=e[2]+glcmVU[i1,j1]*glcmVU[i1,j1] 77 e[3]=e[3]+glcmWNU[i1,j1]*glcmWNU[i1,j1] 78 79 if glcmHU[i1,j1] ne 0 then begin 80 H[0] = -glcmHU[i1,j1]*Alog10(glcmHU[i1,j1])+H[0]; %%熵 81 endif 82 if glcmENU[i1,j1] ne 0 then begin 83 H[1] = -glcmENU[i1,j1]*Alog10(glcmENU[i1,j1])+H[1]; %%熵 84 endif 85 if glcmVU[i1,j1] ne 0 then begin 86 H[2] = -glcmVU[i1,j1]*Alog10(glcmVU[i1,j1])+H[2]; %%熵 87 endif 88 if glcmWNU[i1,j1] ne 0 then begin 89 H[3] = -glcmWNU[i1,j1]*Alog10(glcmWNU[i1,j1])+H[3]; %%熵 90 endif 91 In[0] = (i1-j1)^2*glcmHU[i1,j1]+In[0]; %%惯性矩 92 In[1] = (i1-j1)^2*glcmENU[i1,j1]+In[1]; 93 In[2] = (i1-j1)^2*glcmVU[i1,j1]+In[2]; 94 In[3] = (i1-j1)^2*glcmWNU[i1,j1]+In[3]; 95 96 Ux[0] = i1*glcmHU[i1,j1]+Ux[0]; %相关性中μx 97 Uy[0] = j1*glcmHU[i1,j1]+Uy[0]; %相关性中μy 98 Ux[1] = i1*glcmENU[i1,j1]+Ux[1]; %相关性中μx 99 Uy[1] = j1*glcmENU[i1,j1]+Uy[1]; %相关性中μy 100 Ux[2] = i1*glcmVU[i1,j1]+Ux[2]; %相关性中μx 101 Uy[2] = j1*glcmVU[i1,j1]+Uy[2]; %相关性中μy 102 Ux[3] = i1*glcmWNU[i1,j1]+Ux[3]; %相关性中μx 103 Uy[3] = j1*glcmWNU[i1,j1]+Uy[3]; %相关性中μy 104 endfor 105 endfor 106 for i2=0,15 do begin 107 for j2=0,15 do begin 108 deltaX[0] = (i2-Ux[0])^2*glcmHU[i2,j2]+deltaX[0]; %相关性中σx 109 deltaY[0] = (j2-Uy[0])^2*glcmHU[i2,j2]+deltaY[0]; %相关性中σy 110 C[0] = i2*j2*glcmHU[i2,j2]+C[0]; 111 112 deltaX[1] = (i2-Ux[1])^2*glcmENU[i2,j2]+deltaX[1]; %相关性中σx 113 deltaY[1] = (j2-Uy[1])^2*glcmENU[i2,j2]+deltaY[1]; %相关性中σy 114 C[1] = i2*j2*glcmENU[i2,j2]+C[1]; 115 116 deltaX[2] = (i2-Ux[2])^2*glcmVU[i2,j2]+deltaX[2]; %相关性中σx 117 deltaY[2] = (j2-Uy[2])^2*glcmVU[i2,j2]+deltaY[2]; %相关性中σy 118 C[2] = i2*j2*glcmVU[i2,j2]+C[2]; 119 120 deltaX[3] = (i2-Ux[3])^2*glcmWNU[i2,j2]+deltaX[3]; %相关性中σx 121 deltaY[3] = (j2-Uy[3])^2*glcmWNU[i2,j2]+deltaY[3]; %相关性中σy 122 C[3] = i2*j2*glcmWNU[i2,j2]+C[3]; 123 endfor 124 endfor 125 C[0] = (C[0]-Ux[0]*Uy[0])/deltaX[0]/deltaY[0]; %相关性 126 C[1] = (C[1]-Ux[1]*Uy[1])/deltaX[1]/deltaY[1]; 127 C[2] = (C[2]-Ux[2]*Uy[2])/deltaX[2]/deltaY[2]; 128 C[3] = (C[3]-Ux[3]*Uy[3])/deltaX[3]/deltaY[3]; 129 ;特征值求平均 130 feature=dblarr(4) 131 for i3=0,3 do begin 132 feature[0]=feature[0]+e[i3] 133 feature[1]=feature[1]+H[i3] 134 feature[2]=feature[2]+In[i3] 135 feature[3]=feature[3]+C[i3] 136 endfor 137 for i4=0,3 do begin 138 feature[i4]=feature[i4]/4 139 endfor 140 return,feature;返回特征值 141 end
该函数传入灰度图像subImage和步长参数d,调用GLCM函数的测试代码:
1 pro test_glcm 2 file=Dialog_Pickfile(Filter='*.bmp',/Must_exist) 3 image=Read_Bmp(file) 4 sz=size(image) 5 m=sz[1] 6 n=sz[2] 7 ;影像压缩成16个灰度级 8 dimage=uintarr(m,n) 9 textImage=uintarr(m,n) 10 for i=0,m-1 do begin 11 for j=0,n-1 do begin 12 dimage[i,j]=image[i,j] * 16 / 256 13 endfor 14 endfor 15 feature=glcm(dimage,1) 16 print,feature 17 end
这里贴出代码,希望熟悉的朋友也帮忙验证一下,看看能否对其中的某些部分优化一下!
参考文献:
1.贾永红《数字图像处理》武汉大学出版社 P182~P184
2.http://www.cnblogs.com/skyseraph/archive/2011/08/27/2155776.html