日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Otsu阈值分割详解

發布時間:2024/8/1 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Otsu阈值分割详解的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。