Example #1
0
        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;
            }
        }
Example #2
0
        public Dictionary <int, RasterQuickStatResult> Compute(IRasterDataProvider dataProvider, int[] aoi, int[] bandNos, Action <int, string> progressTracker)
        {
            if (dataProvider == null)
            {
                throw new ArgumentNullException("dataProvider");
            }
            if (bandNos == null || bandNos.Length == 0)
            {
                throw new ArgumentNullException("bandNos");
            }
            int minBandNo = bandNos.Min(), maxBandNo = bandNos.Max();

            if (minBandNo < 1 || maxBandNo > dataProvider.BandCount)
            {
                throw new IndexOutOfRangeException("bandNo");
            }
            //
            double[]             minValues  = new double[bandNos.Length];
            double[]             maxValues  = new double[bandNos.Length];
            double[]             meanValues = new double[bandNos.Length];
            IMaxMinValueComputer computer   = MaxMinValueComputerFactory.GetMaxMinValueComputer(dataProvider.DataType);

            IRasterBand[] srcRasters        = new IRasterBand[bandNos.Length];
            for (int i = 0; i < bandNos.Length; i++)
            {
                srcRasters[i] = dataProvider.GetRasterBand(bandNos[i]);
            }
            //
            computer.Compute(srcRasters, aoi, out minValues, out maxValues, out meanValues, progressTracker);
            //
            Dictionary <int, RasterQuickStatResult> results = new Dictionary <int, RasterQuickStatResult>();
            List <IRasterBand> rasterBands = new List <IRasterBand>();
            int b = 0;

            foreach (int bandNo in bandNos)
            {
                IRasterBand rstBand = dataProvider.GetRasterBand(bandNo);
                rasterBands.Add(rstBand);
                RasterQuickStatResult result = BuildStatResult(rstBand, minValues[b], maxValues[b], meanValues[b]);
                if (result != null)
                {
                    results.Add(bandNo, result);
                }
                b++;
            }
            //
            IHistogramComputer histogramComputer = HistogramComputerFactory.GetHistogramComputer(dataProvider.DataType);

            //
            if (aoi == null || aoi.Length == 0)
            {
                histogramComputer.Compute(rasterBands.ToArray(), results.Values.ToArray(), progressTracker);
            }
            else
            {
                histogramComputer.Compute(rasterBands.ToArray(), aoi, results.Values.ToArray(), progressTracker);
            }
            //
            return(results);
        }
Example #3
0
        private RasterQuickStatResult BuildStatResult(IRasterBand rasterBand, double minValue, double maxValue, double meanValue)
        {
            int    acutalBuckets;
            double bin = GetBin(rasterBand.DataType, maxValue, minValue, HistogramResult.MAX_BUCKETS, out acutalBuckets);
            RasterQuickStatResult result = new RasterQuickStatResult();

            result.HistogramResult.ActualBuckets = acutalBuckets + 1;
            result.HistogramResult.MinDN         = minValue;
            result.HistogramResult.MaxDN         = maxValue;
            result.HistogramResult.Bin           = bin;
            result.MinValue  = minValue;
            result.MaxValue  = maxValue;
            result.MeanValue = meanValue;
            return(result);
        }
Example #4
0
        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();
        }
Example #5
0
        private void DisplayStatResult(string fileName, Dictionary <int, RasterQuickStatResult> results)
        {
            StringBuilder sb = new StringBuilder();

            btnStatItems.DropDownItems.Add(GetItem("所有波段", -1));
            btnStatItems.DropDownItems.Add(new ToolStripSeparator());
            sb.AppendLine("文件:" + fileName);
            sb.AppendLine("    -".PadRight(76, '-'));
            sb.AppendLine("波段".PadLeft(9) + "最小值".PadLeft(10) + "最大值".PadLeft(10) + "平均值".PadLeft(10) + "方差".PadLeft(11) + "标准差".PadLeft(10));
            foreach (int bandNo in results.Keys)
            {
                btnStatItems.DropDownItems.Add(GetItem("波段 " + bandNo.ToString(), bandNo));
                RasterQuickStatResult result = results[bandNo];
                sb.AppendLine(bandNo.ToString().PadLeft(11) + result.MinValue.ToString("0.####").PadLeft(13) + result.MaxValue.ToString("0.####").PadLeft(13) + result.MeanValue.ToString("0.####").PadLeft(13) + result.Stddev.ToString("0.####").PadLeft(13) + Math.Sqrt(result.Stddev).ToString("0.####").PadLeft(13));
            }
            _statStrings      = sb.ToString();
            richTextBox1.Text = _statStrings;
            PrintHistogram(results.Keys.ToArray()[0], results[results.Keys.ToArray()[0]]);
            (btnStatItems.DropDownItems[2] as ToolStripMenuItem).Checked = true;
        }
Example #6
0
        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);
        }
Example #7
0
 protected abstract void HandleHistogram(T[] buffer, RasterQuickStatResult rasterQuickStatResult, int cols);
Example #8
0
        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);
            }
        }