简体   繁体   English

如何使用CIDetector和Swift裁剪图像中检测到的矩形

[英]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. 我正在开发可检测身份证的应用程序,并且尝试使用ios中内置的CIDetector在实时预览中检测矩形对象。 i am using the solution explained in this tutorial here CoreImage Detectors 我正在使用本教程中在此处解释的解决方案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 Swift 3,简单的解决方案

let faceScanningArea = CGRect(x: 0, y: 0, width: 50, height: 50) 让faceScanningArea = CGRect(x:0,y:0,宽度:50,高度:50)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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