Example #1
0
        private void applyImageTransformations(FREngine.IFRPage page, ProcessingSettings settings)
        {
            checkProcessingSettingsForImage(page.ImageDocument.ImageColorType, ref settings);

            if (settings.RemoveGarbage)
            {
                page.ImageDocument.RemoveGarbage(null, settings.GarbageSize);
            }

            if (settings.RemoveNoise)
            {
                page.ImageDocument.RemoveNoise(settings.NoiseModel, settings.HasLargeCharacters);
            }

            if (settings.RemoveMotionBlur)
            {
                page.ImageDocument.RemoveMotionBlur(null);
            }

            if (settings.SuppressColor)
            {
                page.ImageDocument.SuppressColorObjects(settings.HslHueToSuppress, settings.HslSaturationBoundaryToSuppress);
            }

            if (settings.RemoveObjects)
            {
                FREngine.ObjectsColorEnum colorToRemove = FREngine.ObjectsColorEnum.OC_Blue;
                switch (settings.ColorToRemove)
                {
                case ProcessingSettings.ObjectsColorEnum.Red:
                    colorToRemove = FREngine.ObjectsColorEnum.OC_Red; break;

                case ProcessingSettings.ObjectsColorEnum.Green:
                    colorToRemove = FREngine.ObjectsColorEnum.OC_Green; break;

                case ProcessingSettings.ObjectsColorEnum.Blue:
                    colorToRemove = FREngine.ObjectsColorEnum.OC_Blue; break;

                case ProcessingSettings.ObjectsColorEnum.Yellow:
                    colorToRemove = FREngine.ObjectsColorEnum.OC_Yellow; break;
                }

                FREngine.ObjectsTypeEnum objectsTypeToRemove = FREngine.ObjectsTypeEnum.OT_Background;
                switch (settings.ObjectsTypeToRemove)
                {
                case ProcessingSettings.ObjectsTypeEnum.Full:
                    objectsTypeToRemove = FREngine.ObjectsTypeEnum.OT_Full; break;

                case ProcessingSettings.ObjectsTypeEnum.Background:
                    objectsTypeToRemove = FREngine.ObjectsTypeEnum.OT_Background; break;

                case ProcessingSettings.ObjectsTypeEnum.Stamp:
                    objectsTypeToRemove = FREngine.ObjectsTypeEnum.OT_Stamp; break;
                }

                page.ImageDocument.RemoveColorObjects(null, colorToRemove, objectsTypeToRemove);
            }
        }
Example #2
0
 // Warm-up recognition engine
 // It's good to call warm-up with all settings enabled
 public void WarmUp(string imagePath, ProcessingSettings settings)
 {
     LoadEngine();
     setStep("Warming up...");
     engine.LoadModule(FREngine.FREngineModuleEnum.FREM_ImageSupport);
     engine.LoadModule(FREngine.FREngineModuleEnum.FREM_Recognizer);
     engine.LoadModule(FREngine.FREngineModuleEnum.FREM_DocumentAnalyzer);
     engine.LoadModule(FREngine.FREngineModuleEnum.FREM_EuropeanPatterns);
 }
Example #3
0
        private void applyVisualEnhancements(FREngine.IFRPage page, ProcessingSettings settings)
        {
            checkProcessingSettingsForImage(page.ImageDocument.ImageColorType, ref settings);

            if (settings.EqualizeBrightness)
            {
                page.ImageDocument.EqualizeBrightness(settings.MakeWhiteBackground);
            }

            if (settings.SmoothTexture)
            {
                page.ImageDocument.SmoothTexture();
            }

            if (settings.ConvertToBW && settings.EnableDithering)
            {
                page.ImageDocument.DitherImage(FREngine.DitheringMethodEnum.DM_FloydSteinberg);
            }
        }
