【问题标题】:getting error after processing image. iOS, Swift处理图像后出现错误。 iOS、斯威夫特
【发布时间】:2017-10-08 15:24:59
【问题描述】:

当我尝试处理图像并发送到 Swift OCR 时出现此错误。

NSAssert( widthOfImage > 0 && heightOfImage > 0, @"Passed image must not be empty - it should be at least 1px tall and wide");

如果我绕过 handle rectangles 函数并在拍摄第一张图像时调用 swift OCR 函数,它可以正常工作,但是在通过函数 processImage 放置图像后,它会因上述错误而崩溃。

这是我的功能。

lazy var rectanglesRequest: VNDetectRectanglesRequest = {
    print("Tony 1 Requested....")
    return VNDetectRectanglesRequest(completionHandler: self.handleRectangles)

}()

@objc func processImage() {
    finalImage = nil
    //      finalImage = main.correctedImageView.image

    guard let uiImage = correctedImageView.image
        else { fatalError("no image from image picker") }
    guard let ciImage = CIImage(image: uiImage)
        else { fatalError("can't create CIImage from UIImage") }
    let orientation = CGImagePropertyOrientation(uiImage.imageOrientation)
    inputImage = ciImage.oriented(forExifOrientation: Int32(orientation.rawValue))

    // Show the image in the UI.
    //  imageView.image = uiImage

    // Run the rectangle detector, which upon completion runs the ML classifier.
    let handler = VNImageRequestHandler(ciImage: ciImage, orientation: CGImagePropertyOrientation(rawValue: UInt32(Int32(orientation.rawValue)))!)
    DispatchQueue.global(qos: .userInteractive).async {
        do {
            try handler.perform([self.rectanglesRequest])
        } catch {
            print(error)
        }
    }
}




func handleRectangles(request: VNRequest, error: Error?) {
    guard let observations = request.results as? [VNRectangleObservation]
        else { fatalError("unexpected result type from VNDetectRectanglesRequest") }
    guard let detectedRectangle = observations.first else {
        //          DispatchQueue.main.async {
        //              self.classificationLabel.text = "No rectangles detected."
        //          }
        return
    }

    let imageSize = inputImage.extent.size

    // Verify detected rectangle is valid.
    let boundingBox = detectedRectangle.boundingBox.scaled(to: imageSize)
    guard inputImage.extent.contains(boundingBox)
        else { print("invalid detected rectangle"); return }

    // Rectify the detected image and reduce it to inverted grayscale for applying model.
    let topLeft = detectedRectangle.topLeft.scaled(to: imageSize)
    let topRight = detectedRectangle.topRight.scaled(to: imageSize)
    let bottomLeft = detectedRectangle.bottomLeft.scaled(to: imageSize)
    let bottomRight = detectedRectangle.bottomRight.scaled(to: imageSize)
    let correctedImage = inputImage
        .cropped(to: boundingBox)
        .applyingFilter("CIPerspectiveCorrection", parameters: [
            "inputTopLeft": CIVector(cgPoint: topLeft),
            "inputTopRight": CIVector(cgPoint: topRight),
            "inputBottomLeft": CIVector(cgPoint: bottomLeft),
            "inputBottomRight": CIVector(cgPoint: bottomRight)
            ])


    // Show the pre-processed image
    DispatchQueue.main.async {
        print("Tony: 1 adding image")
        self.finalImage = UIImage(ciImage: correctedImage)
        self.FinalizedImage.image = self.finalImage

        //          }else {
        //              print("Tony: No corected image......")

        if self.FinalizedImage.image != nil {

        print("Tony: 2 Got here to OCR")
        self.perform(#selector(self.startOCR), with: nil, afterDelay: 1.0)
    }
}
}

使用此 OCR 功能

@objc func startOCR() {
    print("Tony: OCR called")
            if self.FinalizedImage.image != nil {
        swiftOCRInstance.recognize(FinalizedImage.image!) {recognizedString in
            self.classificationLabel.text = recognizedString
            print("Tony: \(recognizedString)")
            }
        }else {
            print("Tony: No image here")
        }
    }

【问题讨论】:

  • 遇到类似问题,你们都解决了吗?
  • 嗨,DaE,我能够保存它,我忘了添加答案。因为 ciImage 只是数据,它告诉图像它想要对它实际未固化的图像进行什么过滤器工作图像,所以我必须先这样做。希望我添加的答案有意义...
  • 只要在运行OCR之前将过滤器中的ciImage变成cgImage然后返回uiImage就可以了

标签: image image-processing ocr


【解决方案1】:

我能够弄清楚这一点。我必须将图像从 ciImage 转换为 cgImage,然后再转换回 uiImage。因为 ciImage 只是过滤器在处理后如何影响图像的数据,所以我需要先固化它。

a ciImage var inputImage 或 make input image = 您正在使用的另一个 ciImage

inputImage = correctedImage

    // Show the pre-processed image
    DispatchQueue.main.async {
        print("Tony: 1 adding image")

        let cgImage = self.context.createCGImage(self.inputImage, from: self.inputImage.extent)
        self.finalImageView.image =  UIImage(cgImage: cgImage!)


        //Filter Logic
        let currentFilter = CIFilter(name: "CISharpenLuminance")
        currentFilter!.setValue(CIImage(image: UIImage(cgImage: cgImage!)), forKey: kCIInputImageKey)


        let output = currentFilter!.outputImage
        let cgimg = self.context.createCGImage(output!, from: output!.extent)
        let processedImage = UIImage(cgImage: cgimg!)
        self.finalImageView.image = processedImage

        // Then start the OCR work using finalImageView.image as the input image of the OCR

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-26
    • 1970-01-01
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 1970-01-01
    • 2017-07-22
    • 2017-03-24
    相关资源
    最近更新 更多