Skip to content

Commit

Permalink
Remove Restriction to StreamingHistogram
Browse files Browse the repository at this point in the history
The various histogram equalization functionality had previously been
tied to StreamingHistogram, now it works with Histogram[T].
  • Loading branch information
James McClain committed Oct 19, 2016
1 parent ee53ce7 commit 2dc4e4d
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package geotrellis.raster.equalization

import geotrellis.raster._
import geotrellis.raster.histogram.Histogram
import geotrellis.raster.histogram.StreamingHistogram


Expand Down Expand Up @@ -74,14 +75,14 @@ object HistogramEqualization {
}

/**
* Given a [[Tile]] and a [[StreamingHistogram]], return a Tile
* with an equalized histogram.
* Given a [[Tile]] and a [[Histogram]], return a Tile with an
* equalized histogram.
*
* @param tile A singleband tile
* @param histogram The histogram of the tile
* @return A singleband tile with improved contrast
*/
def apply(tile: Tile, histogram: StreamingHistogram): Tile = {
def apply[T <: AnyVal](tile: Tile, histogram: Histogram[T]): Tile = {
val T = _T(tile.cellType, histogram.cdf)_
tile.mapDouble(T)
}
Expand All @@ -99,15 +100,15 @@ object HistogramEqualization {
}

/**
* Given a [[MultibandTile]] and a [[StreamingHistogram]] for each
* of its bands, return a MultibandTile whose bands all have
* equalized histograms.
* Given a [[MultibandTile]] and a [[Histogram]] for each of its
* bands, return a MultibandTile whose bands all have equalized
* histograms.
*
* @param tile A multiband tile
* @param histograms A sequence of histograms, one for each band
* @return A multiband tile with improved contrast
*/
def apply(tile: MultibandTile, histograms: Seq[StreamingHistogram]): MultibandTile = {
def apply[T <: AnyVal](tile: MultibandTile, histograms: Seq[Histogram[T]]): MultibandTile = {
MultibandTile(
tile.bands
.zip(histograms)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,18 @@ class FastMapHistogram(_size: Int, _buckets: Array[Int], _counts: Array[Long], _
None
}

/**
* CDF of the distribution.
*/
def cdf(): Array[(Double, Double)] = {
val pdf = counts.map({ n => (n / total.toDouble) })

buckets
.map({ label => label.toDouble })
.zip(pdf.scanLeft(0.0)(_ + _).drop(1))
.toArray
}

/**
* Return the smallest and largest values seen by the histogram, if
* it has seen any values.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ abstract trait Histogram[@specialized (Int, Double) T <: AnyVal] extends Seriali
*/
def maxValue(): Option[T]

/**
* CDF of the distribution.
*/
def cdf(): Array[(Double, Double)]

/**
* Return the smallest and largest items seen as a tuple.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package geotrellis.spark.equalization

import geotrellis.raster._
import geotrellis.raster.equalization.HistogramEqualization
import geotrellis.raster.histogram.Histogram
import geotrellis.raster.histogram.StreamingHistogram
import geotrellis.spark._

Expand Down Expand Up @@ -35,16 +36,16 @@ object RDDHistogramEqualization {
}

/**
* Given an RDD of [[Tile]] objects and a [[StreamingHistogram]]
* derived from all of the tiles, return another RDD of tiles where
* the respective tiles have had their histograms equalized.
* Given an RDD of [[Tile]] objects and a [[Histogram]] derived
* from all of the tiles, return another RDD of tiles where the
* respective tiles have had their histograms equalized.
*
* @param rdd An RDD of tile objects
* @param histogram A histogram derived from the whole RDD of tiles
*/
def singleband[K, V: (? => Tile): ClassTag, M](
def singleband[K, V: (? => Tile): ClassTag, M, T <: AnyVal](
rdd: RDD[(K, V)] with Metadata[M],
histogram: StreamingHistogram
histogram: Histogram[T]
): RDD[(K, Tile)] with Metadata[M] = {
ContextRDD(
rdd.map({ case (key, tile: Tile) =>
Expand All @@ -70,23 +71,24 @@ object RDDHistogramEqualization {
.map({ band => StreamingHistogram.fromTile(band, 1<<17) })
.toArray })
.reduce(r)
.map(_.asInstanceOf[Histogram[Double]])

multiband(rdd, histograms)
}

/**
* Given an RDD of [[MultibandTile]] objects and a sequence of
* [[StreamingHistogram]] objects (on per band) derived from all of
* the tiles, return another RDD of multiband tiles where the
* [[Histogram]] objects (on per band) derived from all of the
* tiles, return another RDD of multiband tiles where the
* respective bands of the respective tiles have had their
* histograms equalized.
*
* @param rdd An RDD of tile objects
* @param histograms A histogram derived from the whole RDD of tiles
*/
def multiband[K, V: (? => MultibandTile): ClassTag, M](
def multiband[K, V: (? => MultibandTile): ClassTag, M, T <: AnyVal](
rdd: RDD[(K, V)] with Metadata[M],
histograms: Array[StreamingHistogram]
histograms: Array[Histogram[T]]
): RDD[(K, MultibandTile)] with Metadata[M] = {
ContextRDD(
rdd.map({ case (key, tile: MultibandTile) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package geotrellis.spark.equalization

import geotrellis.raster._
import geotrellis.raster.histogram.StreamingHistogram
import geotrellis.raster.histogram.Histogram
import geotrellis.spark._
import geotrellis.util.MethodExtensions

Expand All @@ -25,6 +25,6 @@ trait RDDMultibandEqualizationMethods[K, M] extends MethodExtensions[RDD[(K, Mul
*
* @param histograms A sequence of histograms
*/
def equalize(histograms: Array[StreamingHistogram]): RDD[(K, MultibandTile)] with Metadata[M] =
def equalize[T <: AnyVal](histograms: Array[Histogram[T]]): RDD[(K, MultibandTile)] with Metadata[M] =
RDDHistogramEqualization.multiband(self, histograms)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package geotrellis.spark.equalization

import geotrellis.raster._
import geotrellis.raster.histogram.StreamingHistogram
import geotrellis.raster.histogram.Histogram
import geotrellis.spark._
import geotrellis.util.MethodExtensions

Expand All @@ -24,6 +24,6 @@ trait RDDSinglebandEqualizationMethods[K, M] extends MethodExtensions[RDD[(K, Ti
*
* @param histogram A histogram
*/
def equalize(histogram: StreamingHistogram): RDD[(K, Tile)] with Metadata[M] =
def equalize[T <: AnyVal](histogram: Histogram[T]): RDD[(K, Tile)] with Metadata[M] =
RDDHistogramEqualization.singleband(self, histogram)
}

0 comments on commit 2dc4e4d

Please sign in to comment.