/// <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]; } }
/// <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)); } } } } } }