gusucode.com > 《matlab图像处理与界面编程宝典》秦襄培 编著,每章的MATLAB源代码程序 > 第23章/代码23-4.txt
f=imread('peppers.png'); % 读入图像 f=rgb2gray(f); % 转换为灰度图像 f=im2double(f); % 数据类型转换 % 全局阈值 T=0.5*(min(f(:))+max(f(:))); done=false; while ~done g=f>=T; Tn=0.5*(mean(f(g))+mean(f(~g))); done=abs(T-Tn)<0.1; T=Tn; end display('Threshold(T) - Iterative'); % 显示文字 T r=im2bw(f,T); % 图像黑白转换 figure,imshow(f),title('Original Image'); % 显示原始图像 figure,imshow(r); % 显示处理后的图像 title('Global Thresholding - Iterative Method'); % 图像标题 Th=graythresh(f); % 阈值 display('Threshold(T) - Otsu''s Method'); % 显示文字 Th s=im2bw(f,Th); % 图像黑白转换 figure,imshow(s); % 显示处理后的图像 title('Global Thresholding - Otsu''s Method'); % 图像标题 se=strel('disk',10); ft=imtophat(f,se); Thr=graythresh(ft); % 阈值 display('Threshold(T) - Local Thresholding'); % 显示文字 Thr lt=im2bw(ft,Thr); % 图像黑白转换 figure,imshow(lt); % 显示处理后的图像 title('Local Thresholding'); % 图像标题