How to crop a detected rectangle in an image using CIDetector and Swift

I am working on an application that detects ID cards, and I'm trying to use the CIDetector built into ios to detect rectangular objects in preview mode. I am using the solution described in this tutorial here CoreImage Detectors

I get the current result image

My question is: is there a way to extract and crop the detected rectangle?

+4
source share
2 answers
func cropBusinessCardForPoints(image: CIImage, topLeft: CGPoint, topRight: CGPoint, bottomLeft: CGPoint, bottomRight: CGPoint) -> CIImage {

    var businessCard: CIImage
    businessCard = image.imageByApplyingFilter(
        "CIPerspectiveTransformWithExtent",
        withInputParameters: [
            "inputExtent": CIVector(CGRect: image.extent),
            "inputTopLeft": CIVector(CGPoint: topLeft),
            "inputTopRight": CIVector(CGPoint: topRight),
            "inputBottomLeft": CIVector(CGPoint: bottomLeft),
            "inputBottomRight": CIVector(CGPoint: bottomRight)])
    businessCard = image.imageByCroppingToRect(businessCard.extent)

    return businessCard
}
+5
source

Swift 3, a simple solution

let faceScanningArea = CGRect (x: 0, y: 0, : 50, : 50)

theFaceFrame.image = UIImage (cgImage (orginalImage.image?.cgImage)!. cropping (to: faceScanningArea)!)

0

All Articles