Example #4
0
        public RecognitionStatistics Run(string sourceImagePath, string language = "English", bool exportImage = false, bool drawTable = false, bool drawBlock = false, bool drawLine = false)
        {
            try
            {
                using (Processor processor = new Processor())
                {
                    ProcessingSettings initialSettings = new ProcessingSettings();
                    initialSettings.IsRecognize = true;     //recognitionCheckBox.Checked;
                    initialSettings.Language    = language; //languageComboBox.Text;

                    ProcessingSettings modifiedSettings = new ProcessingSettings();
                    modifiedSettings.IsRecognize          = true;     // recognitionCheckBox.Checked;
                    modifiedSettings.Language             = language; //languageComboBox.Text;
                    modifiedSettings.CropImage            = false;    //cropImageCheckBox.Checked;
                    modifiedSettings.EnhanceLocalContrast = false;    //enhanceContrastCheckBox.Checked;
                    modifiedSettings.DeskewImage          = true;     //deskewImageCheckBox.Checked;
                    //if (correctOrientationCheckBox.Checked)
                    //{
                    modifiedSettings.CorrectOrientationMode = (ProcessingSettings.OrientationCorrectionMode)ProcessingSettings.OrientationCorrectionMode.Automatic;
                    //}
                    //else
                    //{
                    //    modifiedSettings.CorrectOrientationMode = ProcessingSettings.OrientationCorrectionMode.None;
                    //}
                    //if (removeGarbageCheckBox.Checked)
                    //{
                    modifiedSettings.RemoveGarbage = true;
                    int size = 0;//garbageUpDown.Value;
                    modifiedSettings.GarbageSize = size == 0 ? -1 : size;
                    //}
                    //else
                    //{
                    //    modifiedSettings.RemoveGarbage = false;
                    //}
                    //if (correctResolutionCheckBox.Checked)
                    //{
                    modifiedSettings.CorrectResolution = true;
                    modifiedSettings.NewResolution     = 0;//(int)resolutionUpDown.Value;
                    //}
                    //else
                    //{
                    //    modifiedSettings.CorrectResolution = false;
                    //}
                    modifiedSettings.SplitPages         = false; //splitPagesCheckBox.Checked;
                    modifiedSettings.SplitBusinessCards = false; //splitBusinessCardsCheckBox.Checked;
                    modifiedSettings.InvertImage        = false; //invertImageCheckBox.Checked;
                    //if (suppressColorCheckBox.Checked)
                    //{
                    //    modifiedSettings.SuppressColor = true;
                    //    modifiedSettings.HslHueToSuppress = (byte)hueUpDown.Value;
                    //    modifiedSettings.HslSaturationBoundaryToSuppress = (byte)saturationBoundaryUpDown.Value;
                    //}
                    //else
                    //{
                    modifiedSettings.SuppressColor = false;
                    //}
                    //if (removeObjectsCheckBox.Checked)
                    //{
                    //    modifiedSettings.RemoveObjects = true;
                    //    modifiedSettings.ColorToRemove = (ProcessingSettings.ObjectsColorEnum)colorComboBox.SelectedItem;
                    //    modifiedSettings.ObjectsTypeToRemove = (ProcessingSettings.ObjectsTypeEnum)typeComboBox.SelectedItem;
                    //}
                    //else
                    //{
                    modifiedSettings.RemoveObjects = false;
                    //}
                    modifiedSettings.CorrectDistortions = false; //correctDistortionsCheckBox.Checked;
                    modifiedSettings.RemoveNoise        = false; //removeNoiseCheckBox.Checked;
                    //if (noiseModelComboBox.Text == "White noise")
                    //{
                    //    modifiedSettings.NoiseModel = FREngine.NoiseModelEnum.NM_WhiteNoise;
                    //}
                    //else
                    //{
                    //    modifiedSettings.NoiseModel = FREngine.NoiseModelEnum.NM_CorrelatedNoise;
                    //}
                    modifiedSettings.HasLargeCharacters = false; // hasLargeCharactersCheckBox.Checked;
                    modifiedSettings.RemoveMotionBlur   = false; //removeMotionBlurCheckBox.Checked;
                    //if (convertToBWCheckBox.Checked)
                    //{
                    modifiedSettings.ConvertToBW     = true;
                    modifiedSettings.EnableDithering = true;//enableDitheringCheckBox.Checked;
                    //}
                    //else
                    //{
                    //    modifiedSettings.ConvertToBW = false;
                    //}
                    modifiedSettings.SmoothTexture       = true; //smoothTextureCheckBox.Checked;
                    modifiedSettings.EqualizeBrightness  = true; // equalizeBrightnessCheckBox.Checked;
                    modifiedSettings.MakeWhiteBackground = true; // makeWhiteBackgroundCheckBox.Checked;

                    processor.WarmUp(sourceImagePath, modifiedSettings);

                    //processor.Process(sourceImagePath, initialSettings);



                    RecognitionStatistics modifiedStatic = processor.Process(sourceImagePath, modifiedSettings);

                    CustomFile csFile = new CustomFile();
                    foreach (var imagesItem in modifiedStatic.RecognizedText)
                    {
                        //csFile.ImageId = imageID;
                        csFile.PageItems.Add(imagesItem.Page);
                    }
                    modifiedStatic.File = csFile;

                    //WriteXML(csFile, AppDomain.CurrentDomain.BaseDirectory + @"OCR\XML\" + Path.GetFileNameWithoutExtension(sourceImagePath) + ".xml");
                    //if (exportImage)
                    //{
                    //    Export(sourceImagePath, modifiedStatic, drawTable, drawBlock, drawLine);
                    //}
                    return(modifiedStatic);
                }
            }
            finally
            {
            }
        }
Example #5
0
        private int[] splitImage(FREngine.IFRDocument frDoc, FREngine.IPageProcessingParams ppp, ProcessingSettings settings)
        {
            FREngine.PageSplittingParams pageSplittingParams = engine.CreatePageSplittingParams();
            pageSplittingParams.SplitType = FREngine.PageSplitTypeEnum.PST_None;
            if (settings.SplitPages)
            {
                pageSplittingParams.SplitType = FREngine.PageSplitTypeEnum.PST_DoublePageSplit;
            }
            else if (settings.SplitBusinessCards)
            {
                pageSplittingParams.SplitType = FREngine.PageSplitTypeEnum.PST_BusinessCardSplit;
            }
            else
            {
                int[] emptyPageIndices = new int[1];
                emptyPageIndices[0] = 0;
                return(emptyPageIndices);
            }
            if (settings.CorrectOrientationMode != ProcessingSettings.OrientationCorrectionMode.None)
            {
                ppp.PagePreprocessingParams.CorrectOrientation = true;
            }

            engine.MultiProcessingParams.MultiProcessingMode = FREngine.MultiProcessingModeEnum.MPM_Sequential;
            FREngine.SplitRegions splitRegions = frDoc.SplitPages(null, pageSplittingParams, ppp.ObjectsExtractionParams);
            engine.MultiProcessingParams.MultiProcessingMode = FREngine.MultiProcessingModeEnum.MPM_Auto;

            int[] sourcePageIndices = new int[frDoc.Pages.Count];
            for (int pageIndex = 0; pageIndex < sourcePageIndices.Length; pageIndex++)
            {
                sourcePageIndices[pageIndex] = -1;
            }

            foreach (FREngine.SplitRegion item in splitRegions)
            {
                FREngine.IFRPage frPage = frDoc.Pages[item.CurrentPageIndex];
                sourcePageIndices[item.CurrentPageIndex] = item.SourcePageIndex;

                if (settings.CropImage)
                {
                    frPage.ImageDocument.CropImage();
                }

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

                if (settings.DeskewImage)
                {
                    frPage.ImageDocument.CorrectSkew(correctSkewFlags);
                }

                frPage.Flush(true);
            }

            for (int pageIndex = 0; pageIndex < sourcePageIndices.Length; pageIndex++)
            {
                if (sourcePageIndices[pageIndex] == -1)
                {
                    sourcePageIndices[pageIndex] = pageIndex == 0 ? 0 : sourcePageIndices[pageIndex - 1] + 1;
                }
            }
            return(sourcePageIndices);
        }
Example #6
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 #7
0
        private void checkProcessingSettingsForImage(FREngine.ImageColorTypeEnum imageColorType, ref ProcessingSettings settings)
        {
            string unavailableOperations = "";

            if (imageColorType == FREngine.ImageColorTypeEnum.ICT_BlackWhite)
            {
                if (settings.RemoveNoise)
                {
                    settings.RemoveNoise   = false;
                    unavailableOperations += "Remove noise, ";
                }
                if (settings.RemoveMotionBlur)
                {
                    settings.RemoveMotionBlur = false;
                    unavailableOperations    += "Remove motion blur, ";
                }
                if (settings.EqualizeBrightness)
                {
                    settings.EqualizeBrightness = false;
                    unavailableOperations      += "Equalize brightness, ";
                }
                if (settings.EnhanceLocalContrast)
                {
                    settings.EnhanceLocalContrast = false;
                    unavailableOperations        += "Enhance local contrast, ";
                }
                if (settings.SuppressColor)
                {
                    settings.SuppressColor = false;
                    unavailableOperations += "Suppress color, ";
                }
                if (settings.RemoveObjects)
                {
                    settings.RemoveObjects = false;
                    unavailableOperations += "Remove objects, ";
                }
                if (unavailableOperations != "")
                {
                    unavailableOperations = unavailableOperations.Remove(unavailableOperations.Length - 2, 2);
                    unavailableOperations = "These operations will not be applied to a black-and-white image: " + unavailableOperations;
                }
            }
            else if (imageColorType == FREngine.ImageColorTypeEnum.ICT_Gray)
            {
                if (settings.SuppressColor)
                {
                    settings.SuppressColor = false;
                    unavailableOperations += "Suppress color, ";
                }
                if (settings.RemoveObjects)
                {
                    settings.RemoveObjects = false;
                    unavailableOperations += "Remove objects, ";
                }
                if (unavailableOperations != "")
                {
                    unavailableOperations = unavailableOperations.Remove(unavailableOperations.Length - 2, 2);
                    unavailableOperations = "These operations will not be applied to a gray image: " + unavailableOperations;
                }
            }
            if (unavailableOperations != "")
            {
                //MessageBox.Show(null, unavailableOperations, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }
Example #8
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);
        }