图像分割的阈值算法matlab实现
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
图像分割的阈值算法matlab实现【OTSU,1DEntropy,2DEntropy】
今天看了几篇论文,实现了一下,没有验证各算法的有效性
也没有进行定量比较
OTSU
% OTSU method
% 2006/9/4
clc;
clear;
%I = imread('E:\test\chinalake.bmp','bmp');
I = imread('E:\test\lena.png','png');
I = double(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;
for thresh=G_min:G_max % find the best threshold
h_T1 = 0;
h_T2 = 0;
num1 = 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 = P1*P2*(h_T1mean - h_T2mean)^2; % the tow equation is equal if D1 > max
max = D1;
T_best = thresh; % T record the best thresh
end
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));
***************************************************
一维直方图熵阈值算法
% 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;