public void SetGridDef(OCVGridDefinition gridDef)
 {
     GridDef       = new OCVGridDefinition(gridDef);
     nudRows.Value = GridDef.Rows;
     nudCols.Value = GridDef.Cols;
     DrawGrid();
 }
        private int FindValueInField(Image <Gray, Byte> image, OCVGridDefinition gridDef, int row, int col)
        {
            const int MARGIN = 7;
            int       result;
            Rectangle Rect = gridRect(gridDef, row, col);

            Rect.X      += MARGIN;
            Rect.Y      += MARGIN;
            Rect.Width  -= 2 * MARGIN;
            Rect.Height -= 2 * MARGIN;
            Image <Gray, Byte> fieldImage  = image.Copy(Rect);
            Image <Gray, Byte> fieldImage2 = fieldImage.CopyBlank();

            // note: threshold should be investigated here, these may be suboptimal
            CvInvoke.Threshold(fieldImage, fieldImage2, 100, 255, ThresholdType.Binary);
            _ocr.SetImage(fieldImage2);
            if (_ocr.Recognize() != 0)
            {
                throw new Exception("Failed to recognize the image");
            }
            foreach (Tesseract.Character ch in _ocr.GetCharacters())
            {
                if (Int32.TryParse(ch.Text, out result))
                {
                    return(result);
                }
            }
            return(0);
        }
Beispiel #3
0
        public double DeltaCols(OCVGridDefinition grid)
        {
            double result = 0;

            using (StreamWriter sw = new StreamWriter("deltas.log"))
            {
                for (int c = 0; c < grid.Cols; c++)
                {
                    int location = grid.ColLocation(c);
                    sw.Write("c: {0}  location: {1} ", c, location);
                    int line0 = 0;
                    int line1 = VerticalLines.Count - 1;
                    while (line0 < line1 - 1 && VerticalLines[line0 + 1].GetSummaryLine().GetLocation() < location)
                    {
                        line0++;
                    }
                    while (line1 > line0 + 1 && (line1 == VerticalLines.Count - 1 || VerticalLines[line1 + 1].GetSummaryLine().GetLocation() > location))
                    {
                        line1--;
                    }
                    sw.Write(" line0: {0} [{1}]  line1: {2} [{3}] ", line0, VerticalLines[line0].GetSummaryLine().GetLocation(), line1, VerticalLines[line1].GetSummaryLine().GetLocation());
                    double delt = Math.Min(Math.Abs(location - VerticalLines[line0].GetSummaryLine().GetLocation()),
                                           Math.Abs(location - VerticalLines[line1].GetSummaryLine().GetLocation()));
                    sw.WriteLine("  delta = {0} ", delt);
                    result += delt;
                }
            }
            return(result / grid.Cols);
        }
Beispiel #4
0
        private int[,] FindColValues(Matrix <byte> matrix, OCVGridDefinition gridDef, int testWidth)
        {
            int[,] result = new int[gridDef.Rows, gridDef.Cols];
            int minVal, maxVal;

            for (int c = 0; c < gridDef.Cols; c++)
            {
                int colLoc = (int)(gridDef.ColLocation(c) + gridDef.ColSize * 0.5);
                result[0, c] = EXTERNALBORDER;
                for (int r = 1; r < gridDef.Rows; r++)
                {
                    int loc = gridDef.RowLocation(r);
                    minVal = Int32.MaxValue;
                    maxVal = Int32.MinValue;
                    for (int row = loc - testWidth; row < loc + testWidth; row++)
                    {
                        if (row < 0 || row >= matrix.Rows)
                        {
                            continue;
                        }
                        byte value = matrix.Data[row, colLoc];
                        if (value < minVal)
                        {
                            minVal = value;
                        }
                        if (value > maxVal)
                        {
                            maxVal = value;
                        }
                    }
                    result[r, c] = maxVal - minVal;
                }
            }
            return(result);
        }
