Last active
February 16, 2018 10:38
-
-
Save StanDimitroff/0bc6db55ac8bbe2668e7ff5f012c5c69 to your computer and use it in GitHub Desktop.
Image noise reduction
This file contains 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 UIImage { | |
var noiseReducted: UIImage? { | |
guard let openGLContext = EAGLContext(api: .openGLES2) else { return self } | |
let ciContext = CIContext(eaglContext: openGLContext) | |
guard let noiseReduction = CIFilter(name: "CINoiseReduction") else { return self } | |
noiseReduction.setValue(CIImage(image: self), forKey: kCIInputImageKey) | |
noiseReduction.setValue(0.02, forKey: "inputNoiseLevel") | |
noiseReduction.setValue(0.40, forKey: "inputSharpness") | |
if let output = noiseReduction.outputImage, | |
let cgImage = ciContext.createCGImage(output, from: output.extent) { | |
return UIImage(cgImage: cgImage, scale: scale, orientation: imageOrientation) | |
} | |
return nil | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment