Otsu阈值分割详解
?Otsu(大津法或最大類間方差法)使用的是聚類的思想,把圖像的灰度數(shù)按灰度級分成2個部分,使得兩個部分之間的灰度值差異最大,每個部分之間的灰度差異最小,通過方差的計算來尋找一個合適的灰度級別來劃分。 所以可以在二值化的時候采用otsu算法來自動選取閾值進行二值化。otsu算法被認為是圖像分割中閾值選取的最佳算法,計算簡單,不受圖像亮度和對比度的影響。因此,使類間方差最大的分割意味著錯分概率最小。
?
設(shè)t為設(shè)定的閾值。
圖像總平均灰度為:?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算法是對圖像的灰度級進行聚類,因此在執(zhí)行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
總結(jié)
以上是生活随笔為你收集整理的Otsu阈值分割详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: TortoiseSVN的安装和汉化包
- 下一篇: 汇编_指令_INC