Beispiel #5
0
        private int[,] FindRowValues(Matrix <byte> matrix, OCVGridDefinition gridDef, int testWidth)
        {
            int[,] result = new int[gridDef.Rows, gridDef.Cols];
            int minVal, maxVal;

            for (int r = 0; r < gridDef.Rows; r++)
            {
                int rowLoc = (int)(gridDef.RowLocation(r) + gridDef.RowSize * 0.5);
                for (int c = 1; c < gridDef.Cols; c++)
                {
                    result[r, 0] = EXTERNALBORDER;
                    int loc = gridDef.ColLocation(c);
                    minVal = Int32.MaxValue;
                    maxVal = Int32.MinValue;
                    for (int col = loc - testWidth; col < loc + testWidth; col++)
                    {
                        if (col < 0 || col >= matrix.Cols)
                        {
                            continue;
                        }
                        byte value = matrix.Data[rowLoc, col];
                        if (value < minVal)
                        {
                            minVal = value;
                        }
                        if (value > maxVal)
                        {
                            maxVal = value;
                        }
                    }
                    result[r, c] = maxVal - minVal;
                }
            }
            return(result);
        }
Beispiel #6
0
        public TekBorderAnalyzer(UMat matGray, OCVGridDefinition gridDef)
        {
            Matrix <Byte> matrix = new Matrix <Byte>(matGray.Rows, matGray.Cols, matGray.NumberOfChannels);

            matGray.CopyTo(matrix);
            LeftBorderValues = FindRowValues(matrix, gridDef, (int)(gridDef.ColSize * 0.1));
            TopBorderValues  = FindColValues(matrix, gridDef, (int)(gridDef.RowSize * 0.1));
            LeftAreaBorders  = AnalyzeBorderValues(LeftBorderValues, ref HorizontalThreshold);
            TopAreaBorders   = AnalyzeBorderValues(TopBorderValues, ref VerticalTreshold);
        }
Beispiel #7
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);
                RemoveInconsistentLines(HorizontalLines, colsize, 0.45);
                RemoveInconsistentLines(VerticalLines, rowsize, 0.45);
                return(AnalyzeOptimal(Analyze(((rowsize > colsize) ? rowsize : colsize) - 5)));
            }
            return(null);
        }
        public TekCharacterRecognition(UMat matGray, OCVGridDefinition gridDef)
        {
            //create OCR engine
            _ocr = new Tesseract(TessDataPath, "eng", OcrEngineMode.TesseractOnly);
            _ocr.SetVariable("tessedit_char_whitelist", "12345");
            Values = new int[gridDef.Rows, gridDef.Cols];
            Image <Gray, Byte> testImage = matGray.ToImage <Gray, Byte>().Copy();

            for (int row = 0; row < gridDef.Rows; row++)
            {
                for (int col = 0; col < gridDef.Cols; col++)
                {
                    Values[row, col] = FindValueInField(testImage, gridDef, row, col);
                }
            }
        }
 private void DrawGrid(Image <Bgr, Byte> image, OCVGridDefinition gridDef, Bgr color)
 {
     for (int r = 0; r <= gridDef.Rows; r++)
     {
         image.Draw(new LineSegment2D(new Point(gridDef.TopLeft.X, gridDef.RowLocation(r)),
                                      new Point(gridDef.TopLeft.X + gridDef.Width, gridDef.RowLocation(r))),
                    color, 2);
     }
     for (int c = 0; c <= gridDef.Cols; c++)
     {
         image.Draw(new LineSegment2D(new Point(gridDef.ColLocation(c), gridDef.TopLeft.Y),
                                      new Point(gridDef.ColLocation(c), gridDef.TopLeft.Y + gridDef.Height)),
                    color, 2);
     }
     image.Draw(new Rectangle(gridDef.TopLeft.X, gridDef.TopLeft.Y, gridDef.Width, gridDef.Height), color, 2);
 }
        public TekGridAnalyzer(Image <Bgr, Byte> imgOriginal)
        {
            matGrayScaleImage = GrayScaleImage(imgOriginal);
            cannyEdges        = FindEdges();
            Lines             = CvInvoke.HoughLinesP(
                cannyEdges,
                1,                 //Distance resolution in pixel-related units
                Math.PI / 45.0,    //Angle resolution measured in radians.
                houghThreshold,    //threshold
                houghMinLineWidth, //min Line width
                houghGap);         //gap between lines
            OCVGridData gridData = new OCVGridData(Lines);

            Grid    = new OCVGrid(gridData);
            gridDef = Grid.Analyze();
        }
