Exemplo n.º 1
0
        public int Recognize(Digit digit, int cellsNumberInHeight = 3, int cellsNumberInWidth = 3)
        {
            int cellsNumberTotal = cellsNumberInHeight * cellsNumberInWidth; // 3 * 3 = 9
            digit.Cells = new Cell[cellsNumberTotal];
            int currentCellIndex = 0;

            // инициализация ячеек
            for (int i = 0; i < cellsNumberInHeight; i++)
            {
                for (int j = 0; j < cellsNumberInWidth; j++)
                {
                    digit.Cells[currentCellIndex] = new Cell()
                    {
                        iFrom = Convert.ToInt32(((double)i / (double)cellsNumberInHeight) * (double)digit.Height),
                        iTo = Convert.ToInt32((((double)i + (double)1) / (double)cellsNumberInHeight) * (double)digit.Height),
                        jFrom = Convert.ToInt32(((double)j / (double)cellsNumberInWidth) * (double)digit.Width),
                        jTo = Convert.ToInt32((((double)j + (double)1) / (double)cellsNumberInWidth) * (double)digit.Width),
                        PixAmount = 0,
                        Stat = 0.0F
                    };

                    currentCellIndex++;
                }
            }

            // подсчет количества черных пикселей для каждой ячейки
            int blackPixTotal = 0;

            foreach (Cell cell in digit.Cells)
            {
                for (int i = cell.iFrom; i < cell.iTo; i++)
                {
                    for (int j = cell.jFrom; j < cell.jTo; j++)
                    {
                        if (digit.Pixels[i, j] == 255)
                        {
                            cell.PixAmount++;
                            blackPixTotal++;
                        }
                    }
                }
            }

            // расчет распределения черных пикселей по ячейкам
            foreach (Cell cell in digit.Cells)
            {
                cell.Stat = Math.Round(((double)cell.PixAmount / (double)blackPixTotal), 3);
            }

            // вывалить digit.Pixels для проверки правильного выделения цифры
            // вывалить полученную статистику распределения в консоль
            // заполнить savedStats полученной статистикой
            //DoDebugText(digit, cells);

            // сравнить полученное значение статистики с известными
            return CompareStats(digit.Cells);
        }
Exemplo n.º 2
0
        public int Recognize(Digit digit)
        {
            int allBlackPixelCount = 0;

            int quarter1BlackPixelCount = 0;
            int quarter2BlackPixelCount = 0;
            int quarter3BlackPixelCount = 0;
            int quarter4BlackPixelCount = 0;

            float quarter1 = 0;
            float quarter2 = 0;
            float quarter3 = 0;
            float quarter4 = 0;

            int halfHeight = (int)(digit.Height / 2);
            int halfWidth = (int)(digit.Width / 2);

            int n = 0;
            int color = -1; // white

            for (int i = 0; i < digit.Height; i++)
            {
                //String str = "";
                for (int j = 0; j < digit.Width; j++)
                {
                    color = digit.Pixels[i, j];
                    if (color == 255) // black
                    {
                        allBlackPixelCount++;

                        // распределение по четвертям
                        if (j < halfWidth)
                        {
                            if (i < halfHeight)
                            {
                                quarter1BlackPixelCount++;
                            }
                            else
                            {
                                quarter3BlackPixelCount++;
                            }
                        }
                        else // if j >= halfWidth
                        {
                            if (i < halfHeight)
                            {
                                quarter2BlackPixelCount++;
                            }
                            else
                            {
                                quarter4BlackPixelCount++;
                            }
                        }

                        //str += "1";
                    }
                    //else
                    //{
                    //    str += "0";
                    //}

                    n++;
                }

                // вывод цифр в консоль в виде матрицы
                //Debug.WriteLine(str);
            }

            if (quarter1BlackPixelCount != 0 && quarter2BlackPixelCount != 0
                && quarter3BlackPixelCount != 0 && quarter4BlackPixelCount != 0)
            {
                quarter1 = (float)quarter1BlackPixelCount / (float)allBlackPixelCount;
                quarter2 = (float)quarter2BlackPixelCount / (float)allBlackPixelCount;
                quarter3 = (float)quarter3BlackPixelCount / (float)allBlackPixelCount;
                quarter4 = (float)quarter4BlackPixelCount / (float)allBlackPixelCount;
            }

            //Debug.WriteLine("");
            //Debug.WriteLine("quarter1: " + quarter1.ToString() +
            //                " quarter2: " + quarter2.ToString() +
            //                " quarter3: " + quarter3.ToString() +
            //                " quarter4: " + quarter4.ToString());

            //Debug.WriteLine("");

            // сравнить полученное значение статистики с известными
            return CompareStats(quarter1, quarter2, quarter3, quarter4);
        }