【问题标题】:Swift 3 - How do I improve image quality for Tesseract?Swift 3 - 如何提高 Tesseract 的图像质量?
【发布时间】:2017-03-15 21:20:16
【问题描述】:

我正在使用 Swift 3 构建一个移动应用程序,允许用户拍照并在生成的图像上运行 Tesseract OCR。

但是,我一直在尝试提高扫描质量,但似乎效果不佳。我已将照片分割成一个更“放大”的区域,我想识别它,甚至尝试将其设为黑白。是否有任何“增强”或优化图片质量/尺寸的策略,以便 Tesseract 可以更好地识别它?谢谢!

tesseract.image = // the camera photo here
tesseract.recognize()
print(tesseract.recognizedText)

我遇到了这些错误,不知道该怎么办:

Error in pixCreateHeader: depth must be {1, 2, 4, 8, 16, 24, 32}
Error in pixCreateNoInit: pixd not made
Error in pixCreate: pixd not made
Error in pixGetData: pix not defined
Error in pixGetWpl: pix not defined
2017-03-11 22:22:30.019717 ProjectName[34247:8754102] Cannot convert image to Pix with bpp = 64
Error in pixSetYRes: pix not defined
Error in pixGetDimensions: pix not defined
Error in pixGetColormap: pix not defined
Error in pixClone: pixs not defined
Error in pixGetDepth: pix not defined
Error in pixGetWpl: pix not defined
Error in pixGetYRes: pix not defined
Please call SetImage before attempting recognition.Please call SetImage before attempting recognition.2017-03-11 22:22:30.026605 EOB-Reader[34247:8754102] No recognized text. Check that -[Tesseract setImage:] is passed an image bigger than 0x0.

【问题讨论】:

    标签: ios swift swift3 tesseract


    【解决方案1】:

    我在 swift 3 中相当成功地使用了 tesseract,使用以下方法:

    func performImageRecognition(_ image: UIImage) {
    
        let tesseract = G8Tesseract(language: "eng")
        var textFromImage: String?
        tesseract?.engineMode = .tesseractCubeCombined
        tesseract?.pageSegmentationMode = .singleBlock
        tesseract?.image = imageView.image
        tesseract?.recognize()
        textFromImage = tesseract?.recognizedText
        print(textFromImage!)
    }
    

    我还发现预处理图像也有帮助。我在 UIImage 中添加了以下扩展

    导入 UIKit 导入CoreImage

        extension UIImage {
    
            func toGrayScale() -> UIImage {
    
                let greyImage = UIImageView()
                greyImage.image = self
                let context = CIContext(options: nil)
                let currentFilter = CIFilter(name: "CIPhotoEffectNoir")
                currentFilter!.setValue(CIImage(image: greyImage.image!), forKey: kCIInputImageKey)
                let output = currentFilter!.outputImage
                let cgimg = context.createCGImage(output!,from: output!.extent)
                let processedImage = UIImage(cgImage: cgimg!)
                greyImage.image = processedImage
    
                return greyImage.image!
            }
    
            func binarise() -> UIImage {
    
                let glContext = EAGLContext(api: .openGLES2)!
                let ciContext = CIContext(eaglContext: glContext, options: [kCIContextOutputColorSpace : NSNull()])
                let filter = CIFilter(name: "CIPhotoEffectMono")
                filter!.setValue(CIImage(image: self), forKey: "inputImage")
                let outputImage = filter!.outputImage
                let cgimg = ciContext.createCGImage(outputImage!, from: (outputImage?.extent)!)
    
                return UIImage(cgImage: cgimg!)
            }
    
            func scaleImage() -> UIImage {
    
                let maxDimension: CGFloat = 640
                var scaledSize = CGSize(width: maxDimension, height: maxDimension)
                var scaleFactor: CGFloat
    
                if self.size.width > self.size.height {
                    scaleFactor = self.size.height / self.size.width
                    scaledSize.width = maxDimension
                    scaledSize.height = scaledSize.width * scaleFactor
                } else {
                    scaleFactor = self.size.width / self.size.height
                    scaledSize.height = maxDimension
                    scaledSize.width = scaledSize.height * scaleFactor
                }
    
                UIGraphicsBeginImageContext(scaledSize)
                self.draw(in: CGRect(x: 0, y: 0, width: scaledSize.width, height: scaledSize.height))
                let scaledImage = UIGraphicsGetImageFromCurrentImageContext()
                UIGraphicsEndImageContext()
    
                return scaledImage!
            }
    
            func orientate(img: UIImage) -> UIImage {
    
                if (img.imageOrientation == UIImageOrientation.up) {
                    return img;
                }
    
                UIGraphicsBeginImageContextWithOptions(img.size, false, img.scale)
                let rect = CGRect(x: 0, y: 0, width: img.size.width, height: img.size.height)
                img.draw(in: rect)
    
                let normalizedImage : UIImage = UIGraphicsGetImageFromCurrentImageContext()!
                UIGraphicsEndImageContext()
    
                return normalizedImage
    
            }
    
        }
    

    然后在将图像传递给performImageRecognition之前调用它

    func processImage() {
    
        self.imageView.image! = self.imageView.image!.toGrayScale()
        self.imageView.image! = self.imageView.image!.binarise()
        self.imageView.image! = self.imageView.image!.scaleImage()
    }
    

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-19
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 2016-11-14
      • 2019-01-30
      相关资源
      最近更新 更多