转自http://hi.baidu.com/yibobin/blog/item/3a84c8f7047dee2f720eec81.html
一维直方图熵阈值算法
% 1D entropy thresholding method
% Pun提出,Kapur对其阈值和熵进行改进
% 两类:object 和background
% P1 = sum(pi) i:1~T
% P2 = sum(pi) i:T+1~255
% HO = ln(P1) + H1/P1;
% HB = ln(P2) + H2/P2;
% H1 = -sum(pi*ln(pi)); i:1~T
% H2 = -sum(pi*ln(pi)); i:T+1~255
% H = HO + HB;
% T_best = argmax(H);
clc;
clear;
%I = imread('E:/test/chinalake.bmp','bmp');
I = imread('E:/test/lena.png','png');
I = double(I);
I = Medianfilter(I); % median filter
[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
I_hist = zeros(1,256); % the array to store the hist of the 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
max = 0;
H1 = 0;
H2 = 0; % the middle var
H_object = 0;
H_background = 0;
H_total = 0; % the total entropy
T_best = 0; % the best thresh
%%%%% 计算直方图 %%%%%%
for i=1:height % calculate the hist of the image
for j=1:width