Created
March 20, 2016 17:52
-
-
Save FlexMonkey/8b3ab538242f3376b2c9 to your computer and use it in GitHub Desktop.
Normalise a CIVector - ideal for use as a convolution matrix
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension CIVector | |
{ | |
func normalize() -> CIVector | |
{ | |
var sum: CGFloat = 0 | |
for i in 0 ..< self.count | |
{ | |
sum += self.valueAtIndex(i) | |
} | |
if sum == 0 | |
{ | |
return self | |
} | |
var normalizedValues = [CGFloat]() | |
for i in 0 ..< self.count | |
{ | |
normalizedValues.append(self.valueAtIndex(i) / sum) | |
} | |
return CIVector(values: normalizedValues, | |
count: normalizedValues.count) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment