protected override void HandleHistogram(double[] buffer, RasterQuickStatResult result, int cols) { int idx = 0; double std = 0; HistogramResult histogram = result.HistogramResult; double minValue = result.MinValue; double maxValue = result.MaxValue; double bin = histogram.Bin; double stddevValue = result.Stddev; double meanValue = result.MeanValue; int itemCount = histogram.Items.Length; if (bin != 0d) { for (int i = 0; i < cols; i++) { idx = (int)((buffer[i] - minValue) / bin); if (idx < 0 || idx > itemCount) { continue; } histogram.Items[idx]++; std = buffer[i] - meanValue; stddevValue += (std * std); } result.Stddev = stddevValue; } else { histogram.Items[0] = cols; } }
private void PrintHistogram(int bandNo, RasterQuickStatResult result) { StringBuilder sb = new StringBuilder(); sb.AppendLine(" -".PadRight(76, '-')); sb.AppendLine(" 波段 " + bandNo.ToString() + " 直方图:"); sb.AppendLine("间隔:".PadLeft(12) + result.HistogramResult.Bin.ToString("0.######")); HistogramResult histResult = result.HistogramResult; int buckets = histResult.ActualBuckets; sb.AppendLine("计数值".PadLeft(12) + "像元个数".PadLeft(11) + "累计像元个数".PadLeft(9) + "百分比".PadLeft(12) + "累计百分比".PadLeft(10)); double minValue = result.MinValue; double bin = histResult.Bin; long accCount = 0; double percent = 0; double accPercent = 0; if (bin != 0) { for (int i = 0; i < buckets; i++) { accCount += histResult.Items[i]; percent = 100 * histResult.Items[i] / (float)histResult.PixelCount; accPercent += percent; string sLine = (minValue + i * bin).ToString("0.######").PadLeft(15) + histResult.Items[i].ToString().PadLeft(15) + accCount.ToString().PadLeft(15) + percent.ToString("0.####").PadLeft(15) + accPercent.ToString("0.####").PadLeft(15); sb.AppendLine(sLine); } } else { accCount += histResult.Items[0]; percent = 100 * histResult.Items[0] / (float)histResult.PixelCount; accPercent += percent; string sLine = minValue.ToString("0.######").PadLeft(15) + histResult.Items[0].ToString().PadLeft(15) + accCount.ToString().PadLeft(15) + percent.ToString("0.####").PadLeft(15) + accPercent.ToString("0.####").PadLeft(15); sb.AppendLine(sLine); } richTextBox1.Text += sb.ToString(); }
public void Apply(string fileName, Dictionary <int, RasterQuickStatResult> results) { _curvePens = new Dictionary <int, Pen>(); _legendBurshes = new Dictionary <int, Brush>(); _fileName = fileName; _results = results; _minValue = double.MaxValue; _maxValue = double.MinValue; double minCount1, maxCount1; _minCount = double.MaxValue; _maxCount = double.MinValue; foreach (int bandNo in results.Keys) { RasterQuickStatResult restul = results[bandNo]; if (restul.MinValue < _minValue) { _minValue = restul.MinValue; } if (restul.MaxValue > _maxValue) { _maxValue = restul.MaxValue; } HistogramResult histResult = restul.HistogramResult; minCount1 = histResult.Items.Min(); maxCount1 = histResult.Items.Max(); if (minCount1 < _minCount) { _minCount = minCount1; } if (maxCount1 > _maxCount) { _maxCount = maxCount1; } // _curvePens.Add(bandNo, new Pen(GetRandomColor())); _legendBurshes.Add(bandNo, new SolidBrush(_curvePens[bandNo].Color)); } // ChangeBand(-1); }
void UCHistogramGrapCanvas_Paint(object sender, PaintEventArgs e) { e.Graphics.Clear(_fillColor); // e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; // SizeF fontSize = e.Graphics.MeasureString(_maxCount.ToString(), Font); int nCountChars = _maxCount.ToString().Length; _marginLeft = (int)Math.Ceiling(fontSize.Width) + 2; //draw fileName e.Graphics.DrawString("文件 : " + _fileName, Font, _axisBrush, new PointF(_marginLeft + 2, 4)); //draw axises e.Graphics.DrawRectangle(_axisPen, _marginLeft, _marginTop, this.Width - _marginLeft - _marginRight, this.Height - _marginTop - _marginBottom); //draw v axis double diffValue = (MaxCount - _minCount) / (float)_vScaleCount;//直方图坐标轴频率分段间隔 double v; float y = Height - _marginBottom; float y1; float dltY = fontSize.Height / 2f; float ySpan = (Height - _marginTop - _marginBottom) / (float)_vScaleCount; //隔ySpan绘制频率值 float y1Span = ySpan / _minorScaleCount; //隔y1Span绘制格线 float x = 0; for (int i = 0; i <= _vScaleCount; i++) { v = (int)(_minCount + diffValue * i); e.Graphics.DrawString(v.ToString().PadLeft(nCountChars), Font, _axisBrush, new PointF(x, y - dltY)); e.Graphics.DrawLine(_axisPen, _marginLeft, y, _marginLeft + _maxScaleLen, y); if (i == _vScaleCount) { break; } y1 = y; for (int j = 0; j < _minorScaleCount; j++) { e.Graphics.DrawLine(_axisPen, _marginLeft, y1, _marginLeft + _minScaleLen, y1); y1 -= y1Span; } y -= ySpan; } //draw h axis diffValue = (MaxValue - MinValue) / (float)_hScaleCount; x = _marginLeft; float x1 = x; y = Height - _marginBottom; float xSpan = (Width - _marginLeft - _marginRight) / (float)_hScaleCount; float x1Span = xSpan / _minorScaleCount; string vString; for (int i = 0; i <= _hScaleCount; i++) { v = (_minValue + diffValue * i); vString = v.ToString("0.####"); e.Graphics.DrawLine(_axisPen, x, y, x, y - _maxScaleLen); fontSize = e.Graphics.MeasureString(vString, Font); e.Graphics.DrawString(vString, Font, _axisBrush, x - fontSize.Width / 2f, y + 2); if (i == _hScaleCount) { break; } x1 = x; for (int j = 0; j < _minorScaleCount; j++) { e.Graphics.DrawLine(_axisPen, x1, y, x1, y - _minScaleLen); x1 += x1Span; } x += xSpan; } //draw curve,绘制曲线 if (_selectedBandNos == null || _selectedBandNos.Length == 0) { return; } double bValue; diffValue = (MaxValue - MinValue) / (Width - _marginLeft - _marginRight); //直方图水平方向的分辨率倒数(间隔多点一个单位) double diffCount = (MaxCount - MinCount) / (Height - _marginTop - _marginBottom); //直方图垂直方向的分辨率倒数 if (diffValue == 0d || diffCount == 0d) { } else { foreach (int bandNo in _selectedBandNos) { RasterQuickStatResult result = _results[bandNo]; HistogramResult hist = result.HistogramResult; int vCount = hist.ActualBuckets; //zyb,20131119新加 if (vCount <= 1) { continue; } PointF[] pts = new PointF[vCount]; for (int i = 0; i < vCount; i++) { bValue = _minValue + i * hist.Bin; x = (float)(_marginLeft + (bValue - _minValue) / diffValue); y = (float)(Height - _marginBottom - hist.Items[i] / diffCount); pts[i] = new PointF(x, y); } e.Graphics.DrawCurve(_curvePens[bandNo], pts, 0f); } } //draw lengends,绘制图例 x = Width - _marginRight + 20; y = _marginTop; foreach (int bandNo in _selectedBandNos) { e.Graphics.FillRectangle(_legendBurshes[bandNo], x, y, _LegendWidth, _LegendWidth); e.Graphics.DrawString("Band " + bandNo.ToString(), Font, _legendBurshes[bandNo], x + _LegendWidth + 2, y); y += (fontSize.Height + 4); } }