public unsafe void GenerateImage() { if (imgLevel == 0 && imgWindow == 0) { Histogram_Data his = new Histogram_Data(); his.ComputeHistogram(ImageData); imgLevel = his.windowCenter; imgWindow = his.windowWidth; } Color[] LUT = null; HisLUT.RefreshLUT(ref LUT, null, false, imgLevel, imgWindow, 65536); BMP = SliceImageToBitmap24(ImageData, LUT); }
/// <summary> /// 获得初始的Lut /// </summary> private Color[] GetImageDataLut(ushort[,] data) { //int maxValueLimit = 65536; AverageHistogram his = new AverageHistogram(data); int HFMax = level + window / 2; int HFMin = level - window / 2; if (HFMax < 0 || HFMin < 0) { //level = his.winWidth / 2 + his.MinValue; //window = his.LenValue; level = his.WinCenter; window = his.WinWidth; } else { if (HFMax > his.MaxValue || HFMin > his.MaxValue) { //level = his.LenValue / 2 + his.MinValue; //window = his.LenValue; level = his.WinCenter; window = his.WinWidth; } } int maxvalue = his.LenValue < 256 ? 256 : his.LenValue; maxValue = maxvalue; minValue = his.MinValue; Color[] colorLUT = null; ImageCapturing.ColorMode[] colorModels = HistogramControl.ReadLUTColorModel(); for (int i = 0; i < colorModels.Length; i++) { if (colorModels[i].Name == colorModeName) { colorLUT = colorModels[i].colorLUT; break; } } return(HisLUT.RefreshLUT(colorLUT, converse, level, window, maxvalue, minValue)); }
/// 静态函数 /// private static List <PointF> ComputeDefaultROIPoints(ushort[,] imgData) { ushort[,] imagedataTemp = (ushort[, ])imgData.Clone(); int[] rdata = LunImage.FindMaxAndMin(imagedataTemp); int maxValue = rdata[0]; int minValue = rdata[1]; int lenValue = maxValue - minValue + 1; int[] histogramData = new int[lenValue]; foreach (int da in imgData) { histogramData[da - minValue]++; } double sum = 0; double csum = 0.0; int n = 0; int thresholdValue = 0; for (int k = 0; k < lenValue; k++) { sum += (double)k * (double)histogramData[k]; /* x*f(x) 质量矩*/ n += histogramData[k]; /* f(x) 质量 */ } if (n <= 0) { // if n has no value, there is problems... return(new List <PointF>()); } // do the otsu global thresholding method double fmax = -1.0; int n1 = 0; int n2 = 0; double m1, m2 = 0; for (int k = 0; k < lenValue; k++) { n1 += histogramData[k]; if (n1 <= 0) { continue; } n2 = n - n1; if (n2 == 0) { break; } csum += (double)k * histogramData[k]; m1 = csum / n1; m2 = (sum - csum) / n2; double sb = (double)n1 * (double)n2 * (m1 - m2) * (m1 - m2); /* bbg: note: can be optimized. */ if (sb > fmax) { fmax = sb; thresholdValue = k; } } for (int i = 0; i < imagedataTemp.GetLength(0); i++) { for (int j = 0; j < imagedataTemp.GetLength(1); j++) { if (imagedataTemp[i, j] < thresholdValue) { imagedataTemp[i, j] = 0; } else { imagedataTemp[i, j] = (ushort)thresholdValue; } } } int level = thresholdValue / 2; int window = thresholdValue / 5; Histogram his = new Histogram(imagedataTemp); int HFMax = level + window / 2; int HFMin = level - window / 2; if (HFMax < 0 || HFMin < 0) { level = his.WinCenter; window = his.WinWidth; } else { if (HFMax > his.MaxValue || HFMin > his.MaxValue) { level = his.WinCenter; window = his.WinWidth; } } int maxvalue = his.LenValue < 256 ? 256 : his.LenValue; maxValue = maxvalue; minValue = his.MinValue; Color[] LUT = HisLUT.RefreshLUT(null, false, level, window, maxvalue, minValue); Bitmap BMP = TypeConvert.SliceImageToBitmap24(imagedataTemp, LUT, minValue); float x1 = 0; float y1 = 0; float x2 = BMP.Width - 1; float y2 = BMP.Height - 1; BoundaryTracker bt = new BoundaryTracker(); bt.GetSerializedBoundary(BMP, LUT[0], new Rectangle((int)x1, (int)y1, (int)(x2 - x1 + 1), (int)(y2 - y1 + 1)), false); List <PointF> temp = new List <PointF>(); if (bt.MaxPointIdx != -1) { PointF[] tmp = bt.CL[bt.MaxPointIdx]; if (tmp.Length > 3) { for (int i = 0; i < tmp.Length; i++) { temp.Add(tmp[i]); } } } else { temp.Add(new PointF(0, 0)); temp.Add(new PointF(0, BMP.Height - 1)); temp.Add(new PointF(BMP.Width - 1, BMP.Height - 1)); temp.Add(new PointF(BMP.Width - 1, 0)); } return(temp); }