图像分割的阈值算法matlab实现_第1页
图像分割的阈值算法matlab实现_第2页
图像分割的阈值算法matlab实现_第3页
图像分割的阈值算法matlab实现_第4页
图像分割的阈值算法matlab实现_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、图像分割的阈值算法matlab实现【OTSU,1DEntropy,2DEntropy】今天看了几篇论文,实现了一下,没有验证各算法的有效性也没有进行定量比较OTSU% OTSU method% 2006/9/4clc;clear;%I = imread('E:testchinalake.bmp','bmp');I = imread('E:testlena.png','png');I = double(I);I = Medianfilter(I);       &

2、#160; % median filterh_Tmean = mean(mean(I);height,width = size(I);Size = height * width;  % the size of the imageh_T = sum(sum(I);      % the total gray value of the imageG_min = min(min(I);    % the min gray value of the imageG_max = max(max(I); &#

3、160;  % the max gray value of the iamgeI_seg = zeros(height,width);     % the array to store the segmented imagethresh = 0;          % the thresholdnum1 = 0;num2 = 0;          

4、0; % count the num of the pixel from the diffrient classP1 = 0;P2 = 0;              % the probability of the different classh_T1 = 0;h_T2 = 0;            % the total gray value of

5、different class h_T1mean = 0;h_T2mean = 0;        % the mean value of the classmax = 0;for thresh=G_min:G_max     % find the best threshold    h_T1 = 0;    h_T2 = 0;    num1 = 0;    for

6、 h=1:height        for w=1:width             if I(h,w) <= thresh                  num1 = num1 + 1;   &

7、#160;              h_T1 = h_T1 + I(h,w);             end         end     end     num2 = Size -

8、num1;     h_T2 = h_T - h_T1;     P1 = num1/Size;     P2 = num2/Size;          h_T1mean = h_T1/num1;     h_T2mean = h_T2/num2;          %D =

9、P1*(h_T1mean - h_Tmean)2 + P2*(h_T2mean - h_Tmean)2;     D1 = P1*P2*(h_T1mean - h_T2mean)2;                             % the tow equation i

10、s equal     if D1 > max         max = D1;         T_best = thresh;            % T record the best thresh     end end&

11、#160; % Seg the image % for i=1:height     for j=1:width         if I(i,j) > T_best             I_seg(i,j) = 255;         end&#

