失败,因为CGImage是零

时间:2018-03-04 08:05:09

标签: ios swift ocr tesseract core-image

我从不同的控制器'图像'传递UIImage。然后我正在处理图像,但是当我将它传递给下一个执行识别的方法时,它崩溃了,因为它没有得到图像。我得到的错误信息是:

[api] - [CIImage initWithCGImage:options:]失败,因为CGImage为零。 请在尝试识别前调用SetImage。 检查 - [Tesseract setImage:]是否传递大于0x0的图像。

performImageRecognition方法期待一个_ image:UIImage!通过

            let originalImage = image
            let docImage = CIImage(image: originalImage!)!
            var rect: CIRectangleFeature = CIRectangleFeature()

            let ciContext = CIContext(options: nil)

            if let detector = CIDetector(ofType: CIDetectorTypeRectangle, context: ciContext, options: [CIDetectorAccuracy: CIDetectorAccuracyHigh]) {

                rect = detector.features(in: docImage).first as! CIRectangleFeature
            }

            // Second step: Prepare transformation:
            let perspectiveCorrection = CIFilter(name: "CIPerspectiveCorrection")!
            _ = CIImage(image: originalImage!)!

            perspectiveCorrection.setValue(CIVector(cgPoint:rect.topLeft),
                                           forKey: "inputTopLeft")
            perspectiveCorrection.setValue(CIVector(cgPoint:rect.topRight),
                                           forKey: "inputTopRight")
            perspectiveCorrection.setValue(CIVector(cgPoint:rect.bottomRight),
                                           forKey: "inputBottomRight")
            perspectiveCorrection.setValue(CIVector(cgPoint:rect.bottomLeft),
                                           forKey: "inputBottomLeft")
            perspectiveCorrection.setValue(docImage,
                                           forKey: kCIInputImageKey)

            let outputImage = perspectiveCorrection.outputImage



            // Optional forth step: Adjust contrast, brightness and saturation for better image recognition:
            let finalImage = CIFilter(name: "CIColorControls", withInputParameters: [
                kCIInputImageKey: outputImage!,
                kCIInputBrightnessKey: NSNumber(value: 0.0),
                kCIInputSaturationKey: NSNumber(value: 0.0),
                kCIInputContrastKey:   NSNumber(value: 1.14)]
                )?.outputImage

    let updatedImage = UIImage(ciImage: finalImage!)

    photo.image = updatedImage

    performImageRecognition(updatedImage)

0 个答案:

没有答案