Created
January 27, 2020 16:33
-
-
Save anupamchugh/86300f5422fa5b71b12a74ea76ea6b1f to your computer and use it in GitHub Desktop.
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
func doPerspectiveCorrection(_ observation: VNRectangleObservation, from buffer: CVImageBuffer) { | |
var ciImage = CIImage(cvImageBuffer: buffer) | |
let topLeft = observation.topLeft.scaled(to: ciImage.extent.size) | |
let topRight = observation.topRight.scaled(to: ciImage.extent.size) | |
let bottomLeft = observation.bottomLeft.scaled(to: ciImage.extent.size) | |
let bottomRight = observation.bottomRight.scaled(to: ciImage.extent.size) | |
ciImage = ciImage.applyingFilter("CIPerspectiveCorrection", parameters: [ | |
"inputTopLeft": CIVector(cgPoint: topLeft), | |
"inputTopRight": CIVector(cgPoint: topRight), | |
"inputBottomLeft": CIVector(cgPoint: bottomLeft), | |
"inputBottomRight": CIVector(cgPoint: bottomRight), | |
]) | |
let context = CIContext() | |
let cgImage = context.createCGImage(ciImage, from: ciImage.extent) | |
let output = UIImage(cgImage: cgImage!) | |
UIImageWriteToSavedPhotosAlbum(output, nil, nil, nil) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment