示例#1
0
        /// <summary>Preprocess given image if it is needed.</summary>
        /// <param name="inputImage">
        /// original input image
        /// <see cref="System.IO.FileInfo"/>
        /// </param>
        /// <param name="pageNumber">number of page to be OCRed</param>
        /// <returns>
        /// path to output image as
        /// <see cref="System.String"/>
        /// </returns>
        private String PreprocessImage(FileInfo inputImage, int pageNumber)
        {
            String tmpFileName = TesseractOcrUtil.GetTempFilePath(Guid.NewGuid().ToString(), GetExtension(inputImage));
            String path        = inputImage.FullName;

            try {
                if (GetTesseract4OcrEngineProperties().IsPreprocessingImages())
                {
                    Pix pix = ImagePreprocessingUtil.PreprocessImage(inputImage, pageNumber);
                    TesseractOcrUtil.SavePixToTempPngFile(tmpFileName, pix);
                    if (!File.Exists(System.IO.Path.Combine(tmpFileName)))
                    {
                        System.Drawing.Bitmap img = TesseractOcrUtil.ConvertPixToImage(pix);
                        if (img != null)
                        {
                            TesseractOcrUtil.SaveImageToTempPngFile(tmpFileName, img);
                        }
                    }
                }
                if (!GetTesseract4OcrEngineProperties().IsPreprocessingImages() || !File.Exists(System.IO.Path.Combine(tmpFileName
                                                                                                                       )))
                {
                    TesseractOcrUtil.CreateTempFileCopy(path, tmpFileName);
                }
                if (File.Exists(System.IO.Path.Combine(tmpFileName)))
                {
                    path = tmpFileName;
                }
            }
            catch (System.IO.IOException e) {
                LogManager.GetLogger(GetType()).Error(MessageFormatUtil.Format(Tesseract4LogMessageConstant.CANNOT_READ_INPUT_IMAGE
                                                                               , e.Message));
            }
            return(path);
        }
示例#2
0
        public virtual void TestPreprocessingConditions()
        {
            Pix pix = null;

            NUnit.Framework.Assert.IsNull(TesseractOcrUtil.ConvertToGrayscale(pix));
            NUnit.Framework.Assert.IsNull(TesseractOcrUtil.OtsuImageThresholding(pix));
            NUnit.Framework.Assert.IsNull(TesseractOcrUtil.ConvertPixToImage(pix));
            TesseractOcrUtil.DestroyPix(pix);
        }
示例#3
0
        /// <summary>
        /// Reads input file as Leptonica
        /// <see cref="Tesseract.Pix"/>
        /// and
        /// converts it to
        /// <see cref="System.Drawing.Bitmap"/>.
        /// </summary>
        /// <param name="inputImage">
        /// input image
        /// <see cref="System.IO.FileInfo"/>
        /// </param>
        /// <returns>
        /// returns a
        /// <see cref="System.Drawing.Bitmap"/>
        /// as the result
        /// </returns>
        internal static System.Drawing.Bitmap ReadAsPixAndConvertToBufferedImage(FileInfo inputImage)
        {
            Pix pix = Tesseract.Pix.LoadFromFile(inputImage.FullName);

            return(TesseractOcrUtil.ConvertPixToImage(pix));
        }