Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for normalizeDoubleImage #92

Merged
merged 7 commits into from
Apr 4, 2018
Merged
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,20 @@ object PixelImageNormalization {

/** Normalizes the image such that its values are on the interval [lower, upper]. */
def normalizeDoubleImageToRange(image: PixelImage[Double], lower: Double, upper: Double): PixelImage[Double] = {
def normalizer(x: Double) = (x - lower) / (upper - lower)
if (lower < 0.0 || upper > 1.0)
image
else
require(lower < upper)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The require can be removed as it does not change the correctness. Hence we could invert an image by: normalizeDoubleImageToRange(image,max,min)

val min = image.values.min
val max = image.values.max
if(min == max){ //if we would divide with 0
image.map(_ => (upper-lower)/2) //return mean of [lower,upper]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it should be (lower+upper)/2 or lower+(upper-lower)/2. I would calculate the constant one above so that it is even better visible that the map does not depend on the position.

}else {
def normalizer(x: Double) = lower + (x - min) / (max - min) * (upper - lower)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you precompute the normalization outside of the function? It might be more efficient except it is optimized by the compiler or the JVM and not much less readable I think.

image.map(normalizer)
}
}

/** Expands value range to the interval [0,1]. */
def normalizeDoubleImage(image: PixelImage[Double]): PixelImage[Double] = {
val min = image.values.min
val max = image.values.max
normalizeDoubleImageToRange(image, min, max)
normalizeDoubleImageToRange(image, 0.0, 1.0)
}

/** Normalize the image to range ([0,1], [0,1], [0,1]). (Each channel is normalized seperately.) */
Expand Down