1、% 一维最大熵法对图片进行分割 filename,pathname=uigetfile;img=imread(strcat(pathname,filename);figure(1);subplot(221);imshow(img);subplot(222);imhist(img(:,1);subplot(223);imhist(img(:,2);subplot(224);imhist(img(:,3);m n k=size(img);p=zeros(256,1);for i=1:mfor j=1:np(img(i,j,1)+1)=p(img(i,j,1)+1)+1;end;end;p=p/(m
2、*n);temp=10;ht=0;Qt=0;for t=12:255ht=0;hn=0;pt=sum(p(1:t);pn=sum(p); for i=1:tif(p(i)0)ht=ht+log2(p(i)*p(i)*(-1);end;end;for i=1:256if(p(i)0)hn=hn+log2(p(i)*p(i)*(-1);end;end;if(pt0)if(ht/pt+(hn-ht)/(1-pt)+log2(pt*(1-pt)Qt)Qt=ht/pt+(hn-ht)/(1-pt)+log2(pt*(1-pt);temp=t;end; end;end;for i=1:mfor j=1:nif(img(i,j,1)temp)img(i,j,1)=255;img(i,j,2)=255;img(i,j,3)=255;elseimg(i,j,1)=0;img(i,j,2)=0;img(i,j,3)=0;end;end;end;figure(2);imshow(img);