基于小波变换和K-Mean算法的图像分类一样本纹理特征提取在计算机视觉研究领域,纹理是描述图像的一个重要特征,基于小波变换的纹理分割方法是一种理想的图像分割算法。本例采用了Daubechies1小波。设定小波包分解的层数为m,对Bi,i=1,2,…,p分别进行m层二维小波包分解,对于每个图像子块,均得到4m个分解子频带图像。这里为了简便,决定进行单尺度的小波变换,m为一,共得到4个分解子频带的图像。每个图像子块的4个子频带的绝对值平均能量值作为一个特征分量,其表示为1(,)ijeXijMN,其中M,N为子频带X的宽和高,(,)Xij为频带系数值,因此第i个子块的特征向量为,1,2,3,4,,,,iiiiiweeee,其中,ike,表示第k个子频带的平均能量值,代码如下%读入样本11,即遥感图像的背景I=imread('11.jpg');I=rgb2gray(I);%灰度值归一化I=im2double(I);%二维小波分解[cA,cH,cV,cD]=dwt2(I,'db1');[MN]=size(I);M=M/2;N=N/2;A11=0;H11=0;V11=0;D11=0;fori=1:Mforj=1:NA11=A11+cA(i,j)/(M*N);H11=H11+cH(i,j)/(M*N);V11=V11+cV(i,j)/(M*N);D11=D11+cD(i,j)/(M*N);endend%得出特征向量T11T11=[A11;H11;V11;D11]';二对待分类图像进行小波变换,提取特征向量%读入待分类遥感图像I=imread('tm2000mask.jpg');I=rgb2gray(I);%灰度值归一化I=im2double(I);%二维小波分解[cA,cH,cV,cD]=dwt2(I,'db1');[MN]=size(I);M=M/2;N=N/2;P=[cA;cH;cV;cD];三使用K-Mean算法进行遥感图像分类1)计算各点与分类初始中心的距离d(1)=((cA(i,j)-T1(1))^2+(cH(i,j)-T1(2))^2+(cV(i,j)-T1(3))^2+(cD(i,j)-T1(4))^2);d(2)=((cA(i,j)-T2(1))^2+(cH(i,j)-T2(2))^2+(cV(i,j)-T2(3))^2+(cD(i,j)-T2(4))^2);d(3)=((cA(i,j)-T3(1))^2+(cH(i,j)-T3(2))^2+(cV(i,j)-T3(3))^2+(cD(i,j)-T3(4))^2);………………..2)确定类别并调整聚类中心if(d(1)==min_d)K(i,j)=1;number1=number1+1;elseif(d(2)==min_d)K(i,j)=2;number2=number2+1;elseif(d(3)==min_d)K(i,j)=3;number3=number3+1;elseif(d(4)==min_d)K(i,j)=4;number4=number4+1;elseif(d(5)==min_d)K(i,j)=5;number5=number5+1;elseif(d(6)==min_d)K(i,j)=6;number6=number6+1;elseif(d(7)==min_d)K(i,j)=7;number7=number7+1;elseif(d(8)==min_d)K(i,j)=8;number8=number8+1;elseif(d(9)==min_d)K(i,j)=9;number9=number9+1;elseif(d(10)==min_d)K(i,j)=10;number10=number10+1;elseif(d(11)==min_d)K(i,j)=11;number11=number11+1;ende_new=e_new+sqrt(min_d);%计算迭代终止条件endendT1=T1*0;T2=T2*0;T3=T3*0;T4=T4*0;T5=T5*0;T6=T6*0;T7=T7*0;T8=T8*0;T9=T9*0;T10=T10*0;T11=T11*0;%调整聚类中心e_new=e_new/M/N;%%%%%%%%%%%%%%%%%%%%%%调整各聚类的中心%%%%%%%%%%%%%%%%%%%%%%%%%fori=1:Mforj=1:Nif(K(i,j)==1)T1(1)=T1(1)+cA(i,j);T1(2)=T1(2)+cH(i,j);T1(3)=T1(3)+cV(i,j);T1(4)=T1(4)+cD(i,j);……………..3)进行迭代while(abs(e_old-e_new)0.00001)times=times+1;e_new;e_old=e_new;e_new=0;四还原分类图像result=zeros(M,N,3);%进行上彩色,RGB彩图fori=1:Mforj=1:N%R分量G分量B分量if(K(i,j)==1)result(i,j,1)=0;result(i,j,2)=0;result(i,j,3)=0;a1=a1+1;elseif(K(i,j)==2)result(i,j,1)=25;result(i,j,2)=76;result(i,j,3)=127;a2=a2+1;elseif(K(i,j)==3)result(i,j,1)=51;result(i,j,2)=102;result(i,j,3)=153;a3=a3+1;elseif(K(i,j)==4)result(i,j,1)=76;result(i,j,2)=51;result(i,j,3)=178;a4=a4+1;elseif(K(i,j)==5)result(i,j,1)=102;result(i,j,2)=102;result(i,j,3)=178;a5=a5+1;elseif(K(i,j)==6)result(i,j,1)=127;result(i,j,2)=127;result(i,j,3)=102;a6=a6+1;elseif(K(i,j)==7)result(i,j,1)=153;result(i,j,2)=229;result(i,j,3)=51;a7=a7+1;elseif(K(i,j)==8)result(i,j,1)=178;result(i,j,2)=51;result(i,j,3)=204;a8=a8+1;elseif(K(i,j)==9)result(i,j,1)=204;result(i,j,2)=76;result(i,j,3)=127;a9=a9+1;elseif(K(i,j)==10)result(i,j,1)=229;result(i,j,2)=51;result(i,j,3)=25;a10=a10+1;elseif(K(i,j)==11)result(i,j,1)=255;result(i,j,2)=127;result(i,j,3)=153;a11=a11+1;endendendresult=uint8(result);figure;imshow(result);title('基于小波变换的图像分类效果');五显示各类地物比例%绘制地物面积饼状图a=[a1,a2,a3,a4,a5,a6,a7,a8,a9,a10];figure;pie(a);legend('海水','农地','绿林地','房屋','养殖场','芦苇','互花米草','海三棱藨草','光滩','未利用地',-1);%标注图例