http://www.cnblogs.com/carekee/articles/3643394.htmlphp
图像二值化的目的是最大限度的将图象中感兴趣的部分保留下来,在不少状况下,也是进行图像分析、特征提取与模式识别以前的必要的图像预处理过程。这个看似简单的问题,在过去的四十年里受到国内外学者的普遍关注,产生了数以百计的阈值选取方法,但如同其余图像分割算法同样,没有一个现有方法对各类各样的图像都能获得使人满意的结果。html
在这些庞大的分类方法中,基于直方图的全局二值算法占有了绝对的市场份额,这些算法都从不一样的科学层次提出了各自的实施方案,而且这类方法都有着一些共同的特色:算法
一、简单;网络
二、算法容易实现;app
三、执行速度快。jsp
本文摘取了若干种这类方法进行了介绍。ide
一:灰度平局值值法:函数
一、描述:即便用整幅图像的灰度平均值做为二值化的阈值,通常该方法可做为其余方法的初始猜测值。post
二、原理: this
三、实现代码:
public static int GetMeanThreshold(int[] HistGram) { int Sum = 0, Amount = 0; for (int Y = 0; Y < 256; Y++) { Amount += HistGram[Y]; Sum += Y * HistGram[Y]; } return Sum / Amount; }
2、百分比阈值(P-Tile法)
一、描述
Doyle于1962年提出的P-Tile (即P分位数法)能够说是最古老的一种阈值选取方法。该方法根据先验几率来设定阈值,使得二值化后的目标或背景像素比例等于先验几率,该方法简单高效,可是对于先验几率难于估计的图像却无能为力。
二、该原理比较简单,直接以代码实现。
/// <summary> /// 百分比阈值 /// </summary> /// <param name="HistGram">灰度图像的直方图</param> /// <param name="Tile">背景在图像中所占的面积百分比</param> /// <returns></returns> public static int GetPTileThreshold(int[] HistGram, int Tile = 50) { int Y, Amount = 0, Sum = 0; for (Y = 0; Y < 256; Y++) Amount += HistGram[Y]; // 像素总数 for (Y = 0; Y < 256; Y++) { Sum = Sum + HistGram[Y]; if (Sum >= Amount * Tile / 100) return Y; } return -1; }
3、基于谷底最小值的阈值
一、描述:
此方法实用于具备明显双峰直方图的图像,其寻找双峰的谷底做为阈值,可是该方法不必定能得到阈值,对于那些具备平坦的直方图或单峰图像,该方法不合适。
二、实现过程:
该函数的实现是一个迭代的过程,每次处理前对直方图数据进行判断,看其是否已是一个双峰的直方图,若是不是,则对直方图数据进行半径为1(窗口大小为3)的平滑,若是迭代了必定的数量好比1000次后仍未得到一个双峰的直方图,则函数执行失败,如成功得到,则最终阈值取两个双峰之间的谷底值做为阈值。
注意在编码过程当中,平滑的处理须要当前像素以前的信息,所以须要对平滑前的数据进行一个备份。另外,首数据类型精度限制,不该用整形的直方图数据,必须转换为浮点类型数据来进行处理,不然得不到正确的结果。
该算法相关参考论文以下:
J. M. S. Prewitt and M. L. Mendelsohn, "The analysis of cell images," innnals of the New York Academy of Sciences, vol. 128, pp. 1035-1053, 1966.
C. A. Glasbey, "An analysis of histogram-based thresholding algorithms," CVGIP: Graphical Models and Image Processing, vol. 55, pp. 532-537, 1993.
三、实现代码:
public static int GetMinimumThreshold(int[] HistGram) { int Y, Iter = 0; double[] HistGramC = new double[256]; // 基于精度问题,必定要用浮点数来处理,不然得不到正确的结果 double[] HistGramCC = new double[256]; // 求均值的过程会破坏前面的数据,所以须要两份数据 for (Y = 0; Y < 256; Y++) { HistGramC[Y] = HistGram[Y]; HistGramCC[Y] = HistGram[Y]; } // 经过三点求均值来平滑直方图 while (IsDimodal(HistGramCC) == false) // 判断是否已是双峰的图像了 { HistGramCC[0] = (HistGramC[0] + HistGramC[0] + HistGramC[1]) / 3; // 第一点 for (Y = 1; Y < 255; Y++) HistGramCC[Y] = (HistGramC[Y - 1] + HistGramC[Y] + HistGramC[Y + 1]) / 3; // 中间的点 HistGramCC[255] = (HistGramC[254] + HistGramC[255] + HistGramC[255]) / 3; // 最后一点 System.Buffer.BlockCopy(HistGramCC, 0, HistGramC, 0, 256 * sizeof(double)); Iter++; if (Iter >= 1000) return -1; // 直方图没法平滑为双峰的,返回错误代码 } // 阈值极为两峰之间的最小值 bool Peakfound = false; for (Y = 1; Y < 255; Y++) { if (HistGramCC[Y - 1] < HistGramCC[Y] && HistGramCC[Y + 1] < HistGramCC[Y]) Peakfound = true; if (Peakfound == true && HistGramCC[Y - 1] >= HistGramCC[Y] && HistGramCC[Y + 1] >= HistGramCC[Y]) return Y - 1; } return -1; }
其中IsDimodal函数为判断直方图是不是双峰的函数,代码以下:
private static bool IsDimodal(double[] HistGram) // 检测直方图是否为双峰的 { // 对直方图的峰进行计数,只有峰数位2才为双峰 int Count = 0; for (int Y = 1; Y < 255; Y++) { if (HistGram[Y - 1] < HistGram[Y] && HistGram[Y + 1] < HistGram[Y]) { Count++; if (Count > 2) return false; } } if (Count == 2) return true; else return false; }
四、效果:
原图 二值图 原始直方图 平滑后的直方图
对于这种有较明显的双峰的图像,该算法仍是能取得不错的效果的。
4、基于双峰平均值的阈值
一、描述:
该算法和基于谷底最小值的阈值方法相似,只是最后一步不是取得双峰之间的谷底值,而是取双峰的平均值做为阈值。
二、参考代码:
public static int GetIntermodesThreshold(int[] HistGram) { int Y, Iter = 0, Index; double[] HistGramC = new double[256]; // 基于精度问题,必定要用浮点数来处理,不然得不到正确的结果 double[] HistGramCC = new double[256]; // 求均值的过程会破坏前面的数据,所以须要两份数据 for (Y = 0; Y < 256; Y++) { HistGramC[Y] = HistGram[Y]; HistGramCC[Y] = HistGram[Y]; } // 经过三点求均值来平滑直方图 while (IsDimodal(HistGramCC) == false) // 判断是否已是双峰的图像了 { HistGramCC[0] = (HistGramC[0] + HistGramC[0] + HistGramC[1]) / 3; // 第一点 for (Y = 1; Y < 255; Y++) HistGramCC[Y] = (HistGramC[Y - 1] + HistGramC[Y] + HistGramC[Y + 1]) / 3; // 中间的点 HistGramCC[255] = (HistGramC[254] + HistGramC[255] + HistGramC[255]) / 3; // 最后一点 System.Buffer.BlockCopy(HistGramCC, 0, HistGramC, 0, 256 * sizeof(double)); // 备份数据,为下一次迭代作准备 Iter++; if (Iter >= 10000) return -1; // 彷佛直方图没法平滑为双峰的,返回错误代码 } // 阈值为两峰值的平均值 int[] Peak = new int[2]; for (Y = 1, Index = 0; Y < 255; Y++) if (HistGramCC[Y - 1] < HistGramCC[Y] && HistGramCC[Y + 1] < HistGramCC[Y]) Peak[Index++] = Y - 1; return ((Peak[0] + Peak[1]) / 2); }
三、效果:
原图 二值图 原始直方图 平滑后的直方图
5、迭代最佳阈值
一、描述:
该算法先假定一个阈值,而后计算在该阈值下的前景和背景的中心值,当前景和背景中心值得平均值和假定的阈值相同时,则迭代停止,并以此值为阈值进行二值化。
二、实现过程:
(1)求出图象的最大灰度值和最小灰度值,分别记为gl和gu,令初始阈值为:
(2) 根据阈值T0将图象分割为前景和背景,分别求出二者的平均灰度值Ab和Af:
(3) 令
若是Tk=Tk+1,则取Tk为所求得的阈值,不然,转2继续迭代。
三、参考代码:
public static int GetIterativeBestThreshold(int[] HistGram) { int X, Iter = 0; int MeanValueOne, MeanValueTwo, SumOne, SumTwo, SumIntegralOne, SumIntegralTwo; int MinValue, MaxValue; int Threshold, NewThreshold; for (MinValue = 0; MinValue < 256 && HistGram[MinValue] == 0; MinValue++) ; for (MaxValue = 255; MaxValue > MinValue && HistGram[MinValue] == 0; MaxValue--) ; if (MaxValue == MinValue) return MaxValue; // 图像中只有一个颜色 if (MinValue + 1 == MaxValue) return MinValue; // 图像中只有二个颜色 Threshold = MinValue; NewThreshold = (MaxValue + MinValue) >> 1; while (Threshold != NewThreshold) // 当先后两次迭代的得到阈值相同时,结束迭代 { SumOne = 0; SumIntegralOne = 0; SumTwo = 0; SumIntegralTwo = 0; Threshold = NewThreshold; for (X = MinValue; X <= Threshold; X++) //根据阈值将图像分割成目标和背景两部分,求出两部分的平均灰度值 { SumIntegralOne += HistGram[X] * X; SumOne += HistGram[X]; } MeanValueOne = SumIntegralOne / SumOne; for (X = Threshold + 1; X <= MaxValue; X++) { SumIntegralTwo += HistGram[X] * X; SumTwo += HistGram[X]; } MeanValueTwo = SumIntegralTwo / SumTwo; NewThreshold = (MeanValueOne + MeanValueTwo) >> 1; //求出新的阈值 Iter++; if (Iter >= 1000) return -1; } return Threshold; }
四、效果:
原图 二值图 直方图
6、OSTU大律法
一、描述:
该算法是1979年由日本大津提出的,主要是思想是取某个阈值,使得前景和背景两类的类间方差最大,matlab中的graythresh便是以该算法为原理执行的。
二、原理:
关于该算法的原理,网络上有不少,这里为了篇幅有限,不加以赘述。
三、参考代码:
public static int GetOSTUThreshold(int[] HistGram) { int X, Y, Amount = 0; int PixelBack = 0, PixelFore = 0, PixelIntegralBack = 0, PixelIntegralFore = 0, PixelIntegral = 0; double OmegaBack, OmegaFore, MicroBack, MicroFore, SigmaB, Sigma; // 类间方差; int MinValue, MaxValue; int Threshold = 0; for (MinValue = 0; MinValue < 256 && HistGram[MinValue] == 0; MinValue++) ; for (MaxValue = 255; MaxValue > MinValue && HistGram[MinValue] == 0; MaxValue--) ; if (MaxValue == MinValue) return MaxValue; // 图像中只有一个颜色 if (MinValue + 1 == MaxValue) return MinValue; // 图像中只有二个颜色 for (Y = MinValue; Y <= MaxValue; Y++) Amount += HistGram[Y]; // 像素总数 PixelIntegral = 0; for (Y = MinValue; Y <= MaxValue; Y++) PixelIntegral += HistGram[Y] * Y; SigmaB = -1; for (Y = MinValue; Y < MaxValue; Y++) { PixelBack = PixelBack + HistGram[Y]; PixelFore = Amount - PixelBack; OmegaBack = (double)PixelBack / Amount; OmegaFore = (double)PixelFore / Amount; PixelIntegralBack += HistGram[Y] * Y; PixelIntegralFore = PixelIntegral - PixelIntegralBack; MicroBack = (double)PixelIntegralBack / PixelBack; MicroFore = (double)PixelIntegralFore / PixelFore; Sigma = OmegaBack * OmegaFore * (MicroBack - MicroFore) * (MicroBack - MicroFore); if (Sigma > SigmaB) { SigmaB = Sigma; Threshold = Y; } } return Threshold; }
四、效果:
该算法对于那些具备平坦的直方图的图像具备必定的适应能力。
七、一维最大熵
一、描述:
该算法把信息论中熵的概念引入到图像中,经过计算阈值分割后两部分熵的和来判断阈值是否为最佳阈值。
二、算法原理
这方面的文章也比较多,留给读者自行去查找相关资料。
三、参考代码:
public static int Get1DMaxEntropyThreshold(int[] HistGram) { int X, Y,Amount=0; double[] HistGramD = new double[256]; double SumIntegral, EntropyBack, EntropyFore, MaxEntropy; int MinValue = 255, MaxValue = 0; int Threshold = 0; for (MinValue = 0; MinValue < 256 && HistGram[MinValue] == 0; MinValue++) ; for (MaxValue = 255; MaxValue > MinValue && HistGram[MinValue] == 0; MaxValue--) ; if (MaxValue == MinValue) return MaxValue; // 图像中只有一个颜色 if (MinValue + 1 == MaxValue) return MinValue; // 图像中只有二个颜色 for (Y = MinValue; Y <= MaxValue; Y++) Amount += HistGram[Y]; // 像素总数 for (Y = MinValue; Y <= MaxValue; Y++) HistGramD[Y] = (double)HistGram[Y] / Amount+1e-17; MaxEntropy = double.MinValue; ; for (Y = MinValue + 1; Y < MaxValue; Y++) { SumIntegral = 0; for (X = MinValue; X <= Y; X++) SumIntegral += HistGramD[X]; EntropyBack = 0; for (X = MinValue; X <= Y; X++) EntropyBack += (-HistGramD[X] / SumIntegral * Math.Log(HistGramD[X] / SumIntegral)); EntropyFore = 0; for (X = Y + 1; X <= MaxValue; X++) EntropyFore += (-HistGramD[X] / (1 - SumIntegral) * Math.Log(HistGramD[X] / (1 - SumIntegral))); if (MaxEntropy < EntropyBack + EntropyFore) { Threshold = Y; MaxEntropy = EntropyBack + EntropyFore; } } return Threshold; }
8、力矩保持法
一、描述:
该算法经过选择恰当的阈值从而使得二值后的图像和原始的灰度图像具备三个相同的初始力矩值。
二、原理:
参考论文:W. Tsai, “Moment-preserving thresholding: a new approach,” Comput.Vision Graphics Image Process., vol. 29, pp. 377-393, 1985.
因为没法下载到该论文(收费的),仅仅给出从其余一些资料中找到的公式共享一下。
其中的A\B\C的函数可见代码部分。
三、参考代码:
public static byte GetMomentPreservingThreshold(int[] HistGram) { int X, Y, Index = 0, Amount=0; double[] Avec = new double[256]; double X2, X1, X0, Min; for (Y = 0; Y <= 255; Y++) Amount += HistGram[Y]; // 像素总数 for (Y = 0; Y < 256; Y++) Avec[Y] = (double)A(HistGram, Y) / Amount; // The threshold is chosen such that A(y,t)/A(y,n) is closest to x0. // The following finds x0. X2 = (double)(B(HistGram, 255) * C(HistGram, 255) - A(HistGram, 255) * D(HistGram, 255)) / (double)(A(HistGram, 255) * C(HistGram, 255) - B(HistGram, 255) * B(HistGram, 255)); X1 = (double)(B(HistGram, 255) * D(HistGram, 255) - C(HistGram, 255) * C(HistGram, 255)) / (double)(A(HistGram, 255) * C(HistGram, 255) - B(HistGram, 255) * B(HistGram, 255)); X0 = 0.5 - (B(HistGram, 255) / A(HistGram, 255) + X2 / 2) / Math.Sqrt(X2 * X2 - 4 * X1); for (Y = 0, Min = double.MaxValue; Y < 256; Y++) { if (Math.Abs(Avec[Y] - X0) < Min) { Min = Math.Abs(Avec[Y] - X0); Index = Y; } } return (byte)Index; } private static double A(int[] HistGram, int Index) { double Sum = 0; for (int Y = 0; Y <= Index; Y++) Sum += HistGram[Y]; return Sum; } private static double B(int[] HistGram, int Index) { double Sum = 0; for (int Y = 0; Y <= Index; Y++) Sum += (double)Y * HistGram[Y]; return Sum; } private static double C(int[] HistGram, int Index) { double Sum = 0; for (int Y = 0; Y <= Index; Y++) Sum += (double)Y * Y * HistGram[Y]; return Sum; } private static double D(int[] HistGram, int Index) { double Sum = 0; for (int Y = 0; Y <= Index; Y++) Sum += (double)Y * Y * Y * HistGram[Y]; return Sum; }
对于不少图像,该算法页能取得比较满意的结果。
9、基于模糊集理论的阈值
该算法的具体分析可见:基于模糊集理论的一种图像二值化算法的原理、实现效果及代码
此法也借用香农熵的概念,该算法通常都能得到较为理想的分割效果,不论是对双峰的仍是单峰的图像。
10、Kittler最小错误分类法
因为精力有限,如下几种算法仅仅给出算法的论文及相关的代码。
该算法具体的分析见:
参考代码:
public static int GetKittlerMinError(int[] HistGram) { int X, Y; int MinValue, MaxValue; int Threshold ; int PixelBack, PixelFore; double OmegaBack, OmegaFore, MinSigma, Sigma, SigmaBack, SigmaFore; for (MinValue = 0; MinValue < 256 && HistGram[MinValue] == 0; MinValue++) ; for (MaxValue = 255; MaxValue > MinValue && HistGram[MinValue] == 0; MaxValue--) ; if (MaxValue == MinValue) return MaxValue; // 图像中只有一个颜色 if (MinValue + 1 == MaxValue) return MinValue; // 图像中只有二个颜色 Threshold = -1; MinSigma = 1E+20; for (Y = MinValue; Y < MaxValue; Y++) { PixelBack = 0; PixelFore = 0; OmegaBack = 0; OmegaFore = 0; for (X = MinValue; X <= Y; X++) { PixelBack += HistGram[X]; OmegaBack = OmegaBack + X * HistGram[X]; } for (X = Y + 1; X <= MaxValue; X++) { PixelFore += HistGram[X]; OmegaFore = OmegaFore + X * HistGram[X]; } OmegaBack = OmegaBack / PixelBack; OmegaFore = OmegaFore / PixelFore; SigmaBack = 0; SigmaFore = 0; for (X = MinValue; X <= Y; X++) SigmaBack = SigmaBack + (X - OmegaBack) * (X - OmegaBack) * HistGram[X]; for (X = Y + 1; X <= MaxValue; X++) SigmaFore = SigmaFore + (X - OmegaFore) * (X - OmegaFore) * HistGram[X]; if (SigmaBack == 0 || SigmaFore == 0) { if (Threshold == -1) Threshold = Y; } else { SigmaBack = Math.Sqrt(SigmaBack / PixelBack); SigmaFore = Math.Sqrt(SigmaFore / PixelFore); Sigma = 1 + 2 * (PixelBack * Math.Log(SigmaBack / PixelBack) + PixelFore * Math.Log(SigmaFore / PixelFore)); if (Sigma < MinSigma) { MinSigma = Sigma; Threshold = Y; } } } return Threshold; }
从实际的运行效果看,该算法并不很好。
十一:ISODATA(也叫作intermeans法)
参考论文:
参考代码(未作整理):
// Also called intermeans // Iterative procedure based on the isodata algorithm [T.W. Ridler, S. Calvard, Picture // thresholding using an iterative selection method, IEEE Trans. System, Man and // Cybernetics, SMC-8 (1978) 630-632.] // The procedure divides the image into objects and background by taking an initial threshold, // then the averages of the pixels at or below the threshold and pixels above are computed. // The averages of those two values are computed, the threshold is incremented and the // process is repeated until the threshold is larger than the composite average. That is, // threshold = (average background + average objects)/2 // The code in ImageJ that implements this function is the getAutoThreshold() method in the ImageProcessor class. // // From: Tim Morris (dtm@ap.co.umist.ac.uk) // Subject: Re: Thresholding method? // posted to sci.image.processing on 1996/06/24 // The algorithm implemented in NIH Image sets the threshold as that grey // value, G, for which the average of the averages of the grey values // below and above G is equal to G. It does this by initialising G to the // lowest sensible value and iterating: // L = the average grey value of pixels with intensities < G // H = the average grey value of pixels with intensities > G // is G = (L + H)/2? // yes => exit // no => increment G and repeat // // There is a discrepancy with IJ because they are slightly different methods public static int GetIsoDataThreshold(int[] HistGram) { int i, l, toth, totl, h, g = 0; for (i = 1; i < HistGram.Length; i++) { if (HistGram[i] > 0) { g = i + 1; break; } } while (true) { l = 0; totl = 0; for (i = 0; i < g; i++) { totl = totl + HistGram[i]; l = l + (HistGram[i] * i); } h = 0; toth = 0; for (i = g + 1; i < HistGram.Length; i++) { toth += HistGram[i]; h += (HistGram[i] * i); } if (totl > 0 && toth > 0) { l /= totl; h /= toth; if (g == (int)Math.Round((l + h) / 2.0)) break; } g++; if (g > HistGram.Length - 2) { return 0; } } return g; }
12、Shanbhag 法
参考论文:
Shanbhag, Abhijit G. (1994), "Utilization of information measure as a means of image thresholding", Graph. Models Image Process. (Academic Press, Inc.) 56 (5): 414--419, ISSN 1049-9652, DOI 10.1006/cgip.1994.1037
参考代码(未整理):
public static int GetShanbhagThreshold(int[] HistGram) { int threshold; int ih, it; int first_bin; int last_bin; double term; double tot_ent; /* total entropy */ double min_ent; /* max entropy */ double ent_back; /* entropy of the background pixels at a given threshold */ double ent_obj; /* entropy of the object pixels at a given threshold */ double[] norm_histo = new double[HistGram.Length]; /* normalized histogram */ double[] P1 = new double[HistGram.Length]; /* cumulative normalized histogram */ double[] P2 = new double[HistGram.Length]; int total = 0; for (ih = 0; ih < HistGram.Length; ih++) total += HistGram[ih]; for (ih = 0; ih < HistGram.Length; ih++) norm_histo[ih] = (double)HistGram[ih] / total; P1[0] = norm_histo[0]; P2[0] = 1.0 - P1[0]; for (ih = 1; ih < HistGram.Length; ih++) { P1[ih] = P1[ih - 1] + norm_histo[ih]; P2[ih] = 1.0 - P1[ih]; } /* Determine the first non-zero bin */ first_bin = 0; for (ih = 0; ih < HistGram.Length; ih++) { if (!(Math.Abs(P1[ih]) < 2.220446049250313E-16)) { first_bin = ih; break; } } /* Determine the last non-zero bin */ last_bin = HistGram.Length - 1; for (ih = HistGram.Length - 1; ih >= first_bin; ih--) { if (!(Math.Abs(P2[ih]) < 2.220446049250313E-16)) { last_bin = ih; break; } } // Calculate the total entropy each gray-level // and find the threshold that maximizes it threshold = -1; min_ent = Double.MaxValue; for (it = first_bin; it <= last_bin; it++) { /* Entropy of the background pixels */ ent_back = 0.0; term = 0.5 / P1[it]; for (ih = 1; ih <= it; ih++) { //0+1? ent_back -= norm_histo[ih] * Math.Log(1.0 - term * P1[ih - 1]); } ent_back *= term; /* Entropy of the object pixels */ ent_obj = 0.0; term = 0.5 / P2[it]; for (ih = it + 1; ih < HistGram.Length; ih++) { ent_obj -= norm_histo[ih] * Math.Log(1.0 - term * P2[ih]); } ent_obj *= term; /* Total entropy */ tot_ent = Math.Abs(ent_back - ent_obj); if (tot_ent < min_ent) { min_ent = tot_ent; threshold = it; } } return threshold; }
十3、Yen法
参考论文:
1) Yen J.C., Chang F.J., and Chang S. (1995) "A New Criterion for Automatic Multilevel Thresholding" IEEE Trans. on Image Processing, 4(3): 370-378
2) Sezgin M. and Sankur B. (2004) "Survey over Image Thresholding Techniques and Quantitative Performance Evaluation" Journal of Electronic Imaging, 13(1): 146-165
参考代码(未整理):
// M. Emre Celebi // 06.15.2007 // Ported to ImageJ plugin by G.Landini from E Celebi's fourier_0.8 routines public static int GetYenThreshold(int[] HistGram) { int threshold; int ih, it; double crit; double max_crit; double[] norm_histo = new double[HistGram.Length]; /* normalized histogram */ double[] P1 = new double[HistGram.Length]; /* cumulative normalized histogram */ double[] P1_sq = new double[HistGram.Length]; double[] P2_sq = new double[HistGram.Length]; int total = 0; for (ih = 0; ih < HistGram.Length; ih++) total += HistGram[ih]; for (ih = 0; ih < HistGram.Length; ih++) norm_histo[ih] = (double)HistGram[ih] / total; P1[0] = norm_histo[0]; for (ih = 1; ih < HistGram.Length; ih++) P1[ih] = P1[ih - 1] + norm_histo[ih]; P1_sq[0] = norm_histo[0] * norm_histo[0]; for (ih = 1; ih < HistGram.Length; ih++) P1_sq[ih] = P1_sq[ih - 1] + norm_histo[ih] * norm_histo[ih]; P2_sq[HistGram.Length - 1] = 0.0; for (ih = HistGram.Length - 2; ih >= 0; ih--) P2_sq[ih] = P2_sq[ih + 1] + norm_histo[ih + 1] * norm_histo[ih + 1]; /* Find the threshold that maximizes the criterion */ threshold = -1; max_crit = Double.MinValue; for (it = 0; it < HistGram.Length; it++) { crit = -1.0 * ((P1_sq[it] * P2_sq[it]) > 0.0 ? Math.Log(P1_sq[it] * P2_sq[it]) : 0.0) + 2 * ((P1[it] * (1.0 - P1[it])) > 0.0 ? Math.Log(P1[it] * (1.0 - P1[it])) : 0.0); if (crit > max_crit) { max_crit = crit; threshold = it; } } return threshold; }
一行不少代码是摘自开源软件ImageJ的资料,读者也能够参考:http://fiji.sc/wiki/index.php/Auto_Threshold 这里得到更多的信息。
最后,我对这些算法的作了简单的UI界面,供有兴趣的读者参考。
工程代码下载:http://files.cnblogs.com/Imageshop/HistgramBinaryzation.rar
//图像的熵=========================================================================================
private void Menu_Entropy_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
//计算熵
double entropy = GetEntropy(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:"+curFileName +"\n\n"+"图像熵为 H = " + entropy);
}
}
//计算熵的函数-----------------------------------------------
public double GetEntropy(Bitmap bmp, int w, int h)
{
int g;
double H = 0.0;
int[] pix = new int[256];
Array.Clear(pix, 0, 256);
//获取各灰度值的像素个数
for (int i = 0; i < h; i++)
{
for (int j = 0; j < w; j++)
{
Color c = bmp.GetPixel(j, i);
g = (int)(0.299*c.R + 0.587*c.B + 0.114*c.G);
pix[g]++;
}
}
//计算熵
for (int i = 0; i < 256; i++)
if (pix[i] > 0)
H = H + pix[i] * Math.Log10(pix[i]);
H = Math.Log10(w * h) / Math.Log10(2) - H / (w * h * Math.Log10(2));
return H;
}
//灰度平均值=======================================================================================
private void Menu_GrayAverage_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
double aver = GetGrayAverage(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:" + curFileName +"\n\n"+ "灰度平均值为 A = " + aver);
}
}
//计算灰度平均值的函数---------------------------------------
public double GetGrayAverage(Bitmap bmp, int w, int h)
{
double sum = 0;
//计算平均值
for (int i = 0; i < w; i++)
for (int j = 0; j < h; j++)
{
Color c = bmp.GetPixel(i, j);
sum = sum + (int)(0.299 * c.R + 0.587 * c.B + 0.114 * c.G);
}
double aver = sum / (w * h);
return aver;
}
//灰度中值=========================================================================================
private void Menu_GrayMid_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
Bitmap bmp = curBitmap;
//由5 X 5的矩形获取值
int[] pix = new int[25];
for (int j = 0; j < 5; j++)
{
for (int i = 0; i < 5; i++)
{
pix[i + j * 5] = bmp.GetPixel(100 + i, 100 + j).B;
}
}
pix = MedianSorter(pix,25);
MessageBox.Show("图像:" + curFileName + "\n" +
"左上角坐标为(100,100)的5X5矩形块\n灰度中值为" +
" M = " + pix[12]);
}
}
//排序-------------------------------------------------------
public int[] MedianSorter(int[] data, int m)
{
int tem;
for (int i = m - 1; i >= 1; i--)
for (int j = 1; j <= i; j++)
if (data[j - 1] > data[j])
{
tem = data[j];
data[j] = data[j - 1];
data[j - 1] = tem;
}
return data;
}
//方差=============================================================================================
private void Menu_Variance_Click(object sender, EventArgs e)
{
if (curBitmap != null)
{
double vari = GetVariance(curBitmap, curBitmap.Width, curBitmap.Height);
MessageBox.Show("图像:" + curFileName + "\n\n" + "方差值 S = " + vari);
}
}
//计算方差值的函数-------------------------------------------
public double GetVariance(Bitmap bmp, int w, int h)
{
double aver, vari =0;
int data;
//求平均值
aver = GetGrayAverage(bmp ,w , h);
//求方差
for (int i = 0; i < w; i++)
{
for (int j = 0; j < h; j++)
{
Color c = bmp.GetPixel(i, j);
data = (int)(0.299 * c.R + 0.587 * c.B + 0.114 * c.G);
vari = vari + (data - aver) * (data - aver);
}
}
vari = vari / (w * h);
return vari;
}