Beispiel #11
0
        private OCVGridDefinition Analyze(double threshold)
        {
            double left   = OCVConst.REALLYLARGE;
            double right  = -OCVConst.REALLYLARGE;
            double top    = OCVConst.REALLYLARGE;
            double bottom = -OCVConst.REALLYLARGE;

            RemoveSmallLines(threshold);
            int rows = AnalyzeLinesGroup(HorizontalLines, ref left, ref right, ref top, ref bottom);
            int cols = AnalyzeLinesGroup(VerticalLines, ref top, ref bottom, ref left, ref right);
            OCVGridDefinition result = new OCVGridDefinition(new Point((int)left, (int)top),
                                                             new Point((int)right, (int)bottom),
                                                             rows, cols
                                                             );

            return(result);
        }
Beispiel #12
0
        public OCVGridDefinition AnalyzeOptimal(OCVGridDefinition gridDef)
        {
            double deltaRows = DeltaRows(gridDef);
            int    minRows   = gridDef.Rows;

            if (deltaRows > 1)
            {
                double            minVal = deltaRows;
                OCVGridDefinition temp   = new OCVGridDefinition(gridDef);
                for (int r = gridDef.Rows - 3; r < 3 * gridDef.Rows; r++)
                {
                    temp.Rows = r;
                    double tempVal = DeltaRows(temp);
                    if (tempVal < minVal)
                    {
                        minVal  = tempVal;
                        minRows = r;
                    }
                }
            }
            double deltaCols = DeltaCols(gridDef);
            int    minCols   = gridDef.Cols;

            if (deltaCols > 1)
            {
                double            minVal = deltaCols;
                OCVGridDefinition temp   = new OCVGridDefinition(gridDef);
                for (int c = gridDef.Cols - 3; c < 3 * gridDef.Cols; c++)
                {
                    temp.Cols = c;
                    double tempVal = DeltaCols(temp);
                    if (tempVal < minVal)
                    {
                        minVal  = tempVal;
                        minCols = c;
                    }
                }
            }
            OCVGridDefinition result = new OCVGridDefinition(gridDef);

            result.Rows = minRows;
            result.Cols = minCols;
            return(result);
        }
Beispiel #13
0
        public double DeltaRows(OCVGridDefinition grid)
        {
            double result = 0;

            for (int r = 0; r < grid.Rows; r++)
            {
                int location = grid.RowLocation(r);
                int line0    = 0;
                int line1    = HorizontalLines.Count - 1;
                while (line0 < line1 - 1 && HorizontalLines[line0 + 1].GetSummaryLine().GetLocation() < location)
                {
                    line0++;
                }
                while (line1 > line0 + 1 && (line1 == HorizontalLines.Count - 1 || HorizontalLines[line1 + 1].GetSummaryLine().GetLocation() > location))
                {
                    line1--;
                }
                result += Math.Min(Math.Abs(location - HorizontalLines[line0].GetSummaryLine().GetLocation()),
                                   Math.Abs(location - HorizontalLines[line1].GetSummaryLine().GetLocation()));
            }
            return(result / grid.Rows);
        }
 private Rectangle gridRect(OCVGridDefinition gridDef, int row, int col)
 {
     return(new Rectangle((int)gridDef.ColLocation(col), (int)gridDef.RowLocation(row), (int)gridDef.ColSize, (int)gridDef.RowSize));
 }
Beispiel #15
0
 public OCVGridDefinition(OCVGridDefinition gridDef) : this(gridDef.TopLeft, gridDef.BottomRight, gridDef.Rows, gridDef.Cols)
 {
 }