Created
December 16, 2019 12:51
-
-
Save ozgurshn/e7be0487df6496d78b533dd690d70421 to your computer and use it in GitHub Desktop.
Core image perspective transform on cvpixelbuffer
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
| guard let rectangle = request?.results?.first as? VNRectangleObservation else { | |
| guard let error = error else { return } | |
| print("Error: Rectangle detection failed - Vision request returned an error. \(error.localizedDescription)") | |
| return | |
| } | |
| guard let filter = CIFilter(name: "CIPerspectiveCorrection") else { | |
| print("Error: Rectangle detection failed - Could not create perspective correction filter.") | |
| return | |
| } | |
| let width = CGFloat(CVPixelBufferGetWidth(currentCameraImage)) | |
| let height = CGFloat(CVPixelBufferGetHeight(currentCameraImage)) | |
| let topLeft = CGPoint(x: rectangle.topLeft.x * width, y: rectangle.topLeft.y * height) | |
| let topRight = CGPoint(x: rectangle.topRight.x * width, y: rectangle.topRight.y * height) | |
| let bottomLeft = CGPoint(x: rectangle.bottomLeft.x * width, y: rectangle.bottomLeft.y * height) | |
| let bottomRight = CGPoint(x: rectangle.bottomRight.x * width, y: rectangle.bottomRight.y * height) | |
| filter.setValue(CIVector(cgPoint: topLeft), forKey: "inputTopLeft") | |
| filter.setValue(CIVector(cgPoint: topRight), forKey: "inputTopRight") | |
| filter.setValue(CIVector(cgPoint: bottomLeft), forKey: "inputBottomLeft") | |
| filter.setValue(CIVector(cgPoint: bottomRight), forKey: "inputBottomRight") | |
| let ciImage = CIImage(cvPixelBuffer: currentCameraImage).oriented(.up) | |
| filter.setValue(ciImage, forKey: kCIInputImageKey) | |
| guard let perspectiveImage: CIImage = filter.value(forKey: kCIOutputImageKey) as? CIImage else { | |
| print("Error: Rectangle detection failed - perspective correction filter has no output image.") | |
| return | |
| } | |
| delegate?.rectangleFound(rectangleContent: perspectiveImage) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment