Skip to content

Instantly share code, notes, and snippets.

@ozgurshn
Created December 16, 2019 12:51
Show Gist options
  • Select an option

  • Save ozgurshn/e7be0487df6496d78b533dd690d70421 to your computer and use it in GitHub Desktop.

Select an option

Save ozgurshn/e7be0487df6496d78b533dd690d70421 to your computer and use it in GitHub Desktop.
Core image perspective transform on cvpixelbuffer
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