/// <summary> /// Prepares the result of the operation. /// </summary> /// <returns>The resulting object.</returns> protected override ISpectralGeometry PrepareResult() { _offset = new Int32[Source.Raster.NumberOfBands]; _factor = new Double[Source.Raster.NumberOfBands]; for (Int32 bandIndex = 0; bandIndex < Source.Raster.NumberOfBands; bandIndex++) { Int32 minIntensity = 0, maxIntensity = Source.Raster.HistogramValues[bandIndex].Count - 1; while (minIntensity < Source.Raster.HistogramValues[bandIndex].Count && Source.Raster.HistogramValues[bandIndex][minIntensity] == 0) { minIntensity++; } while (maxIntensity >= 0 && Source.Raster.HistogramValues[bandIndex][maxIntensity] == 0) { maxIntensity--; } if (minIntensity == maxIntensity) { _offset[bandIndex] = 0; _factor[bandIndex] = 1; } else { _offset[bandIndex] = -minIntensity; _factor[bandIndex] = (Double)RasterAlgorithms.RadiometricResolutionMax(Source.Raster.RadiometricResolution) / (maxIntensity - minIntensity); } } return(base.PrepareResult()); }
/// <summary> /// Computes the specified spectral value. /// </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 UInt32 Compute(Int32 rowIndex, Int32 columnIndex, Int32 bandIndex) { if (_multipliers[bandIndex] == 0) { return(RasterAlgorithms.RadiometricResolutionMax(Source.Raster.RadiometricResolution) / 2 + 1); } return(RasterAlgorithms.Restrict(_multipliers[bandIndex] * Source.Raster.GetValue(rowIndex, columnIndex, bandIndex), Source.Raster.RadiometricResolution)); }
/// <summary> /// Initializes a new instance of the <see cref="KMeansClustering"/> class. /// </summary> /// <param name="source">The source.</param> /// <param name="target">The target.</param> /// <param name="parameters">The parameters.</param> /// <exception cref="System.ArgumentNullException"> /// The source is null. /// or /// The method is null. /// or /// The method requires parameters which are not specified. /// </exception> /// <exception cref="System.ArgumentException"> /// The parameters do not contain a required parameter value. /// or /// The type of a parameter does not match the type specified by the method. /// or /// The value of a parameter is not within the expected range. /// or /// The specified source and result are the same objects, but the method does not support in-place operations. /// or /// The source geometry does not contain raster data. /// or /// The raster format of the source is not supported by the method. /// </exception> public KMeansClustering(ISpectralGeometry source, ISpectralGeometry target, IDictionary<OperationParameter, Object> parameters) : base(source, target, SpectralOperationMethods.KMeansClustering, parameters) { _numberOfClusters = Convert.ToInt32(ResolveParameter(SpectralOperationParameters.NumberOfClusters)); _maximumIterations = Convert.ToInt32(ResolveParameter(CommonOperationParameters.NumberOfIterations)); if (_numberOfClusters == 0) _numberOfClusters = (Int32)(Math.Sqrt(Source.Raster.NumberOfRows * Source.Raster.NumberOfColumns) * Source.Raster.RadiometricResolution / 8); if (_maximumIterations == 0) _maximumIterations = _numberOfClusters; _maximumDifference = RasterAlgorithms.RadiometricResolutionMax(Source.Raster) / Source.Raster.NumberOfRows * Source.Raster.NumberOfColumns; }
/// <summary> /// Computes the parameters of the specified band. /// </summary> /// <param name="bandIndex">The band index.</param> private void ComputeParameters(Int32 bandIndex) { // source: http://en.wikipedia.org/wiki/Histogram_equalization IReadOnlyList <Int32> histogram = Source.Raster.HistogramValues[bandIndex]; _cumulativeDistributionValues[bandIndex] = new Double[histogram.Count]; // setting values _cumulativeDistributionValues[bandIndex][0] = Convert.ToDouble(histogram[0]) / (Source.Raster.NumberOfRows * Source.Raster.NumberOfColumns); for (Int32 i = 1; i < histogram.Count; i++) { _cumulativeDistributionValues[bandIndex][i] = _cumulativeDistributionValues[bandIndex][i - 1] + Convert.ToDouble(histogram[i]) / (Source.Raster.NumberOfRows * Source.Raster.NumberOfColumns); } // setting minimum Int32 minIndex = 0; for (Int32 i = 0; i < histogram.Count; i++) { if (histogram[i] != 0) { minIndex = i; break; } } _cumulativeDistributionMinimums[bandIndex] = _cumulativeDistributionValues[bandIndex][minIndex]; // setting maximum Int32 maxIndex = histogram.Count - 1; for (Int32 i = histogram.Count - 1; i >= 0; i--) { if (histogram[i] != 0) { maxIndex = i; break; } } _cumulativeDistributionMaximums[bandIndex] = _cumulativeDistributionValues[bandIndex][maxIndex]; // exponent _radiometricResolutionExponents[bandIndex] = Calculator.Pow(2, Source.Raster.RadiometricResolution); // radiometric value limit _radiometricValueLimits[bandIndex] = RasterAlgorithms.RadiometricResolutionMax(Source.Raster.RadiometricResolution); }
/// <summary> /// Computes the specified spectral value. /// </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 UInt32 Compute(Int32 rowIndex, Int32 columnIndex, Int32 bandIndex) { Int32 rowCenter = (_structuringElement.NumberOfRows - 1) / 2; Int32 columnCenter = (_structuringElement.NumberOfColumns - 1) / 2; Int32 firstRowIndex = rowIndex - rowCenter; Int32 firstColumnIndex = columnIndex - columnCenter; Double result = RasterAlgorithms.RadiometricResolutionMax(Source.Raster.RadiometricResolution); for (Int32 row = 0; row < _structuringElement.NumberOfRows; row++) { for (Int32 column = 0; column < _structuringElement.NumberOfColumns; column++) { Double value = Source.Raster.GetNearestValue(firstRowIndex + row, firstColumnIndex + column, bandIndex) - _structuringElement[row, column]; if (value >= 0 && value < result) { result = value; } } } return((UInt32)result); }
/// <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> private void AssertResultForBand(IRaster source, Int32 sourceBandIndex, IRaster result, Int32 resultBandIndex) { Assert.AreEqual(source.HistogramValues[sourceBandIndex].Sum(), result.HistogramValues[resultBandIndex].Sum()); Assert.Greater(result.HistogramValues[resultBandIndex][0], 0); for (Int32 rowIndex = 0; rowIndex < result.NumberOfRows; rowIndex++) { for (Int32 columnIndex = 0; columnIndex < result.NumberOfColumns; columnIndex++) { Assert.GreaterOrEqual(result.GetValue(rowIndex, columnIndex, resultBandIndex), 0); Assert.LessOrEqual(result.GetValue(rowIndex, columnIndex, resultBandIndex), RasterAlgorithms.RadiometricResolutionMax(source.RadiometricResolution)); } } }
/// <summary> /// Prepares the result of the operation. /// </summary> /// <returns>The resulting object.</returns> protected override ISpectralGeometry PrepareResult() { _peek = RasterAlgorithms.RadiometricResolutionMax(Source.Raster.RadiometricResolution); return(base.PrepareResult()); }