/// <summary> /// Asserts the results for the specified band. /// </summary> /// <param name="source">The source raster.</param> /// <param name="sourceBandIndex">The band index of the source raster.</param> /// <param name="result">The resulting raster.</param> /// <param name="resultBandIndex">The band index of the resulting raster.</param> /// <param name="filterRadius">The radius of the filter.</param> private void AssertResultForBand(IRaster source, Int32 sourceBandIndex, IRaster result, Int32 resultBandIndex, Int32 filterRadius) { for (Int32 rowIndex = 0; rowIndex < result.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < result.NumberOfColumns; columnIndex++) { Double filteredValue = 0; switch (result.Format) { case RasterFormat.Integer: for (Int32 filterRowIndex = -filterRadius; filterRowIndex <= filterRadius; filterRowIndex++) { for (Int32 filterColumnIndex = -filterRadius; filterColumnIndex <= filterRadius; filterColumnIndex++) { filteredValue += source.GetNearestValue(rowIndex + filterRowIndex, columnIndex + filterColumnIndex, sourceBandIndex); } } filteredValue /= Calculator.Square(2 * filterRadius + 1); Assert.AreEqual(Convert.ToUInt32(filteredValue), result.GetValue(rowIndex, columnIndex, resultBandIndex)); break; case RasterFormat.Floating: for (Int32 filterRowIndex = -filterRadius; filterRowIndex <= filterRadius; filterRowIndex++) { for (Int32 filterColumnIndex = -filterRadius; filterColumnIndex <= filterRadius; filterColumnIndex++) { filteredValue += source.GetNearestFloatValue(rowIndex + filterRowIndex, columnIndex + filterColumnIndex, sourceBandIndex); } } filteredValue /= Calculator.Square(2 * filterRadius + 1); Assert.AreEqual(filteredValue, result.GetFloatValue(rowIndex, columnIndex, resultBandIndex)); break; } } } }
/// <summary> /// Creates a raster image. /// </summary> /// <param name="other">The other raster image.</param> /// <returns>The produced raster image matching <paramref name="other"/>.</returns> /// <exception cref="System.ArgumentNullException">The other raster is null.</exception> public IRaster CreateRaster(IRaster other) { if (other == null) { throw new ArgumentNullException("other", "The other raster is null."); } IRaster raster = CreateRaster(other.Format, other.NumberOfBands, other.NumberOfRows, other.NumberOfColumns, other.RadiometricResolution, other.Mapper); switch (raster.Format) { case RasterFormat.Integer: for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++) { for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++) { raster.SetValue(rowIndex, columnIndex, bandIndex, raster.GetValue(rowIndex, columnIndex, bandIndex)); } } } break; case RasterFormat.Floating: for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++) { for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++) { raster.SetFloatValue(rowIndex, columnIndex, bandIndex, raster.GetFloatValue(rowIndex, columnIndex, bandIndex)); } } } break; } return(raster); }
/// <summary> /// Writes the raster of the geometry into a strip. /// </summary> /// <param name="raster">The raster.</param> /// <param name="compression">The compression.</param> /// <param name="format">The sample format.</param> private void WriteRasterContentToStrip(IRaster raster, TiffCompression compression, TiffSampleFormat format) { _baseStream.Seek(_currentImageStartPosition, SeekOrigin.Begin); // mark the starting position of the strip UInt32 numberOfBytes = (UInt32)(Math.Ceiling(raster.RadiometricResolution / 8.0) * raster.NumberOfBands * raster.NumberOfRows * raster.NumberOfColumns); UInt32 numberOfBytesLeft = numberOfBytes; if (numberOfBytes % 2 != 0) // correct the number of bytes { numberOfBytes++; } Byte[] bytes = new Byte[numberOfBytes < NumberOfWritableBytes ? numberOfBytes : NumberOfWritableBytes]; Int32 byteIndex = 0; Int32 bitIndex = 8; for (Int32 rowIndex = 0; rowIndex < raster.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < raster.NumberOfColumns; columnIndex++) { // write the values for each band into the buffer for (Int32 bandIndex = 0; bandIndex < raster.NumberOfBands; bandIndex++) { switch (format) { case TiffSampleFormat.Undefined: case TiffSampleFormat.UnsignedInteger: WriteUnsignedIntergerValue(raster.GetValue(rowIndex, columnIndex, bandIndex), raster.RadiometricResolution, bytes, ref byteIndex, ref bitIndex); break; case TiffSampleFormat.SignedInteger: case TiffSampleFormat.Floating: WriteFloatValue(raster.GetFloatValue(rowIndex, columnIndex, bandIndex), raster.RadiometricResolution, bytes, ref byteIndex, ref bitIndex); break; } if (byteIndex == bytes.Length) { // write the buffer to the file _baseStream.Write(bytes, 0, byteIndex); byteIndex = 0; numberOfBytesLeft -= (UInt32)byteIndex; // the final array of bytes should not be the number of bytes left if (numberOfBytes > NumberOfWritableBytes && numberOfBytesLeft > 0 && numberOfBytesLeft < NumberOfWritableBytes) { bytes = new Byte[numberOfBytesLeft % 2 == 0 ? numberOfBytesLeft : numberOfBytesLeft + 1]; } } } } } // if any values are left if (numberOfBytesLeft > 0) { _baseStream.Write(bytes, 0, byteIndex); } }
/// <summary> /// Returns the spectral value at a specified index. /// </summary> /// <param name="rowIndex">The zero-based row index of the value.</param> /// <param name="columnIndex">The zero-based column index of the value.</param> /// <param name="bandIndex">The zero-based band index of the value.</param> /// <returns>The spectral value at the specified index.</returns> protected override Double ApplyGetFloatValue(Int32 rowIndex, Int32 columnIndex, Int32 bandIndex) { return(_source.GetFloatValue(_rowIndex + rowIndex, _columnIndex + columnIndex, bandIndex)); }
/// <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)); } } } } } }
/// <summary> /// Gets a spectral value at a specified row and column index. /// </summary> /// <param name="rowIndex">The zero-based column index of the value.</param> /// <param name="columnIndex">The zero-based row index of the value.</param> /// <returns>The spectral value located at the specified row and column index.</returns> /// <exception cref="System.NotSupportedException">The raster is not readable.</exception> /// <exception cref="System.ArgumentOutOfRangeException"> /// The row index is less than 0. /// or /// The row index is equal to or greater than the number of rows. /// or /// The column index is less than 0. /// or /// the column index is equal to or greater than the number of columns. /// </exception> public Double GetFloatValue(Int32 rowIndex, Int32 columnIndex) { return(_raster.GetFloatValue(rowIndex, columnIndex, _bandIndex)); }