Otsu阈值分割详解

 Otsu(大津法或最大类间方差法)使用的是聚类的思想,把图像的灰度数按灰度级分成2个部分,使得两个部分之间的灰度值差异最大,每个部分之间的灰度差异最小,通过方差的计算来寻找一个合适的灰度级别来划分。 所以可以在二值化的时候采用otsu算法来自动选取阈值进行二值化。otsu算法被认为是图像分割中阈值选取的最佳算法,计算简单,不受图像亮度和对比度的影响。因此,使类间方差最大的分割意味着错分概率最小。
 

设t为设定的阈值。

图像总平均灰度为: u = w0∗u0 + w1∗u1 

从L个灰度级遍历 t,使得 t 为某个值的时候,前景和背景的方差最大,则 这个 t 值便是我们要求得的阈值。其中,方差的计算公式如下:

 g = wo∗(u0−u)∗(u0−u) + w1∗(u1−u)∗(u1−u) 

此公式计算量较大,可以采用:

 g = w0∗w1∗(u0−u1)∗(u0−u1) 

由于Otsu算法是对图像的灰度级进行聚类,因此在执行Otsu算法之前,需要计算该图像的灰度直方图。

 源码(matlab):

function [t,em] = otsuthresh(counts)
%OTSUTHRESH Global histogram threshold using Otsu's method.
%   T = OTSUTHRESH(COUNTS) computes a global threshold from histogram
%   counts COUNTS that minimizes the intraclass variance for a bimodal
%   histogram. T is a normalized intensity value that lies in the range [0,
%   1] and can be used with IMBINARIZE to convert an intensity image to a
%   binary image.
%
%   [T, EM] = OTSUTHRESH(COUNTS) returns effectiveness metric, EM, as the
%   second output argument. It indicates the effectiveness of thresholding
%   using threshold T and is in the range [0, 1]. The lower bound is
%   attainable only by histogram counts with all data in a single non-zero
%   bin. The upper bound is attainable only by histogram counts with
%   two non-zero bins.
%
%   Class Support
%   -------------
%   The histogram counts COUNTS must be a real, non-sparse, numeric vector.
%
%   Example 
%   -------
%  % This example shows how to compute a threshold from an image histogram
%  % and binarize the image .
%
%   % Read an image of coins and compute a 16-bin histogram.
%   I = imread('coins.png');
%   counts = imhist(I, 16);
%
%   % Compute a global threshold using the histogram counts.
%   T = otsuthresh(counts);
%
%   % Binarize image using computed threshold.
%   BW = imbinarize(I,T);
%
%   figure, imshow(BW)
%
%   See also IMBINARIZE, GRAYTHRESH.

% Copyright 2015-2018 The MathWorks, Inc.

validateattributes(counts, {'numeric'}, {'real','nonsparse','vector','nonnegative','finite'}, mfilename, 'COUNTS');

num_bins = numel(counts);

% Make counts a double column vector
counts = double( counts(:) );

% Variables names are chosen to be similar to the formulas in
% the Otsu paper.
p = counts / sum(counts);
omega = cumsum(p);
mu = cumsum(p .* (1:num_bins)');
mu_t = mu(end);

sigma_b_squared = (mu_t * omega - mu).^2 ./ (omega .* (1 - omega));

% Find the location of the maximum value of sigma_b_squared.
% The maximum may extend over several bins, so average together the
% locations.  If maxval is NaN, meaning that sigma_b_squared is all NaN,
% then return 0.
maxval = max(sigma_b_squared);
isfinite_maxval = isfinite(maxval);
if isfinite_maxval
    idx = mean(find(sigma_b_squared == maxval));
    % Normalize the threshold to the range [0, 1].
    t = (idx - 1) / (num_bins - 1);
else
    t = 0.0;
end

% compute the effectiveness metric
if nargout > 1
    if isfinite_maxval
        em = maxval/(sum(p.*((1:num_bins).^2)') - mu_t^2);
    else
        em = 0;
    end
end

end

  • 2
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超翔之逸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值