资源简介
多阈值的OTSU算法,用于图像分割
代码片段和文件信息
function [IDXsep] = otsu(In)
%OTSU Global image thresholding/segmentation using Otsu‘s method.
% IDX = OTSU(IN) segments the image I into N classes by means of Otsu‘s
% N-thresholding method. OTSU returns an array IDX containing the cluster
% indices (from 1 to N) of each point. Zero values are assigned to
% non-finite (NaN or Inf) pixels.
%
% IDX = OTSU(I) uses two classes (N=2 default value).
%
% [IDXsep] = OTSU(...) also returns the value (sep) of the separability
% criterion within the range [0 1]. Zero is obtained only with data
% having less than N values whereas one (optimal value) is obtained only
% with N-valued arrays.
%
% Notes:
% -----
% It should be noticed that the thresholds generally become less credible
% as the number of classes (N) to be separated increases (see Otsu‘s
% paper for more details).
%
% If I is an RGB image a Karhunen-Loeve transform is first performed on
% the three RGB channels. The segmentation is then carried out on the
% image component that contains most of the energy.
%
% Example:
% -------
% load clown
% subplot(221)
% X = ind2rgb(Xmap);
% imshow(X)
% title(‘Original‘‘FontWeight‘‘bold‘)
% for n = 2:4
% IDX = otsu(Xn);
% subplot(22n)
% imagesc(IDX) axis image off
% title([‘n = ‘ int2str(n)]‘FontWeight‘‘bold‘)
% end
% colormap(gray)
%
% Reference:
% ---------
% Otsu N A Threshold Selection Method from Gray-Level Histograms
% IEEE Trans. Syst. Man Cybern. 9:62-66;1979
%
% See also GRAYTHRESH IM2BW
%
% -- Damien Garcia -- 2007/08 revised 2010/03
% Visit my % href=“matlab:web(‘http://www.biomecardio.com/matlab/otsu.html‘)“>website for more details about OTSU
error(nargchk(12nargin))
% Check if is the input is an RGB image
isRGB = isrgb(I);
assert(isRGB | ndims(I)==2...
‘The input must be a 2-D array or an RGB image.‘)
%% Checking n (number of classes)
if nargin==1
n = 2;
elseif n==1;
IDX = NaN(size(I));
sep = 0;
return
elseif n~=abs(round(n)) || n==0
error(‘MATLAB:otsu:WrongNValue‘...
‘n must be a strictly positive integer!‘)
elseif n>255
n = 255;
warning(‘MATLAB:otsu:TooHighN‘...
‘n is too high. n value has been changed to 255.‘)
end
I = single(I);
%% Perform a KLT if isRGB and keep the component of highest energy
if isRGB
sizI = size(I);
I = reshape(I[]3);
[VD] = eig(cov(I));
[tmpc] = max(diag(D));
I = reshape(I*V(:c)sizI(1:2)); % component with the highest energy
end
%% Convert to 256 levels
I = I-min(I(:));
I = round(I/max(I(:))*255);
%% Probability distribution
unI = sort(unique(I));
nbins = min(length(unI)256);
if nbins==n
IDX = ones(size(I));
for i = 1:n IDX(I==unI(i)) = i; end
sep = 1;
return
elseif nbins IDX
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
目录 0 2014-03-02 10:14 otsu\
文件 6055 2010-03-10 23:35 otsu\otsu.m
文件 118 2014-03-01 18:22 otsu\Unti
文件 118 2014-03-01 16:50 otsu\Unti
- 上一篇:两相混合式步进电机的电机主体仿真
- 下一篇:matlab编写运动视频检测
评论
共有 条评论