namespace Splash
{
/// <summary>
/// 圖像處理
/// </summary>
public static partial class Binarize
{
/// <summary>
/// 大津法計算阈值
/// </summary>
/// <param name=
/// <returns>二值化阈值</returns>
public static Int
{ // 建立統計直方圖
Int
Array
foreach (Byte b in grayArray)
{
Histogram[b]++; // 統計直方圖
}
// 總的質量矩和圖像點數
Int
Double SumU =
for (Int
{
SumU += i * Histogram[i]; // 總的質量矩
}
// 灰度區間
Int
Int
// 計算最大類間方差
Int
Double MaxVariance =
Double U
Int
for (Int
{
if (Histogram[i] ==
// 目標的質量矩和點數
U
C
// 計算目標和背景的類間方差
Double Diference = U
Double Variance = Diference * Diference / C
if (Variance > MaxVariance)
{
MaxVariance = Variance;
Threshold = i;
}
}
// 返回類間方差最大阈值
return Threshold;
}
/// <summary>
/// 檢測非零值
/// </summary>
/// <param name=
/// <returns>
/// true:非零
/// false:零
/// </returns>
private static Boolean NonZero(Int
{
return (value !=
}
}
}
From:http://tw.wingwit.com/Article/program/net/201311/13006.html