欢迎来到天天文库
浏览记录
ID:38497720
大小:40.00 KB
页数:4页
时间:2019-06-13
《用matlab实现自适应图像阈值分割最大类方差法代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、%用matlab实现自适应图像阈值分割最大类方差法代码clear;warningoff;SE=strel('diamond',4);BW1=imread('cameraman.tif');BW2=imerode(BW1,SE);BW3=imdilate(BW2,SE);BW4=BW1-BW3;%rgb转灰度ifisrgb(BW4)==1I_gray=rgb2gray(BW4);elseI_gray=BW4;endfigure,imshow(I_gray);I_double=double(I_gray);%转化为双精度[wid,len]=size(I_gray)
2、;colorlevel=256;%灰度级hist=zeros(colorlevel,1);%直方图%threshold=128;%初始阈值%计算直方图fori=1:widforj=1:lenm=I_gray(i,j)+1;hist(m)=hist(m)+1;endendhist=hist/(wid*len);%直方图归一化miuT=0;form=1:colorlevelmiuT=miuT+(m-1)*hist(m);endxigmaB2=0;formindex=1:colorlevelthreshold=mindex-1;omega1=0;omega2=0;f
3、orm=1:threshold-1omega1=omega1+hist(m);endomega2=1-omega1;miu1=0;miu2=0;form=1:colorlevelifmxigm
4、aB2finalT=threshold;xigmaB2=xigmaB21;endendfT=finalT/255%阈值归一化T=graythresh(I_gray)%matlab函数求阈值fori=1:widforJ=1:lenifI_double(i,j)>finalTbn(i,j)=1;elsebin(i,j)=0;endendendfigure,imshow(bin);figure,plot(1:colorlevel,xigma);clear;warningoff;SE=strel('diamond',4);BW1=imread('img1.bmp');
5、BW2=imerode(BW1,SE);BW3=imdilate(BW2,SE);BW4=BW1-BW3;%rgb转灰度ifisrgb(BW4)==1I_gray=rgb2gray(BW4);elseI_gray=BW4;endfigure,imshow(I_gray);I_double=double(I_gray);%转化为双精度[wid,len]=size(I_gray);colorlevel=256;%灰度级hist=zeros(colorlevel,1);%直方图%threshold=128;%初始阈值%计算直方图fori=1:widforj=1:le
6、nm=I_gray(i,j)+1;hist(m)=hist(m)+1;endendhist=hist/(wid*len);%直方图归一化miuT=0;form=1:colorlevelmiuT=miuT+(m-1)*hist(m);endxigmaB2=0;formindex=1:colorlevelthreshold=mindex-1;omega1=0;omega2=0;form=1:threshold-1omega1=omega1+hist(m);endomega2=1-omega1;miu1=0;miu2=0;form=1:colorlevelifm7、hresholdmiu1=miu1+(m-1)*hist(m);elsemiu2=miu2+(m-1)*hist(m);endendmiu1=miu1/omega1;miu2=miu2/omega2;xigmaB21=omega1*(miu1-miuT)^2+omega2*(miu2-miuT)^2;xigma(mindex)=xigmaB21;ifxigmaB21>xigmaB2finalT=threshold;xigmaB2=xigmaB21;endendfT=finalT/255%阈值归一化T=graythresh(I_gray)%matlab函数求阈值8、fori=1:widforJ=1:le
7、hresholdmiu1=miu1+(m-1)*hist(m);elsemiu2=miu2+(m-1)*hist(m);endendmiu1=miu1/omega1;miu2=miu2/omega2;xigmaB21=omega1*(miu1-miuT)^2+omega2*(miu2-miuT)^2;xigma(mindex)=xigmaB21;ifxigmaB21>xigmaB2finalT=threshold;xigmaB2=xigmaB21;endendfT=finalT/255%阈值归一化T=graythresh(I_gray)%matlab函数求阈值
8、fori=1:widforJ=1:le
此文档下载收益归作者所有