简体   繁体   中英

How to crop detected rectangle in Image with CIDetector and Swift

I am working on an app that detect id cards and i am trying to use CIDetector built in ios to detect rectangle shape objects on live preview. i am using the solution explained in this tutorial here CoreImage Detectors

i am getting the flowing result image

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

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
}

Swift 3, simple solution

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM