• 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

  • 相关阅读:
    通讯录的实现
    CSAPP深入理解计算机系统-笔记1
    创造者设计模式
    React Native优质开源项目精选
    LeetCode每日一题——1684. 统计一致字符串的数目
    使用 MobileNet和ImageHash做图片相似度匹配(以图搜图)
    记录paddlepaddle-gpu安装
    【工程实践】Docker使用记录
    波浪的柱子
    ENVI:如何对自带GLT表的图像进行几何校正?
  • 原文地址:https://blog.csdn.net/Protinx/article/details/127574112