Ejemplo n.º 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Segment" /> class.
        /// </summary>
        /// <param name="numberOfBands">The number of bands.</param>
        /// <param name="statistics">The statistics computed for the segment.</param>
        /// <exception cref="System.ArgumentOutOfRangeException">The number of bands is less than 1.</exception>
        public Segment(Int32 numberOfBands, SpectralStatistics statistics)
        {
            if (numberOfBands < 1)
            {
                throw new ArgumentOutOfRangeException(nameof(numberOfBands), "The number of bands is less than 1.");
            }

            Count = 0;

            // initialize collections
            _mean = new Double[numberOfBands];

            if (statistics.HasFlag(SpectralStatistics.Variance))
            {
                _meanSquare = new Double[numberOfBands];
                _variance   = new Double[numberOfBands];
            }

            if (statistics.HasFlag(SpectralStatistics.Comoment))
            {
                _comoment = new Double[numberOfBands, numberOfBands];
            }

            if (statistics.HasFlag(SpectralStatistics.Covariance))
            {
                Covariance = new Double[numberOfBands, numberOfBands];
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SegmentCollection" /> class.
        /// </summary>
        /// <param name="other">The other segment collection.</param>
        /// <param name="statistics">The spectral statistics.</param>
        /// <exception cref="System.ArgumentNullException">The other segment collection is null.</exception>
        public SegmentCollection(SegmentCollection other, SpectralStatistics statistics)
        {
            if (other == null)
            {
                throw new ArgumentNullException("other", "The other segment collection is null.");
            }

            Raster     = other.Raster;
            Count      = other.Count;
            Statistics = other.Statistics | statistics;
            _indexToSegmentDictionary = new Dictionary <Int32, Segment>();
            _segmentToIndexDictionary = new Dictionary <Segment, List <Int32> >();

            foreach (Segment otherSegment in other._segmentToIndexDictionary.Keys)
            {
                List <Int32> otherIndices = other._segmentToIndexDictionary[otherSegment];

                Segment segment;
                if (Statistics == other.Statistics)
                {
                    segment = new Segment(otherSegment);
                    otherIndices.ForEach(x => _indexToSegmentDictionary.Add(x, segment));
                }
                else
                {
                    Int32 rowIndex    = otherIndices[0] / Raster.NumberOfColumns;
                    Int32 columnIndex = otherIndices[0] % Raster.NumberOfColumns;

                    if (Raster.Format == RasterFormat.Integer)
                    {
                        segment = new Segment(Raster.GetValues(rowIndex, columnIndex), Statistics);

                        for (Int32 i = 0; i < other._segmentToIndexDictionary[otherSegment].Count; i++)
                        {
                            _indexToSegmentDictionary.Add(otherIndices[i], segment);
                            rowIndex    = otherIndices[i] / Raster.NumberOfColumns;
                            columnIndex = otherIndices[i] % Raster.NumberOfColumns;

                            segment.AddValues(Raster.GetValues(rowIndex, columnIndex));
                        }
                    }
                    else
                    {
                        segment = new Segment(Raster.GetFloatValues(rowIndex, columnIndex), Statistics);

                        for (Int32 i = 0; i < other._segmentToIndexDictionary[otherSegment].Count; i++)
                        {
                            _indexToSegmentDictionary.Add(otherIndices[i], segment);
                            rowIndex    = otherIndices[i] / Raster.NumberOfColumns;
                            columnIndex = otherIndices[i] % Raster.NumberOfColumns;

                            segment.AddFloatValues(Raster.GetFloatValues(rowIndex, columnIndex));
                        }
                    }
                }

                _segmentToIndexDictionary.Add(segment, new List <Int32>(otherIndices));
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SegmentCollection" /> class.
        /// </summary>
        /// <param name="raster">The raster.</param>
        /// <param name="statistics">The statistics computed for the segments.</param>
        /// <exception cref="System.ArgumentNullException">The raster is null.</exception>
        public SegmentCollection(IRaster raster, SpectralStatistics statistics)
            : this(statistics)
        {
            if (raster == null)
            {
                throw new ArgumentNullException("raster", "The raster is null.");
            }

            Raster = raster;
            Count  = raster.NumberOfRows * raster.NumberOfColumns;
            _indexToSegmentDictionary = new Dictionary <Int32, Segment>(raster.NumberOfRows * raster.NumberOfColumns);
            _segmentToIndexDictionary = new Dictionary <Segment, List <Int32> >();
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Segment" /> class.
        /// </summary>
        /// <param name="spectralValues">The array of spectral values.</param>
        /// <param name="statistics">The statistics computed for the segment.</param>
        /// <exception cref="System.ArgumentNullException">The array of spectral values is null.</exception>
        /// <exception cref="System.ArgumentException">The array of spectral values is empty.</exception>
        public Segment(Double[] spectralValues, SpectralStatistics statistics)
            : this(spectralValues == null || spectralValues.Length == 0 ? 1 : spectralValues.Length, statistics)
        {
            if (spectralValues == null)
            {
                throw new ArgumentNullException(nameof(spectralValues), "The array of spectral values is null.");
            }
            if (spectralValues.Length == 0)
            {
                throw new ArgumentException("The array of spectral values is empty.", "spectralValues");
            }

            AddFloatValues(spectralValues);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="QuadSegmentCollection" /> class.
        /// </summary>
        /// <param name="raster">The raster.</param>
        /// <exception cref="System.ArgumentNullException">The raster is null.</exception>
        public QuadSegmentCollection(IRaster raster, SpectralStatistics statistics)
            : base(statistics)
        {
            if (raster == null)
            {
                throw new ArgumentNullException("raster", "The raster is null.");
            }

            Raster = raster;
            Count  = 1;

            _indexToSegmentDictionary = new Dictionary <Int32, Segment>(raster.NumberOfRows * raster.NumberOfColumns);
            Segment segment = new Segment(raster, Statistics)
            {
                MortonCode = 1
            };

            _segmentToIndexDictionary = new Dictionary <Segment, List <Int32> >();
            for (Int32 index = 0; index < raster.NumberOfRows * raster.NumberOfColumns; index++)
            {
                _indexToSegmentDictionary.Add(index, segment);
            }
            _segmentToIndexDictionary.Add(_indexToSegmentDictionary[0], new List <Int32>(Enumerable.Range(0, raster.NumberOfRows * raster.NumberOfColumns)));
        }
Ejemplo n.º 6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Segment" /> class.
        /// </summary>
        /// <param name="raster">The raster from which the segment is derived.</param>
        /// <param name="statistics">The statistics computed for the segment.</param>
        /// <exception cref="System.ArgumentNullException">The raster is null.</exception>
        public Segment(IRaster raster, SpectralStatistics statistics)
        {
            if (raster == null)
            {
                throw new ArgumentNullException("raster", "The raster is null.");
            }

            Count = raster.NumberOfRows * raster.NumberOfColumns;

            // mean computation
            _mean = new Double[raster.NumberOfBands];

            for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++)
            {
                for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++)
                {
                    for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
                    {
                        _mean[bandIndex] += raster.GetFloatValue(rowIndex, columnIndex, bandIndex);
                    }
                }
            }

            for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
            {
                _mean[bandIndex] /= (raster.NumberOfColumns * raster.NumberOfRows);
            }

            // variance computation
            if (statistics.HasFlag(SpectralStatistics.Variance) && raster.NumberOfColumns * raster.NumberOfRows > 1)
            {
                _meanSquare = new Double[raster.NumberOfBands];
                _variance   = new Double[raster.NumberOfBands];
                for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++)
                {
                    for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++)
                    {
                        for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
                        {
                            _variance[bandIndex] += Math.Pow(_mean[bandIndex] - raster.GetFloatValue(rowIndex, columnIndex, bandIndex), 2);
                        }
                    }
                }

                for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
                {
                    _meanSquare[bandIndex] = _mean[bandIndex] * _mean[bandIndex];
                    _variance[bandIndex]  /= (raster.NumberOfColumns * raster.NumberOfRows - 1);
                }
            }

            // comoment computation
            if (statistics.HasFlag(SpectralStatistics.Comoment))
            {
                _comoment = new Double[raster.NumberOfBands, raster.NumberOfBands];
                for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++)
                {
                    for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++)
                    {
                        for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
                        {
                            for (Int32 otherBandIndex = 0; otherBandIndex < raster.NumberOfBands; otherBandIndex++)
                            {
                                _comoment[bandIndex, otherBandIndex] += (raster.GetFloatValue(rowIndex, columnIndex, bandIndex) - _mean[bandIndex]) * (raster.GetFloatValue(rowIndex, columnIndex, otherBandIndex) - _mean[otherBandIndex]);
                            }
                        }
                    }
                }
            }

            // covariance computation
            if (statistics.HasFlag(SpectralStatistics.Covariance) && raster.NumberOfColumns * raster.NumberOfRows > 1)
            {
                Covariance = new Double[raster.NumberOfBands, raster.NumberOfBands];
                for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++)
                {
                    for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++)
                    {
                        for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++)
                        {
                            for (Int32 otherBandIndex = 0; otherBandIndex < raster.NumberOfBands; otherBandIndex++)
                            {
                                Covariance[bandIndex, otherBandIndex] += (_mean[bandIndex] - raster.GetFloatValue(rowIndex, columnIndex, bandIndex)) * (_mean[otherBandIndex] - raster.GetFloatValue(rowIndex, columnIndex, otherBandIndex));
                            }
                        }
                    }
                }
            }
        }
Ejemplo n.º 7
0
 /// <summary>
 /// Initializes a new instance of the <see cref="SegmentCollection" /> class.
 /// </summary>
 /// <param name="statistics">The statistics computed for the segments.</param>
 public SegmentCollection(SpectralStatistics statistics)
 {
     Statistics = statistics;
 }