⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 outsfengesuanfa.m

📁 大津站直的阈值化算法
💻 M
字号:
% OTSU method
% 2006/9/4
clc;
clear;

%I = imread('E:\test\chinalake.bmp','bmp');
I = imread('camera.bmp');
figure,imshow(I);
I=I(:,:,1);

I = double(I);

I=medfilt2(I);
%I = Medianfilter(I);         % median filter
h_Tmean = mean(mean(I));

[height,width] = size(I);
Size = height * width;  % the size of the image
h_T = sum(sum(I));      % the total gray value of the image

G_min = min(min(I));    % the min gray value of the image
G_max = max(max(I));    % the max gray value of the iamge

I_seg = zeros(height,width);     % the array to store the segmented image

thresh = 0;          % the threshold

num1 = 0;
num2 = 0;            % count the num of the pixel from the diffrient class
P1 = 0;
P2 = 0;              % the probability of the different class
h_T1 = 0;
h_T2 = 0;            % the total gray value of different class 
h_T1mean = 0;
h_T2mean = 0;        % the mean value of the class

max = 0;
count=1;
countt=0;
for thresh=G_min:G_max     % find the best threshold
    h_T1 = 0;
    h_T2 = 0;
    num1 = 0;
    num2 = 0;
    for h=1:height
        for w=1:width
             if I(h,w) <= thresh
                  num1 = num1 + 1;
                  h_T1 = h_T1 + I(h,w);
             end
         end
     end
     num2 = Size - num1;
     h_T2 = h_T - h_T1;
     P1 = num1/Size;
     P2 = num2/Size;
     
     h_T1mean = h_T1/num1;
     h_T2mean = h_T2/num2;
     
     %D = P1*(h_T1mean - h_Tmean)^2 + P2*(h_T2mean - h_Tmean)^2;
     D1(count) = P1*P2*(h_T1mean - h_T2mean)^2;                             % the tow equation is equal
     if D1(count) > max
         max = D1;
         T_best = thresh;  
         countt=count;
         % T record the best thresh
     end
     count=count+1;
 end
 
 %%%%%%% Seg the image %%%%%%%%%
 for i=1:height
     for j=1:width
         if I(i,j) > T_best
             I_seg(i,j) = 255;
         end
     end
 end
 T_best
 figure;
 imshow(uint8(I_seg));
 figure;
 imhist(uint8(I));

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -