Exemplo n.º 1
0
        private void loadImageButton_Click(object sender, EventArgs e)
        {
            if (openImageFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Bgr drawColor = new Bgr(Color.Blue);
                try
                {
                    Image <Bgr, Byte> image = new Image <Bgr, byte>(openImageFileDialog.FileName);

                    using (Image <Gray, byte> gray = image.Convert <Gray, Byte>())
                    {
                        _ocr.Recognize(gray);
                        Tesseract.Charactor[] charactors = _ocr.GetCharactors();
                        foreach (Tesseract.Charactor c in charactors)
                        {
                            image.Draw(c.Region, drawColor, 1);
                        }

                        imageBox1.Image = image;

                        //String text = String.Concat( Array.ConvertAll(charactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                        String text = _ocr.GetText();
                        ocrTextBox.Text = text;
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }
Exemplo n.º 2
0
 public Tester(string path, bool languagePolish = false)
 {
     _path        = path;
     OCRSpaceTxt  = OCRSpace.Recognize(path, languagePolish);
     PumaNETTxt   = PumaNET.Recognize(path, languagePolish);
     TesseractTxt = Tesseract.Recognize(path);
     show();
 }
Exemplo n.º 3
0
Arquivo: Form1.cs Projeto: netonjm/OCR
 void RecognizeText(Bitmap image)
 {
     using (Image <Gray, byte> gray = new Image <Gray, Byte>(image))
     {
         _ocRz.Recognize(gray);
         textBox1.Text = _ocRz.GetText().Replace(Environment.NewLine, " ");
     }
 }
Exemplo n.º 4
0
        static void Main(string[] args)
        {
            // _ocr = new Tesseract("tessdata", "eng", Tesseract.OcrEngineMode.OEM_TESSERACT_CUBE_COMBINED);
            try
            {
                var image = new Image <Bgr, byte>("IMAG0277.jpg");

                using (Image <Gray, byte> gray = image.Convert <Gray, Byte>())
                {
                    _ocr.Recognize(gray);
                    Tesseract.Charactor[] charactors = _ocr.GetCharactors();

                    //String text = String.Concat( Array.ConvertAll(charactors, delegate(Tesseract.Charactor t) { return t.Text; }) );
                    String text = _ocr.GetText();
                }
            }
            catch (Exception exception)
            {
                MessageBox.Show(exception.Message);
            }
        }
Exemplo n.º 5
0
        private static String OcrImage(Tesseract ocr, Mat image, OCRMode mode, Mat imageColor)
        {
            Bgr drawCharColor = new Bgr(Color.Red);

            if (image.NumberOfChannels == 1)
            {
                CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr);
            }
            else
            {
                image.CopyTo(imageColor);
            }

            if (mode == OCRMode.FullPage)
            {
                ocr.SetImage(imageColor);

                if (ocr.Recognize() != 0)
                {
                    throw new Exception("Failed to recognizer image");
                }

                Tesseract.Character[] characters = ocr.GetCharacters();
                if (characters.Length == 0)
                {
                    Mat imgGrey = new Mat();
                    CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray);
                    Mat imgThresholded = new Mat();
                    CvInvoke.Threshold(imgGrey, imgThresholded, 65, 255, ThresholdType.Binary);
                    ocr.SetImage(imgThresholded);
                    characters = ocr.GetCharacters();
                    imageColor = imgThresholded;
                    if (characters.Length == 0)
                    {
                        CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary);
                        ocr.SetImage(imgThresholded);
                        characters = ocr.GetCharacters();
                        imageColor = imgThresholded;
                    }
                }
                foreach (Tesseract.Character c in characters)
                {
                    CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                }

                return(ocr.GetUTF8Text());
            }
            else
            {
                bool checkInvert = true;

                Rectangle[] regions;

                using (
                    ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f))
                    using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f))
                    {
                        int    channelCount = image.NumberOfChannels;
                        UMat[] channels     = new UMat[checkInvert ? channelCount * 2 : channelCount];

                        for (int i = 0; i < channelCount; i++)
                        {
                            UMat c = new UMat();
                            CvInvoke.ExtractChannel(image, c, i);
                            channels[i] = c;
                        }

                        if (checkInvert)
                        {
                            for (int i = 0; i < channelCount; i++)
                            {
                                UMat c = new UMat();
                                CvInvoke.BitwiseNot(channels[i], c);
                                channels[i + channelCount] = c;
                            }
                        }

                        VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length];
                        for (int i = 0; i < regionVecs.Length; i++)
                        {
                            regionVecs[i] = new VectorOfERStat();
                        }

                        try
                        {
                            for (int i = 0; i < channels.Length; i++)
                            {
                                er1.Run(channels[i], regionVecs[i]);
                                er2.Run(channels[i], regionVecs[i]);
                            }
                            using (VectorOfUMat vm = new VectorOfUMat(channels))
                            {
                                regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz,
                                                              "trained_classifier_erGrouping.xml", 0.5f);
                            }
                        }
                        finally
                        {
                            foreach (UMat tmp in channels)
                            {
                                if (tmp != null)
                                {
                                    tmp.Dispose();
                                }
                            }
                            foreach (VectorOfERStat tmp in regionVecs)
                            {
                                if (tmp != null)
                                {
                                    tmp.Dispose();
                                }
                            }
                        }

                        Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size);
                        for (int i = 0; i < regions.Length; i++)
                        {
                            Rectangle r = ScaleRectangle(regions[i], 1.1);

                            r.Intersect(imageRegion);
                            regions[i] = r;
                        }
                    }


                List <Tesseract.Character> allChars = new List <Tesseract.Character>();
                String allText = String.Empty;
                foreach (Rectangle rect in regions)
                {
                    using (Mat region = new Mat(image, rect))
                    {
                        ocr.SetImage(region);
                        if (ocr.Recognize() != 0)
                        {
                            throw new Exception("Failed to recognize image");
                        }
                        Tesseract.Character[] characters = ocr.GetCharacters();

                        //convert the coordinates from the local region to global
                        for (int i = 0; i < characters.Length; i++)
                        {
                            Rectangle charRegion = characters[i].Region;
                            charRegion.Offset(rect.Location);
                            characters[i].Region = charRegion;
                        }
                        allChars.AddRange(characters);

                        allText += ocr.GetUTF8Text() + Environment.NewLine;
                    }
                }

                Bgr drawRegionColor = new Bgr(Color.Red);
                foreach (Rectangle rect in regions)
                {
                    CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar);
                }
                foreach (Tesseract.Character c in allChars)
                {
                    CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                }

                return(allText);
            }
        }
