public static byte[] ZFTIamgeStreatcher(byte[] imageBytes, int maxGray, int minGray) { int length = imageBytes.Length; byte[] newImageBytes = new byte[length]; Histogram his = new Histogram(imageBytes); HistogramValue[] hisv = his.GetHistogramValue(); int fj = hisv.Length; if (maxGray - minGray < fj) { return(null); } HistogramValue[] hisv1 = new HistogramValue[hisv.Length]; hisv1 = GetPLMDandPaixuHisValue(hisv); ZFTJH[] zftjh = new ZFTJH[fj]; int jiange = (maxGray - minGray) / fj; double jiangemd = 1.0 / fj; ZFTJH z = new ZFTJH(); zftjh[0] = z; zftjh[0].Gray = (byte)minGray; zftjh[0].Ljmd = jiangemd; for (int i = 1; i < fj - 1; i++) { z = new ZFTJH(); zftjh[i] = z; zftjh[i].Gray = (byte)(minGray + i * jiange); zftjh[i].Ljmd = (i + 1) * jiangemd; } if (fj > 2) { z = new ZFTJH(); zftjh[fj - 1] = z; zftjh[fj - 1].Gray = (byte)maxGray; zftjh[fj - 1].Ljmd = 1; } Dictionary <byte, byte> dic = ZFTStreatcher(hisv, zftjh); for (int i = 0; i < length; i++) { byte b = imageBytes[i]; newImageBytes[i] = dic[imageBytes[i]]; } return(newImageBytes); }
/// <summary> /// 得到每个像素值以及相对应的频率及频数 /// </summary> /// <param name="bytes">图像数据</param> /// <returns></returns> private List <HistogramValue> GetHistorgramCountandProbability(byte[] bytes) { int Length = bytes.Length; List <HistogramValue> hiss = new List <HistogramValue>(); HistogramValue his = new HistogramValue(bytes[0]); his.Count++; hiss.Add(his); for (int i = 1; i < Length; i++) { bool NoAdd = true; foreach (HistogramValue item in hiss) { if (item.Value == bytes[i]) { item.Count++; NoAdd = false; continue; } } if (NoAdd) { HistogramValue h = new HistogramValue(bytes[i]); h.Count++; hiss.Add(h); NoAdd = true; } } foreach (var item in hiss) { item.Probability = (item.Count * 1.0) / Length; } return(hiss); }
/// <summary> /// 对每个灰度级进行排序并得到其累计频率 /// </summary> /// <param name="hisv">在直方图中的灰度级类</param> /// <returns></returns> private static HistogramValue[] GetPLMDandPaixuHisValue(HistogramValue[] hisv) { int length = hisv.Length; double LJPL = 0;//累计频率 for (int i = 0; i < length; i++) { for (int j = i + 1; j < length; j++) { if (hisv[i].Value > hisv[j].Value)//利用冒泡排序对灰度级进行从小到大排序 { HistogramValue h = hisv[i]; hisv[i] = hisv[j]; hisv[j] = h; } } } for (int i = 0; i < length; i++) { LJPL += hisv[i].Probability;//对排好序的灰度级频率累加,得到累计频率 hisv[i].Plmd = LJPL; } return(hisv); }