资源简介
Ostu方法又名最大类间差方法,通过统计整个图像的直方图特性来实现全局阈值T的自动选取,其算法步骤为:
1) 先计算图像的直方图,即将图像所有的像素点按照0~255共256个bin,统计落在每个bin的像素点数量
2) 归一化直方图,也即将每个bin中像素点数量除以总的像素点
3) i表示分类的阈值,也即一个灰度级,从0开始迭代
4) 通过归一化的直方图,统计0~i 灰度级的像素(假设像素值在此范围的像素叫做前景像素) 所占整幅图像的比例w0,并统计前景像素的平均灰度u0;统计i~255灰度级的像素(假设像素值在此范围的像素叫做背景像素) 所占整幅图像的比例w1,并统计背景像素的平均灰度u1;
5) 计算前景像素和背景像素的方差 g = w0*w1*(u0-u1) (u0-u1)
6) i++;转到4),直到i为256时结束迭代
7)将最大g相应的i值作为图像的全局阈值
代码片段和文件信息
function [IDXsep] = otsu(In)
%OTSU Global image thresholding/segmentation using Otsu‘s method.
% IDX = OTSU(IN) segments the image I into N classes 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 = NaN(si
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 6046 2018-01-26 13:54 otsu.m
相关资源
- 基于小波变换的数字水印嵌入与提取
- matlab使用libsvm进行分类代码.rar
- 多目标粒子群算法matlab
- 车辆路径调度问题matlab
- 运用粒子群算法解决车间调度问题m
- matlab 2016a osgenericvideointerface
- MATLAB 2010a 激活文件 安装key lic_stan
- Park变换和Clark变换
- stokesmatlab 二维 有限元程序
- 运用MATLAB实现遗传算法求解规划问题
- 基于matlab的机器手puma560的运行仿真
- LMS音频降噪matlab程序
- 非线性各向异性扩散滤波MATLAB程序
- 锁相环 PLL matlab建模
- 地心地固坐标系ECEF转地心惯性系ECI
- matlab 去除人脸图像中的雀斑,人脸美
- RGB到YIQ,RGB到HSI和HSI到RGB,RGB到YcbC
- 基于标记的分水岭算法matlab程序
- M/G/1排队系统
- 基于变速模糊-PI混合控制的直流电机
- 最优化 外点罚函数 有matlab程序
- 数据包络法DEA)matlab代码
- 33节点的遗传算法无功优化MATLAB程序
- 人工鱼群算法Matlab代码
- matlab图像处理gui49054
- 时滞系统的模糊PID控制的matlab/simuli
- 基于matlab异步电机矢量控制
- SLEP工具包
- 基于Matlab随机时间序列预测模型数据
- 快速傅里叶变换算法matlab实现
评论
共有 条评论