12、160;    end end T_best figure; imshow(uint8(I_seg); figure; imhist(uint8(I);      *    一维直方图熵阈值算法% 1D entropy thresholding method% Pun提出,Kapur对其阈值和熵进行改进%  两类:object 和background% P1 = sum(pi)  i:1T% P2

13、 = sum(pi)  i:T+1255% HO = ln(P1)  + H1/P1;% HB = ln(P2) + H2/P2;% H1 = -sum(pi*ln(pi); i:1T% H2 = -sum(pi*ln(pi); i:T+1255% H = HO + HB;% T_best = argmax(H);clc;clear;%I = imread('E:testchinalake.bmp','bmp');I = imread('E:testlena.png','png');I = double(I);

14、I = Medianfilter(I);         % median filterheight,width = size(I);Size = height * width;  % the size of the imageh_T = sum(sum(I);      % the total gray value of the imageG_min = min(min(I);    % the min gray value

15、 of the imageG_max = max(max(I);    % the max gray value of the iamgeI_seg = zeros(height,width);     % the array to store the segmented imageI_hist = zeros(1,256);           % the array to store the hist of the ima

16、gethresh = 0;          % the thresholdnum1 = 0;num2 = 0;            % count the num of the pixel from the diffrient classP1 = 0;P2 = 0;           

17、;   % the probability of the different classh_T1 = 0;h_T2 = 0;            % the total gray value of different class max = 0;H1 = 0;H2 = 0;             % the middle varH_object

18、 = 0;H_background = 0;H_total = 0;        % the total entropyT_best = 0;         % the best thresh% 计算直方图 %for i=1:height        % calculate the hist of the image    for j=1:w

19、idth        I_hist(I(i,j)+1) = I_hist(I(i,j)+1) + 1;    endendfor thresh=G_min:G_max     % find the best threshold    H1 = 0;    h_T1 = 0;    H2 = 0;    for h=1:height

20、60;       for w=1:width             if I(h,w) <= thresh                  num1 = num1 + 1;     

21、60;            h_T1 = h_T1 + I(h,w);             end         end     end     num2 = Size - num1; 

22、60;   h_T2 = h_T - h_T1;     P1 = num1/Size;     P2 = num2/Size;     for i=1:thresh         px = I_hist(i+1)/Size;         H1 = H1 + (-px*ln(px); 

23、;    end     for i=thresh+1:G_max         px = I_hist(i+1)/Size;         H2 = H2 + (-px*ln(px);     end          H_object = l

24、n(P1) + H1/P1;     H_background = ln(P2) + H2/P2;          H_total = H_object + H_background;          if H_total > max         max = H_total;  

25、60;      T_best = thresh;     end end  % Seg the image % for i=1:height     for j=1:width         if I(i,j) > T_best          &

26、#160;  I_seg(i,j) = 255;         end     end end T_best figure; imshow(uint8(I_seg); figure; imhist(uint8(I);*2维直方图熵阈值算法% 二维直方图熵阈值法% 参考 基于2D 熵阈值的铁谱磨粒图像分割方法,傅建平%廖振强,张培林,汪传忠,(南京理工大学机械学院,南京 ),%(军械工程学院,石家庄)%&#

27、160;  pixel gray%   %   |%   |              => 2D histgram%   |%   |%   |_> local grayclc;clear;%I = imread('E:testchinalake.bmp','bmp');I = imread(

28、'E:testlena.png','png');I = double(I);height,width = size(I);Size = height * width;  % the size of the imageG_min = min(min(I);    % the min gray value of the imageG_max = max(max(I);    % the max gray value of the iamgeI_2Dhist = zeros(G_max+1,G_ma

29、x+1);   % the array to store the 2D hist of the imageI_mean = zeros(height,width);   % the mean value of the local imageI_seg = zeros(height,width);WS = 3;                     

30、;     % mean filter's window size 3*3nr = floor(WS/2);I_big = zeros(height+2*nr,width+2*nr);    % the bigger array used to mean filterI_big(nr+1:height+nr,nr+1:width+nr) = I;  % copy data from the original image% mean filter % 获取局部区域灰度信息 %for i=1:height

31、60;   for j=1:width         sum = 0;         num = 0;         for h=-nr:nr             for w=-nr:nr  

32、;               sum = sum + I_big(i+h,j+w);                 num = num + 1;             end

33、         end         I_mean(i,j) = sum/num;     end end  % 构建2D直方图,横轴上以点象素灰度表示,纵轴上以局部区域灰度表示 % for i=1:height     for j=1:width     

34、0;   h = I(i,j)+1;             % 横轴信息,避免0,所以加1,象素灰度         w = I_mean(i,j)+1;        % 纵轴信息,避免0,所以加1,局部区域灰度        

35、; I_2Dhist(h,w) = I_2Dhist(h,w) + 1;  % 统计灰度对<pixel,local>的出现次数,构建2D直方图     end end   % find the best thresh : hor_thresh,and ver_thresh % for ver_thresh=0:G_max     for hor_thresh=0:G_max      

36、    sum1 = 0;          sum2 = 0;          H1 = 0;          H2 = 0;         for i=0:ver_thresh   

37、;          for j=0:hor_thresh                 sum1 = sum1 + I_2Dhist(i+1,j+1);             end   

38、60;     end         for i=0:ver_thresh             for j=0:hor_thresh                 P1 = I_2Dhist(

39、i+1,j+1)/sum1;                 H1 = H1 + P1*log(P1);             end         end      

40、0;          if i < G_max & j < G_max            for i=ver_thresh+1:G_max                 for j=hor_thresh+1:G_max&#

41、160;                   sum2 = sum2 + I_2Dhist(i+1,j+1);                end          &

42、#160; end            for i=ver_thresh+1:G_max                for j=hor_thresh+1:G_max                    P2 = I_2Dhist(i+1,j+1)/sum2;                    H2 = H2 +P2*log(P2);       

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论