图像分割的阈值算法matlab实现.doc
图像分割的阈值算法matlab实现【OTSU,1DEntropy,2DEntropy】今天看了几篇论文,实现了一下,没有验证各算法的有效性也没有进行定量比较OTSU%OTSU%2006/9/4clc;clear;%I=imread( E:\test\chinalake.bmp , bmp );I=imread( E:\test\lena.png , png );I=double(I);I=Medianfilter(I);%medianfilterh_Tmean=mean(mean(I));[height,width]=size(I);Size=height*width;%thesizeoftheimageh_T=sum(sum(I));%thetotalgrayvalueoftheimageG_min=min(min(I));%themingrayvalueoftheimageG_max=max(max(I));%themaxgrayvalueoftheiamgeI_seg=zeros(height,width);%thearraytostorethesegmentedimagethresh=0;%thethresholdnum1=0;num2=0;%countthenumofthepixelfromthediffrientclassP1=0;P2=0;%theprobabilityofthedifferentclassh_T1=0;h_T2=0;%thetotalgrayvalueofdifferentclassh_T1mean=0;h_T2mean=0;%themeanvalueoftheclassmax=0;forthresh=G_min:G_max%findthebestthresholdh_T1=0;h_T2=0;num1=0;forh=1:heightforw=1:widthifI(h,w)maxmax=D1;T_best=thresh;%Trecordthebestthreshendend%%%%%%%Segtheimage%%%%%%%%%fori=1:heightforj=1:widthifI(i,j)>T_bestI_seg(i,j)=255;endendendT_bestfigure;imshow(uint8(I_seg));figure;imhist(uint8(I));***************************************************一维直方图熵阈值算法%1Dentropythresholding%Pun提出,Kapur对其阈值和熵进行改进%两类:object和background%P1=sum(pi)i:1~T%P2=sum(pi)i:T+1~255%HO=ln(P1)+H1/P1;%HB=ln(P2)+H2/P2;%H1=-sum(pi*ln(pi));i:1~T%H2=-sum(pi*ln(pi));i:T+1~255%H=HO+HB;%T_best=argmax(H);clc;clear;%I=imread( E:\test\chinalake.bmp , bmp );I=imread( E:\test\lena.png , png );I=double(I);I=Medianfilter(I);%medianfilter[height,width]=size(I);Size=height*width;%thesizeoftheimageh_T=sum(sum(I));%thetotalgrayvalueoftheimageG_min=min(min(I));%themingrayvalueoftheimageG_max=max(max(I));%themaxgrayvalueoftheiamgeI_seg=zeros(height,width);%thearraytostorethesegmentedimageI_hist=zeros(1,256);%thearraytostorethehistoftheimagethresh=0;%thethresholdnum1=0;num2=0;%countthenumofthepixelfromthediffrientclassP1=0;P2=0;%theprobabilityofthedifferentclassh_T1=0;h_T2=0;%thetotalgrayvalueofdifferentclassmax=0;H1=0;H2=0;%themiddlevarH_object=0;H_background=0;H_total=0;%thetotalentropyT_best=0;%thebestthresh%%%%%计算直方图%%%%%%fori=1:height%calculatethehistoftheimageforj=1:widthI_hist(I(i,j)+1)=I_hist(I(i,j)+1)+1;endendforthresh=G_min:G_max%findthebestthresholdH1=0;h_T1=0;H2=0;forh=1:heightforw=1:widthifI(h,w)maxmax=H_total;T_best=thresh;endend%%%%%%%Segtheimage%%%%%%%%%fori=1:heightforj=1:widthifI(i,j)>T_bestI_seg(i,j)=255;endendendT_bestfigure;imshow(uint8(I_seg));figure;imhist(uint8(I));**********************************************************2维直方图熵阈值算法%二维直方图熵阈值法%参考基于2D熵阈值的铁谱磨粒图像分割方法,傅建平%廖振强,张培林,汪传忠,(南京理工大学机械学院,南京),%(军械工程学院,石家庄)%pixelgray%^%|%|==>2Dhistgram%|%|%|__________________________>localgrayclc;clear;%I=imread( E:\test\chinalake.bmp , bmp );I=imread( E:\test\lena.png , png );I=double(I);[height,width]=size(I);Size=height*width;%thesizeoftheimageG_min=min(min(I));%themingrayvalueoftheimageG_max=max(max(I));%themaxgrayvalueoftheiamgeI_2Dhist=zeros(G_max+1,G_max+1);%thearraytostorethe2DhistoftheimageI_mean=zeros(height,width);%themeanvalueofthelocal