Example #1
0
        // Detect orientation of all pages in a document
        // Return array with orientations for all pages
        private FREngine.RotationTypeEnum[] detectOrientation(string imagePath, FREngine.PrepareImageMode _prepareImageMode,
                                                              FREngine.PageProcessingParams pageProcessingParams)
        {
            // Correct skew during loading
            FREngine.PrepareImageMode prepareImageMode = engine.CreatePrepareImageMode();
            prepareImageMode.CopyFrom(_prepareImageMode);
            prepareImageMode.CorrectSkew     = true;
            prepareImageMode.CorrectSkewMode = correctSkewFlags;

            FREngine.RotationTypeEnum[] rotations  = null;
            FREngine.FRDocument         frDocument = engine.CreateFRDocument();
            try
            {
                frDocument.AddImageFile(imagePath, prepareImageMode, null);

                // Get orientation for every page
                int pagesCount = frDocument.Pages.Count;
                rotations = new FREngine.RotationTypeEnum[pagesCount];
                for (int pageIndex = 0; pageIndex < pagesCount; pageIndex++)
                {
                    FREngine.IFRPage         frPage          = frDocument.Pages[pageIndex];
                    FREngine.TextOrientation textOrientation = frPage.DetectOrientation(null, pageProcessingParams.ObjectsExtractionParams,
                                                                                        pageProcessingParams.RecognizerParams);
                    rotations[pageIndex] = FREngine.RotationTypeEnum.RT_UnknownRotation;
                    if (textOrientation != null)
                    {
                        rotations[pageIndex] = textOrientation.RotationType;
                    }
                    frPage.Flush(false);
                }
            }
            finally
            {
                frDocument.Close();
            }

            return(rotations);
        }
Example #2
0
        private void applyGeometricalTransformations(FREngine.IFRPage page, FREngine.IPageProcessingParams ppp, ProcessingSettings settings,
                                                     FREngine.RotationTypeEnum detectedRotation)
        {
            checkProcessingSettingsForImage(page.ImageDocument.ImageColorType, ref settings);

            if (settings.CropImage)
            {
                page.ImageDocument.CropImage();
            }
            if (settings.EnhanceLocalContrast)
            {
                page.ImageDocument.EnhanceLocalContrast();
            }

            if (settings.CorrectOrientationMode != ProcessingSettings.OrientationCorrectionMode.None && detectedRotation != FREngine.RotationTypeEnum.RT_NoRotation)
            {
                FREngine.RotationTypeEnum rotation = FREngine.RotationTypeEnum.RT_NoRotation;
                bool mirror = false;
                if (detectedRotation != FREngine.RotationTypeEnum.RT_UnknownRotation)
                {
                    switch (detectedRotation)
                    {
                    case FREngine.RotationTypeEnum.RT_Clockwise:
                        rotation = FREngine.RotationTypeEnum.RT_Counterclockwise;
                        break;

                    case FREngine.RotationTypeEnum.RT_Upsidedown:
                        rotation = FREngine.RotationTypeEnum.RT_Upsidedown;
                        break;

                    case FREngine.RotationTypeEnum.RT_Counterclockwise:
                        rotation = FREngine.RotationTypeEnum.RT_Clockwise;
                        break;

                    default:
                        break;
                    }
                }
                else
                {
                    switch (settings.CorrectOrientationMode)
                    {
                    case ProcessingSettings.OrientationCorrectionMode.Rotate90CW:
                        rotation = FREngine.RotationTypeEnum.RT_Clockwise;
                        break;

                    case ProcessingSettings.OrientationCorrectionMode.Rotate180UpsideDown:
                        rotation = FREngine.RotationTypeEnum.RT_Upsidedown;
                        break;

                    case ProcessingSettings.OrientationCorrectionMode.Rotate90CCW:
                        rotation = FREngine.RotationTypeEnum.RT_Counterclockwise;
                        break;

                    case ProcessingSettings.OrientationCorrectionMode.MirrorHorizontally:
                        mirror = true;
                        break;

                    default:
                        break;
                    }
                }

                page.ImageDocument.Transform(rotation, mirror, false);
            }

            if (settings.InvertImage)
            {
                page.ImageDocument.Transform(FREngine.RotationTypeEnum.RT_NoRotation, false, true);
            }

            if (settings.CorrectDistortions)
            {
                page.CorrectGeometricalDistortions(ppp.ObjectsExtractionParams);
            }

            if (settings.DeskewImage)
            {
                page.ImageDocument.CorrectSkew(correctSkewFlags);
            }
        }
