Ejemplo n.º 1
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);
        }
Ejemplo n.º 2
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("***");
                }
            }
        }