public void GenerateWL() { if (histo_data == null || histo_data.HistogramData == null) { return; } WL_BMP = new Bitmap(histo_width, histo_height); Graphics gp = Graphics.FromImage(WL_BMP); Point p = HisLUT.GetCminCmax(histo_data.windowCenter, histo_data.windowWidth); int curMin = GetValuePosition(p.X); int curMax = GetValuePosition(p.Y); int maxX = WL_BMP.Width - 1; if (curMin < 0) { curMin = 0; } else if (curMin > maxX) { curMin = maxX; } if (curMax < 0) { curMax = 0; } else if (curMax > maxX) { curMax = maxX; } if (curMax < curMin) { curMax = curMin; } Pen WL_pen = new Pen(WL_Color); Point p_left_down = new Point(curMin, WL_BMP.Height); Point p_left_up = new Point(curMin, 0); Point p_right_down = new Point(curMax, WL_BMP.Height); Point p_right_up = new Point(curMax, 0); Point p_mid_up = new Point((curMin + curMax) / 2, 0); Point p_mid_down = new Point((curMin + curMax) / 2, WL_BMP.Height); gp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias; gp.DrawLine(WL_pen, p_left_down, p_left_up); gp.DrawLine(WL_pen, p_left_down, p_right_up); gp.DrawLine(WL_pen, p_right_down, p_right_up); gp.DrawLine(WL_pen, p_mid_up, p_mid_down); gp.Dispose(); }
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)); }
public void MouseDown(PictureBox picturebox, MouseEventArgs e) { if (histo_data == null || histo_data.HistogramData == null) { return; } Point p = new Point(e.X - startPoint_ref.X, e.Y - startPoint_ref.Y); if (p.Y > histo_height) { return; } Point p_minMax = HisLUT.GetCminCmax(histo_data.windowCenter, histo_data.windowWidth); int curMin = GetValuePosition(p_minMax.X); int curMax = GetValuePosition(p_minMax.Y); if (Math.Abs(p.X - curMin) < 5 && p.Y < histo_height) { breakPoint.Y = 1; } else if (((p.X - curMin) > 5) && ((curMax - p.X) > 5) && p.Y < histo_height) { breakPoint.Y = 2; } else if (Math.Abs(p.X - curMax) < 5 && p.Y < histo_height) { breakPoint.Y = 3; } else { breakPoint.Y = 0; } breakPoint.X = p.X; }
/// 静态函数 /// 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); }
public void MouseMove(PictureBox picturebox, MouseEventArgs e) { if (histo_data == null || histo_data.HistogramData == null) { return; } Point p = new Point(e.X - startPoint_ref.X, e.Y - startPoint_ref.Y); if (p.Y > histo_height) { return; } Point p_minMax = HisLUT.GetCminCmax(histo_data.windowCenter, histo_data.windowWidth); int curMin = GetValuePosition(p_minMax.X); int curMax = GetValuePosition(p_minMax.Y); int error = 3; if (e.Button != MouseButtons.Left) { if (Math.Abs(p.X - curMin) < error && p.Y < histo_height) { picturebox.Cursor = Cursors.SizeWE; } else if (Math.Abs(p.X - curMax) < error && p.Y < histo_height) { picturebox.Cursor = Cursors.SizeWE;//TCursor.curRight; } else if (((p.X - curMin) >= error) && ((curMax - p.X) >= error) && p.Y < histo_height) { picturebox.Cursor = Cursors.Hand; } else { picturebox.Cursor = Cursors.Default; } } else if (breakPoint.Y != 0 && Math.Abs(p.X - breakPoint.X) > 5 && p.Y < histo_height) { int oldMinInHist = curMin; int oldMaxInHist = curMax; if (breakPoint.Y == 1) { curMin = p.X; breakPoint.X = p.X; } else if (breakPoint.Y == 3) { curMax = e.X; breakPoint.X = p.X; } else if (breakPoint.Y == 2) { curMax += (p.X - breakPoint.X); //加上偏移量 curMin += (p.X - breakPoint.X); if (curMin < 0 && p.X < (0 + curMax) / 2) { curMax -= (0 - curMin); } if (curMax > histo_width && p.X > (curMin + histo_width - 0 - 1) / 2) { curMin += (curMax - histo_width); } breakPoint.X = p.X; } if (Math.Abs(oldMinInHist - curMin) < 1 && Math.Abs(oldMaxInHist - curMax) < 1) { return; } int minV = GetPositionValue(curMin); int maxV = GetPositionValue(curMax); if (minV > maxV) { int temp = maxV; maxV = minV; minV = temp; } histo_data.SetCenterWindow((maxV - minV) / 2 + minV, maxV - minV); picturebox.Image = new Bitmap(histo_width, histo_height); Graphics g = Graphics.FromImage(picturebox.Image); GenerateWL(); g.DrawImage(backBMP, new Point(0, 0)); g.DrawImage(WL_BMP, new Point(0, 0)); picturebox.Refresh(); //hist1.RefreshLUT(); //if (LUTChanged != null) //{ // LUTChanged(0, new Point(hist1.windowCenter, hist1.windowWidth), hist1.LUT); //} } }