Beispiel #1
0
        void CharacterTest(OCVGridDefinition gridDef)
        {
            const string dataPath = @"C:\Program Files (x86)\Tesseract-OCR\tessdata";

            //create OCR engine
            _ocr = new Tesseract(dataPath, "eng", OcrEngineMode.TesseractLstmCombined);
            _ocr.SetVariable("tessedit_char_whitelist", "12345");


            Image <Gray, Byte> image23 = uimage.ToImage <Gray, Byte>();

            using (StreamWriter sw = new StreamWriter("ocr.txt"))
            {
                sw.Write("0, 0 (4) ");
                TestRowCol(image23, gridDef, 0, 0, sw);
                sw.Write("0, 2 (1) ");
                TestRowCol(image23, gridDef, 0, 2, sw);
                sw.Write("0, 5 (4) ");
                TestRowCol(image23, gridDef, 0, 5, sw);
                sw.Write("1, 4 (5) ");
                TestRowCol(image23, gridDef, 1, 4, sw);
                sw.Write("3, 0 (2) ");
                TestRowCol(image23, gridDef, 3, 0, sw);
                sw.Write("4, 2 (2) ");
                TestRowCol(image23, gridDef, 4, 2, sw);
                sw.Write("5, 4 (4) ");
                TestRowCol(image23, gridDef, 5, 4, sw);
            }
        }
Beispiel #2
0
        public OCVGridDefinition Analyze()
        {
            OCVGridDefinition tempResult = Analyze(20);

            if (tempResult.Rows > 0 && tempResult.Cols > 0)
            {
                double rowsize = tempResult.Width / (tempResult.Rows + 1);
                double colsize = tempResult.Width / (tempResult.Cols + 1);
                return(Analyze(((rowsize > colsize) ? rowsize : colsize) - 5));
            }
            return(null);
        }
Beispiel #3
0
 void TestRowCol(Image <Gray, Byte> image, OCVGridDefinition gridDef, int row, int col, StreamWriter sw)
 {
     image.ROI = new Rectangle((int)(gridDef.TopLeft.X + col * gridDef.ColSize - 5)
                               , (int)(gridDef.TopLeft.Y + row * gridDef.RowSize - 5)
                               , (int)(gridDef.ColSize - 10),
                               (int)(gridDef.RowSize - 10));
     _ocr.SetImage(image);
     if (_ocr.Recognize() != 0)
     {
         throw new Exception("Failed to recognize zie image");
     }
     Tesseract.Character[] characters = _ocr.GetCharacters();
     sw.WriteLine("{0} characters recognized", characters.Length);
     foreach (Tesseract.Character ch in characters)
     {
         sw.WriteLine("{0} ({1})", ch.Text, ch.Cost);
     }
 }
Beispiel #4
0
        private void button2_Click(object sender, EventArgs e)
        {
            button1_Click(this, e);
            UMat edges = FindEdges();

            LineSegment2D[] lines = FindLines(edges);

            foreach (LineSegment2D line in lines)
            {
                lineImage.Draw(line, new Bgr(Color.LightGreen), 2);
            }
            pictureBox2.Image = lineImage.Bitmap;

            using (StreamWriter sw = new StreamWriter("lines0.dmp"))
            {
                foreach (LineSegment2D line in lines)
                {
                    if (line.Length > 10)
                    {
                        sw.WriteLine("line: (direction) {0} (P1) {1}  (P2) {2}   len: {3}", xyS(line.Direction.X, line.Direction.Y), xyS(line.P1.X, line.P1.Y), xyS(line.P2.X, line.P2.Y), line.Length);
                    }
                    else if (line.Length > 0)
                    {
                        sw.WriteLine("lijntje: (direction) {0} (P1) {1}  (P2) {2}   len: {3}", xyS(line.Direction.X, line.Direction.Y), xyS(line.P1.X, line.P1.Y), xyS(line.P2.X, line.P2.Y), line.Length);
                    }
                    else
                    {
                        sw.WriteLine("line: (point) {0}", xyS(line.P1.X, line.P1.Y));
                    }
                }
            }
            OCVGridData gridData = new OCVGridData(lines);

            using (StreamWriter sw = new StreamWriter("lines.dmp"))
            {
                sw.WriteLine("------------------------------------------------");
                sw.WriteLine("griddata (HorizontalLines): ");
                foreach (OCVLineData line in gridData.HorizontalLines)
                {
                    sw.WriteLine("line: Y = {0}, X1 = {1} X2 = {2} len = {3}", line.GetY(), line.GetMinValue(), line.GetMaxValue(), line.Length);
                }
                sw.WriteLine("------------------------------------------------");
                sw.WriteLine("griddata (Vertical Lines: ");
                foreach (OCVLineData line in gridData.VerticalLines)
                {
                    sw.WriteLine("line: X = {0}, Y1 = {1} Y2 = {2} len = {3}", line.GetX(), line.GetMinValue(), line.GetMaxValue(), line.Length);
                }
                sw.WriteLine("------------------------------------------------");
            }
            OCVGrid grid = new OCVGrid(gridData);

            using (StreamWriter sw = new StreamWriter("grid.dmp"))
            {
                grid.Dump(sw);
            }

            Image <Bgr, Byte> image3 = lineImage.CopyBlank();

            foreach (OCVCombinedLinesData line in grid.HorizontalLines)
            {
                OCVLineData summ = line.GetSummaryLine();
                image3.Draw(summ.Line, new Bgr(Color.Azure), 2);
            }
            foreach (OCVCombinedLinesData line in grid.VerticalLines)
            {
                OCVLineData summ = line.GetSummaryLine();
                if (summ.Length > 35)
                {
                    image3.Draw(summ.Line, new Bgr(Color.LightPink), 2);
                }
            }
            pictureBox2.Image = image3.Bitmap;

            Image <Bgr, Byte> lineImage2 = img1.CopyBlank();
            OCVGridDefinition gridDef    = grid.Analyze();

            listBox1.Items.Add(String.Format("{0} x {1}", gridDef.Rows, gridDef.Cols));
            for (int r = 0; r <= gridDef.Rows; r++)
            {
                lineImage2.Draw(new LineSegment2D(new Point(gridDef.TopLeft.X, gridDef.RowLocation(r)),
                                                  new Point(gridDef.TopLeft.X + gridDef.Width, gridDef.RowLocation(r))),
                                new Bgr(Color.PeachPuff), 2);
            }
            for (int c = 0; c <= gridDef.Cols; c++)
            {
                lineImage2.Draw(new LineSegment2D(new Point(gridDef.ColLocation(c), gridDef.TopLeft.Y),
                                                  new Point(gridDef.ColLocation(c), gridDef.TopLeft.Y + +gridDef.Height)),
                                new Bgr(Color.MediumTurquoise), 2);
            }

            lineImage2.Draw(new Rectangle(gridDef.TopLeft.X, gridDef.TopLeft.Y, gridDef.Width, gridDef.Height), new Bgr(Color.White), 2);

            pictureBox3.Image = lineImage2.Bitmap;
            testingpixels(gridDef);
            CharacterTest(gridDef);
        }
Beispiel #5
0
        private void testingpixels(OCVGridDefinition gridDef)
        {
            int threshold = 250;

            Matrix <Byte> matrix = new Matrix <Byte>(uimage.Rows, uimage.Cols, uimage.NumberOfChannels);

            uimage.CopyTo(matrix);
            using (StreamWriter sw = new StreamWriter("pixels.dmp"))
            {
                sw.WriteLine("------------------ROWS-------------------");
                for (int r = 0; r < gridDef.Rows; r++)
                {
                    int rowLoc = (int)(gridDef.RowLocation(r) + gridDef.RowSize * 0.5);
                    sw.WriteLine("*** row: {0}    (loc: {1})", r, rowLoc);
                    for (int c = 1; c < gridDef.Cols; c++)
                    {
                        int loc = gridDef.ColLocation(c);
                        sw.WriteLine("col {0}  location {1}", c, loc);
                        int nBelow = 0;
                        for (int col = loc - 10; col < loc + 10; col++)
                        {
                            if (col < 0 || col >= matrix.Cols)
                            {
                                continue;
                            }
                            byte value = matrix.Data[rowLoc, col];
                            if (value < threshold)
                            {
                                nBelow++;
                            }
                            //sw.WriteLine("row: {0}  col: {1}  : {2}", row, col, value);
                        }
                        sw.WriteLine("col {0}  pct: {1} %", c, (nBelow / 20.0) * 100);
                    }
                    sw.WriteLine("***");
                }
                sw.WriteLine("------------------COLUMNS-------------------");
                for (int c = 0; c < gridDef.Cols; c++)
                {
                    int colLoc = (int)(gridDef.ColLocation(c) + gridDef.ColSize * 0.5);
                    sw.WriteLine("*** col: {0}    (loc: {1})", c, colLoc);
                    for (int r = 1; r < gridDef.Rows; r++)
                    {
                        int loc = gridDef.RowLocation(r);
                        sw.WriteLine("row {0}  location {1}", r, loc);
                        int nBelow = 0;
                        for (int row = loc - 10; row < loc + 10; row++)
                        {
                            if (row < 0 || row >= matrix.Cols)
                            {
                                continue;
                            }
                            byte value = matrix.Data[row, colLoc];
                            if (value < threshold)
                            {
                                nBelow++;
                            }
                            //sw.WriteLine("row: {0}  col: {1}  : {2}", row, col, value);
                        }
                        sw.WriteLine("row {0}  pct: {1} %", r, (nBelow / 20.0) * 100);
                    }
                    sw.WriteLine("***");
                }
            }
        }
Beispiel #6
0
        public OCVGridDefinition Analyze(double threshold)
        {
            double left = OCVConst.REALLYLARGE, top = OCVConst.REALLYLARGE, right = -OCVConst.REALLYLARGE, bottom = -OCVConst.REALLYLARGE;

            // remove lines that are too small
            RemoveSmallLines(threshold);
            // analyze horizontal lines
            int rows = -1;

            foreach (OCVCombinedLinesData linesGroup in HorizontalLines)
            {
                rows++;
                OCVLineData summaryLine = linesGroup.GetSummaryLine();
                if (summaryLine.GetMinValue() < left)
                {
                    left = summaryLine.GetMinValue();
                }
                if (summaryLine.GetMaxValue() > right)
                {
                    right = summaryLine.GetMaxValue();
                }
                if (summaryLine.GetY() > top)
                {
                    top = summaryLine.GetY();
                }
                if (summaryLine.GetY() < bottom)
                {
                    bottom = summaryLine.GetY();
                }
            }

            // analyze vertical lines
            int cols = -1;

            foreach (OCVCombinedLinesData linesGroup in VerticalLines)
            {
                cols++;
                OCVLineData summaryLine = linesGroup.GetSummaryLine();
                if (summaryLine.GetX() < left)
                {
                    left = summaryLine.GetX();
                }
                if (summaryLine.GetX() > right)
                {
                    right = summaryLine.GetX();
                }
                if (summaryLine.GetMinValue() < top)
                {
                    top = summaryLine.GetMinValue();
                }
                if (summaryLine.GetMaxValue() > bottom)
                {
                    bottom = summaryLine.GetMaxValue();
                }
            }
            OCVGridDefinition result = new OCVGridDefinition(new Point((int)left, (int)top),
                                                             new Point((int)right, (int)bottom),
                                                             rows, cols
                                                             );

            return(result);
        }