Example #3
0
        public RecognitionStatistics Process(string imagePath, ProcessingSettings settings)
        {
            DateTime startTime = System.DateTime.Now;

            setStep("Applying profile...");
            engine.LoadPredefinedProfile("DocumentConversion_Accuracy");

            setStep("Applying settings...");
            FREngine.PrepareImageMode         pim = engine.CreatePrepareImageMode();
            FREngine.DocumentProcessingParams dpp = engine.CreateDocumentProcessingParams();
            FREngine.PageProcessingParams     ppp = dpp.PageProcessingParams;

            ppp.RecognizerParams.SetPredefinedTextLanguage(settings.Language);
            disableAllModifications(pim, ppp);

            pim.AutoOverwriteResolution = false;
            if (settings.CorrectResolution)
            {
                if (settings.NewResolution == 0)
                {
                    pim.AutoOverwriteResolution = true;
                }
                else if (settings.NewResolution > 0)
                {
                    pim.OverwriteResolution    = true;
                    pim.XResolutionToOverwrite = settings.NewResolution;
                    pim.YResolutionToOverwrite = settings.NewResolution;
                }
            }

            if (settings.ConvertToBW)
            {
                pim.DiscardColorImage = true;
            }
            if (settings.DeskewImage)
            {
                pim.CorrectSkew = true;
            }

            // Detect orientation for all pages
            setStep("Detecting orientation...");
            FREngine.RotationTypeEnum[] rotation = null;
            if (settings.CorrectOrientationMode == ProcessingSettings.OrientationCorrectionMode.Automatic)
            {
                rotation = detectOrientation(imagePath, pim, ppp);
            }

            setStep("Loading image...");

            // Create document
            FREngine.FRDocument   frDocument       = engine.CreateFRDocument();
            RecognitionStatistics recognitionStats = new RecognitionStatistics();

            try
            {
                // Add image file to document
                frDocument.AddImageFile(imagePath, pim, null);

                if (frDocument.Pages.Count == 0)
                {
                    throw new Exception("No pages in a file");
                }

                setStep("Performing image modification...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage          frPage       = frDocument.Pages[pageIndex];
                    FREngine.RotationTypeEnum pageRotation = FREngine.RotationTypeEnum.RT_UnknownRotation;
                    if (rotation != null && pageIndex < rotation.Length)
                    {
                        pageRotation = rotation[pageIndex];
                    }

                    applyGeometricalTransformations(frPage, ppp, settings, pageRotation);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                int[] sourcePageIndices = splitImage(frDocument, ppp, settings);

                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    applyImageTransformations(frPage, settings);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                if (settings.IsRecognize)
                {
                    setStep("Recognizing image...");
                    frDocument.Process(dpp);
                }

                setStep("Applying visual enhancements...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    applyVisualEnhancements(frPage, settings);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(true);
                }

                TimeSpan processingTime = DateTime.Now - startTime;

                setStep("Computing statistics...");

                recognitionStats = computeStatistics(frDocument);
                recognitionStats.TotalProcessingTime = processingTime;
                recognitionStats.SourcePageIndices   = sourcePageIndices;

                setStep("Retrieving images...");
                for (int pageIndex = 0; pageIndex < frDocument.Pages.Count; pageIndex++)
                {
                    if (recognitionStats.PreprocessedImages == null)
                    {
                        recognitionStats.PreprocessedImages = new System.Drawing.Image[frDocument.Pages.Count];
                    }
                    FREngine.IFRPage frPage = frDocument.Pages[pageIndex];
                    recognitionStats.PreprocessedImages[pageIndex] = getImageFromPage(frPage);

                    GC.Collect();
                    GC.WaitForPendingFinalizers();
                    frPage.Flush(false);
                }



                //frDocument.Export(AppDomain.CurrentDomain.BaseDirectory + "FileSample2.xml", FREngine.FileExportFormatEnum.FEF_XML, null);

                //frDocument.Export(AppDomain.CurrentDomain.BaseDirectory + "FileSample2.txt", FREngine.FileExportFormatEnum.FEF_TextUnicodeDefaults, null);
            }
            finally
            {
                frDocument.Close();
            }

            return(recognitionStats);
        }