Exemplo n.º 6
0
        private void loadImageButton_Click(object sender, EventArgs e)
        {
            if (openImageFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileNameTextBox.Text = openImageFileDialog.FileName;
                imageBox1.Image      = null;
                ocrTextBox.Text      = String.Empty;
                hocrTextBox.Text     = String.Empty;

                Bgr drawCharColor = new Bgr(Color.Blue);
                try
                {
                    Mat image = new Mat(openImageFileDialog.FileName, ImreadModes.AnyColor);

                    Mat imageColor = new Mat();
                    if (image.NumberOfChannels == 1)
                    {
                        CvInvoke.CvtColor(image, imageColor, ColorConversion.Gray2Bgr);
                    }
                    else
                    {
                        image.CopyTo(imageColor);
                    }

                    if (Mode == OCRMode.FullPage)
                    {
                        _ocr.Recognize(image);
                        Tesseract.Character[] characters = _ocr.GetCharacters();
                        if (characters.Length == 0)
                        {
                            Mat imgGrey = new Mat();
                            CvInvoke.CvtColor(image, imgGrey, ColorConversion.Bgr2Gray);
                            Mat imgThresholded = new Mat();
                            CvInvoke.Threshold(imgGrey, imgThresholded, 65, 255, ThresholdType.Binary);
                            _ocr.Recognize(imgThresholded);
                            characters = _ocr.GetCharacters();
                            imageColor = imgThresholded;
                            if (characters.Length == 0)
                            {
                                CvInvoke.Threshold(image, imgThresholded, 190, 255, ThresholdType.Binary);
                                _ocr.Recognize(imgThresholded);
                                characters = _ocr.GetCharacters();
                                imageColor = imgThresholded;
                            }
                        }
                        foreach (Tesseract.Character c in characters)
                        {
                            CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                        }

                        imageBox1.Image = imageColor;

                        String text = _ocr.GetText();
                        ocrTextBox.Text = text;
                        String hocrText = _ocr.GetHOCRText();
                        hocrTextBox.Text = hocrText;
                    }
                    else
                    {
                        bool checkInvert = true;

                        Rectangle[] regions;

                        using (ERFilterNM1 er1 = new ERFilterNM1("trained_classifierNM1.xml", 8, 0.00025f, 0.13f, 0.4f, true, 0.1f))
                            using (ERFilterNM2 er2 = new ERFilterNM2("trained_classifierNM2.xml", 0.3f))
                            {
                                int    channelCount = image.NumberOfChannels;
                                UMat[] channels     = new UMat[checkInvert ? channelCount * 2 : channelCount];

                                for (int i = 0; i < channelCount; i++)
                                {
                                    UMat c = new UMat();
                                    CvInvoke.ExtractChannel(image, c, i);
                                    channels[i] = c;
                                }

                                if (checkInvert)
                                {
                                    for (int i = 0; i < channelCount; i++)
                                    {
                                        UMat c = new UMat();
                                        CvInvoke.BitwiseNot(channels[i], c);
                                        channels[i + channelCount] = c;
                                    }
                                }

                                VectorOfERStat[] regionVecs = new VectorOfERStat[channels.Length];
                                for (int i = 0; i < regionVecs.Length; i++)
                                {
                                    regionVecs[i] = new VectorOfERStat();
                                }

                                try
                                {
                                    for (int i = 0; i < channels.Length; i++)
                                    {
                                        er1.Run(channels[i], regionVecs[i]);
                                        er2.Run(channels[i], regionVecs[i]);
                                    }
                                    using (VectorOfUMat vm = new VectorOfUMat(channels))
                                    {
                                        regions = ERFilter.ERGrouping(image, vm, regionVecs, ERFilter.GroupingMethod.OrientationHoriz, "trained_classifier_erGrouping.xml", 0.5f);
                                    }
                                }
                                finally
                                {
                                    foreach (UMat tmp in channels)
                                    {
                                        if (tmp != null)
                                        {
                                            tmp.Dispose();
                                        }
                                    }
                                    foreach (VectorOfERStat tmp in regionVecs)
                                    {
                                        if (tmp != null)
                                        {
                                            tmp.Dispose();
                                        }
                                    }
                                }

                                Rectangle imageRegion = new Rectangle(Point.Empty, imageColor.Size);
                                for (int i = 0; i < regions.Length; i++)
                                {
                                    Rectangle r = ScaleRectangle(regions[i], 1.1);

                                    r.Intersect(imageRegion);
                                    regions[i] = r;
                                }
                            }


                        List <Tesseract.Character> allChars = new List <Tesseract.Character>();
                        String allText = String.Empty;
                        foreach (Rectangle rect in regions)
                        {
                            using (Mat region = new Mat(image, rect))
                            {
                                _ocr.Recognize(region);
                                Tesseract.Character[] characters = _ocr.GetCharacters();

                                //convert the coordinates from the local region to global
                                for (int i = 0; i < characters.Length; i++)
                                {
                                    Rectangle charRegion = characters[i].Region;
                                    charRegion.Offset(rect.Location);
                                    characters[i].Region = charRegion;
                                }
                                allChars.AddRange(characters);

                                allText += _ocr.GetText() + Environment.NewLine;
                            }
                        }

                        Bgr drawRegionColor = new Bgr(Color.Red);
                        foreach (Rectangle rect in regions)
                        {
                            CvInvoke.Rectangle(imageColor, rect, drawRegionColor.MCvScalar);
                        }
                        foreach (Tesseract.Character c in allChars)
                        {
                            CvInvoke.Rectangle(imageColor, c.Region, drawCharColor.MCvScalar);
                        }
                        imageBox1.Image = imageColor;
                        ocrTextBox.Text = allText;
                    }
                }
                catch (Exception exception)
                {
                    MessageBox.Show(exception.Message);
                }
            }
        }