6

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 ?

Ayoub
  • 138
  • 1
  • 3
  • 8
  • hi @Ayoub i followed links shared by you to detect rectangle in image but it is not as accurate as your result is what else we have to do apart from solution given in above link? – Pooja M. Bohora Oct 24 '18 at 06:05

2 Answers2

6
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
}
Olcay Ertaş
  • 5,987
  • 8
  • 76
  • 112
Henry P.
  • 222
  • 1
  • 2
  • 9
-1

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)!)

Jose Paredes
  • 329
  • 1
  • 8