Exemplo n.º 1
0
        private Image <Bgr, Byte> DrawCalib(Image <Bgr, Byte> img) //Kalibrálás alatt kirajzolt kép.
        {
            Image <Bgr, Byte> result        = img.Copy();
            CrossCageFinder   fieldFinder   = new CrossCageFinder();
            DetectedField     detectedField = fieldFinder.GeneralField(result);

            if (detectedField != null)
            {
                result = CrossCageDrawer.DrawCage(result,
                                                  detectedField.frameBox,
                                                  detectedField.avgBoxSize,
                                                  new Bgr(0, 0, 255));
            }

            return(result);
        }
Exemplo n.º 2
0
        private void setLocation(DetectedField field) //Mezőhatár beállítása kilógó bábúk ellenőrzéséhez.
        {
            int    row    = field.colCount;
            int    col    = field.rowCount;
            PointF center = field.frameBox.center;
            float  size   = (float)(field.avgBoxSize * 1.05);

            boxBounds = new Rectangle[col, row];
            int x0 = (int)(center.X - (col * size / 2));
            int y0 = (int)(center.Y - (row * size / 2));

            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    boxBounds[x, y] = new Rectangle(x0 + (int)(x * size),
                                                    y0 + (int)(y * size),
                                                    (int)size, (int)size);
                }
            }
        }
Exemplo n.º 3
0
        private void setLocation(DetectedField field)
        {   //beállítom, hogy egy adott cella mettől meddig tart
            int    row    = field.colCount;
            int    col    = field.rowCount;
            PointF center = field.frameBox.center;
            float  size   = (float)(field.avgBoxSize * 1.05);

            boxBounds = new Rectangle[col, row];
            int x0 = (int)(center.X - (col * size / 2));
            int y0 = (int)(center.Y - (row * size / 2));

            for (int x = 0; x < col; x++)
            {
                for (int y = 0; y < row; y++)
                {
                    boxBounds[x, y] = new Rectangle(x0 + (int)(x * size),
                                                    y0 + (int)(y * size),
                                                    (int)size, (int)size);
                }
            }
        }
Exemplo n.º 4
0
        /// <summary>
        /// Összehasonlít két DetectedField objektumot.
        /// </summary>
        /// <param name="obj">A hasonlítandó objektum.</param>
        /// <returns>A hasonlítás eredménye.</returns>
        public bool isTheSame(Object obj)
        {
            DetectedField field = obj as DetectedField;

            if (field == null)
            {
                return(false);
            }
            else
            if (isTheSamePoint(new Point((int)field.frameBox.center.X, (int)field.frameBox.center.Y),
                               new Point((int)field.frameBox.center.X, (int)field.frameBox.center.Y), 10) &&
                field.rowCount == this.rowCount &&
                field.colCount == this.colCount)
            //ha a center pozíciója, sor és oszlopszám is egyezik, akkor egyezőnek tekintem a két objektumot
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
Exemplo n.º 5
0
        private void setBoard(DetectedField detectedField, List <int[]> puppetList) //Tábla tartalmának beállítása a nem megfelelő bábú elhelyezkedések figyelésére.
        {
            int[,] puppetMatrix = new int[detectedField.rowCount, detectedField.colCount];
            setLocation(detectedField);

            //Miután megvan hogy mely mezők hol vannak, végignézem az xoListet.
            int xe = detectedField.rowCount;
            int ye = detectedField.colCount;

            //Kezdetben -1 értéket vesz fel az összes mező (ott nincs bábu).
            for (int x = 0; x < xe; x++)
            {
                for (int y = 0; y < ye; y++)
                {
                    puppetMatrix[x, y] = -1;
                }
            }

            //Mátrix adott pozíciójára a megfelelő bábú érték beállítása.
            for (int col = 0; col < xe; col++)
            {
                for (int row = 0; row < ye; row++)
                {
                    foreach (int[] xo in puppetList)
                    //foreach (DetectedPuppet xo in puppetList)
                    {
                        byte errorBit = 0;
                        //if (isRectInRect(xo.puppetRect, boxBounds[col, row], out errorBit))
                        if (isRectInRect(new Rectangle(xo[0], xo[1], xo[2], xo[3]), boxBounds[col, row], out errorBit))
                        {
                            //if (xo.puppetValue.Equals("O"))
                            if (xo[4] == 0)
                            {
                                if (puppetMatrix[col, row] > -1)
                                {
                                    puppetMatrix[col, row] = 3; //Több bábú egy mezőben.
                                    board.setPieces(col, row, Piece._MoreThan1);
                                }
                                else
                                {
                                    puppetMatrix[col, row] = 0; //O
                                    board.setPieces(col, row, Piece.O);
                                }
                            }
                            else
                            {
                                if (puppetMatrix[col, row] > -1)
                                {
                                    puppetMatrix[col, row] = 3; //Több bábú egy mezőben.
                                    board.setPieces(col, row, Piece._MoreThan1);
                                }
                                else
                                {
                                    puppetMatrix[col, row] = 1; //X
                                    board.setPieces(col, row, Piece.X);
                                }
                            }

                            if (errorBit == 1 && puppetMatrix[col, row] < 3)
                            {
                                puppetMatrix[col, row] = 2; //Bábú kilógás.
                                board.setPieces(col, row, Piece._OutOfField);
                            }
                        }
                    }
                }
            }

            //Üres mező értékek felvitele.
            for (int i = 0; i < detectedField.rowCount; i++)
            {
                for (int j = 0; j < detectedField.colCount; j++)
                {
                    if (puppetMatrix[i, j] == -1)
                    {
                        board.setPieces(i, j, Piece._Empty);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void ProcessFrame(object sender, EventArgs arg) //Képenkénti kamera műveletek.
        {
            OriginalImage = capture.RetrieveBgrFrame();         //Kamera kép lekérdezése.
            DetectedImage = OriginalImage.Copy();

            if (!calOk)      //Kalibráció
            {
                if (newGame) //Ha új játék kezdődik.
                {
                    status = GameStatus.SearchBoard;
                    OnGameStatusChanged(status); //Új pálya keresése.
                    newGame = false;
                }

                List <MCvBox2D> boxlist = new List <MCvBox2D>();                                //Felismert négyszögek tárolása.

                GetRectangles(OriginalImage, out boxlist);                                      //Felismert négyszögek lekérdezése.

                if (boxlist.Count > 0)                                                          //Ha találtunk téglalapot.
                {
                    GetGridEdge(boxlist, out maxTop, out maxBottom, out maxRight, out maxLeft); //Megkeresi a felismert négyszöget közül a legszélsők koordinátáit.
                    GetGridCellsNum(boxlist, out row, out column);                              //A felismert négyszögek alapján meghatározza, hogy hányszor hányas lehet a játékmező.
                    calib.AddValue(row, column, maxLeft, maxBottom);                            //Felismert pálya adatait beadja a kalibrátornak.

                    board = new GameboardDrawer(row, column, 400, 400, 5, 300);                 //Létrehozza a táblát.

                    if (calib.Calibrated() && row == column && (row == 4 || row == 3))          //Ha a pálya be van kalibrálva és megfelelő a sor és oszlop szám.
                    {
                        calOk = true;
                        if (row == 3)
                        {
                            status = GameStatus.BoardDetected_3x3;
                        }
                        else
                        {
                            status = GameStatus.BoardDetected_4x4;
                        }
                        OnGameStatusChanged(status); //Pálya megtalálva esemény küldése.
                    }
                    else
                    {
                        DetectedImage = DrawCalib(DetectedImage); //Kirajzolunk egy derékszöget, és a pillanatnyi helyzetét a pályának.
                    }
                }

                Point corner = calib.getCorner(); //Segédvonal koordinátái.

                //kirajzoljuk a pálya kalibrálását segítő vonalakat
                DetectedImage.Draw(new LineSegment2D(new Point(corner.X - 20, corner.Y),
                                                     new Point(corner.X + 100, corner.Y)),
                                   new Bgr(0, 127, 0), 4);

                DetectedImage.Draw(new LineSegment2D(new Point(corner.X, corner.Y + 20),
                                                     new Point(corner.X, corner.Y - 100)),
                                   new Bgr(0, 127, 0), 4);
            }
            else //Be van kalibrálva.
            {
                List <int[]> foundXO = new List <int[]>(); //Megtalált bábuk tárolása.
                DetectedImage = ocr.ProcessImage(OriginalImage, out foundXO); //Megkeresi a bábukat (karaktereket) és bekeretezi a képen.
                prevBoard     = board.Copy();                                 //Pálya előző állapotának elmentése.

                int width      = maxRight - maxLeft;
                int cellWidth  = width / row;
                int height     = maxBottom - maxTop;
                int cellHeight = height / column;

                List <int[]> detectedPupettList = new List <int[]>();
                if (cellWidth > 0 && cellHeight > 0)
                {
                    nextPiece = false;
                    for (int i = 0; i < foundXO.Count; i++)
                    {
                        //Bábu amit felveszünk, ott van e.
                        if (isSimilar(foundXO[i][0], nextPieceCalibX, nextPieceCalibSens) && isSimilar(foundXO[i][1], nextPieceCalibY, nextPieceCalibSens))
                        {
                            nextPiece = true;
                            if (foundXO[i][2] == 1)
                            {
                                nextPieceType = Piece.X;
                            }
                            else
                            {
                                nextPieceType = Piece.O;
                            }
                        }

                        //Megtalált bábuk objektummá alakítása.
                        int xoCenterX = foundXO[i][0];
                        int xoCenterY = foundXO[i][1];
                        int xoWidth   = foundXO[i][3];
                        int xoHeight  = foundXO[i][4];
                        int xoLeft    = xoCenterX - ((int)(xoWidth / 2));
                        int xoTop     = xoCenterY - ((int)(xoHeight / 2));

                        if (foundXO[i][2] == 1)
                        {
                            detectedPupettList.Add(new int[5] {
                                xoLeft, xoTop, xoWidth, xoHeight, 1
                            });
                        }
                        else if (foundXO[i][2] == 0)
                        {
                            detectedPupettList.Add(new int[5] {
                                xoLeft, xoTop, xoWidth, xoHeight, 0
                            });
                        }
                    }

                    //Meg kell találni a pályát megint.
                    int           dfWidth       = maxRight - maxLeft;
                    int           dfHeight      = maxBottom - maxTop;
                    DetectedField detectedField = new DetectedField(new MCvBox2D(new PointF(maxLeft + (dfWidth / 2), maxTop + (dfHeight / 2)),
                                                                                 new SizeF(dfWidth, dfHeight), 0),
                                                                    (maxRight - maxLeft) / column);
                    if (detectedField.colCount > 0 && detectedField.rowCount > 0)
                    {
                        setBoard(detectedField, detectedPupettList);
                    }
                }

                DetectedImage = DrawGrid(DetectedImage, new Point(maxLeft, maxTop), new Point(maxRight, maxTop), new Point(maxLeft, maxBottom), new Point(maxRight, maxBottom), row, column); //Az előző adatok alapján rárajzolja a képre a játékmezőt.

                if (!GameboardDrawer.Compare(prevBoard, board))                                                                                                                               //Összehasonlítja az előző és az aktuális pályát.
                {
                    OnTableStateChanged(board.GetTable());                                                                                                                                    //Bábu kerül fel vagy le.
                    try
                    {
                        ibDraw.Image = board.getGameboard().Bitmap;//.Rotate(180, new Bgr(0, 0, 0));
                    }
                    catch
                    {
                        Console.WriteLine("PictureBox error!!!!!!!!!!!!!!");
                    }
                }

                if (nextPiece != prevPiece) //Csak változás esetén legyen esemény.
                {
                    if (nextPiece)
                    {
                        OnNextPieceChanged(nextPieceType);
                    }
                    else
                    {
                        OnNextPieceChanged(Piece._NextPieceMissing);
                    }
                    prevPiece = nextPiece;
                }
            }

            //Képek visszaadása.
            try
            {
                ibOriginal.Image = OriginalImage.Bitmap; //.Rotate(180, new Bgr(0, 0, 0));
                ibDetected.Image = DetectedImage.Bitmap; //.Rotate(180, new Bgr(0, 0, 0));
            }
            catch
            {
                Console.WriteLine("PictureBox error!!!!!!!!!!!!!!");
            }
        }
Exemplo n.º 7
0
        private void setBoard(DetectedField detectedField, List <DetectedPuppet> puppetList)
        {
            int[,] puppetMatrix = new int[detectedField.rowCount, detectedField.colCount];
            setLocation(detectedField);

            //miután megvan hogy mely mezők hol vannak, végignézem az xoListet
            int xe = detectedField.rowCount;
            int ye = detectedField.colCount;

            //kezdetben -1 értéket vesz fel az összes mező (ott nincs bábu)
            for (int x = 0; x < xe; x++)
            {
                for (int y = 0; y < ye; y++)
                {
                    puppetMatrix[x, y] = -1;
                }
            }

            for (int col = 0; col < xe; col++)
            {
                for (int row = 0; row < ye; row++)
                {
                    foreach (DetectedPuppet xo in puppetList)
                    {
                        byte errorBit = 0;
                        if (isRectInRect(xo.puppetRect, boxBounds[col, row], out errorBit))
                        {
                            if (xo.puppetValue.Equals("O"))
                            {
                                if (puppetMatrix[col, row] > -1)
                                {
                                    puppetMatrix[col, row] = 3; //two puppets in one box
                                    board.setPieces(col, row, Piece._MoreThan1);
                                }
                                else
                                {
                                    puppetMatrix[col, row] = 0; //O
                                    board.setPieces(col, row, Piece.O);
                                }
                            }
                            else
                            {
                                if (puppetMatrix[col, row] > -1)
                                {
                                    puppetMatrix[col, row] = 3; //two puppets in one box
                                    board.setPieces(col, row, Piece._MoreThan1);
                                }
                                else
                                {
                                    puppetMatrix[col, row] = 1; //X
                                    board.setPieces(col, row, Piece.X);
                                }
                            }

                            if (errorBit == 1 && puppetMatrix[col, row] < 3)
                            {
                                puppetMatrix[col, row] = 2; //outOfBox
                                board.setPieces(col, row, Piece._OutOfField);
                            }
                        }
                    }
                }
            }
        }
Exemplo n.º 8
0
        private void ProcessFrame2(object sender, EventArgs arg)
        {
            /*if (!statusOk && CameraStatusChanged != null)
             * {
             *  OnCameraStatusChanged(status);
             *  statusOk = true;
             * }*/
            OriginalImage = capture.RetrieveBgrFrame();
            DetectedImage = OriginalImage;

            if (!calOk)
            {
                if (newGame)
                {
                    status = CameraStatus.SearchBoard;
                    OnCameraStatusChanged(status); //új pálya keresése
                    newGame = false;
                }

                List <MCvBox2D> boxlist = new List <MCvBox2D>();

                GetRectangles(OriginalImage, out boxlist);

                if (boxlist.Count > 0)
                {
                    GetGridEdge(boxlist, out maxTop, out maxBottom, out maxRight, out maxLeft); //megkeresi a felismert négyszöget közül a legszélők koordinátáit
                    GetGridCellsNum(boxlist, out sor, out oszlop);                              //a felismert négyszögek alapján meghatározza, hogy hányszor hányas lehet a játékmező

                    calib.AddValue(sor, oszlop, maxTop, maxRight);


                    board = new GameboardDrawer(sor, oszlop, 400, 400, 2);

                    int width      = maxRight - maxLeft;
                    int cellWidth  = width / sor;
                    int height     = maxBottom - maxTop;
                    int cellHeight = height / oszlop;

                    if (calib.Calibrated() && sor == oszlop && (sor == 4 || sor == 3))
                    {
                        calOk = true;
                        if (sor == 3)
                        {
                            status = CameraStatus.BoardDetected_3x3;
                        }
                        else
                        {
                            status = CameraStatus.BoardDetected_4x4;
                        }
                        OnCameraStatusChanged(status); //pálya megtalálva
                    }
                    else
                    {
                        //kijzolunk egy derékszöget, és a pillanatnyi helyzetét a pályának
                        DetectedImage = DrawCalib(DetectedImage);
                    }
                }

                Point sarok = calib.getSarok();
                DetectedImage.Draw(new LineSegment2D(new Point(sarok.X - 20, sarok.Y),
                                                     new Point(sarok.X + 100, sarok.Y)),
                                   new Bgr(0, 127, 0), 4);

                DetectedImage.Draw(new LineSegment2D(new Point(sarok.X, sarok.Y + 20),
                                                     new Point(sarok.X, sarok.Y - 100)),
                                   new Bgr(0, 127, 0), 4);
            }
            else
            {
                List <int[]> foundXO = new List <int[]>();
                DetectedImage = processor.ProcessImage(OriginalImage, out foundXO);
                prevBoard     = board.Copy();
                board         = new GameboardDrawer(sor, oszlop, 400, 400, 2);
                int width      = maxRight - maxLeft;
                int cellWidth  = width / sor;
                int height     = maxBottom - maxTop;
                int cellHeight = height / oszlop;

                List <DetectedPuppet> detectedPupettList = new List <DetectedPuppet>();
                if (cellWidth > 0 && cellHeight > 0)
                {
                    nextPiece = false;
                    for (int i = 0; i < foundXO.Count; i++)
                    {
                        //bábu amit felveszünk, ott van e
                        if (isHasonlo(foundXO[i][0], 574, 8) && isHasonlo(foundXO[i][1], 359, 8))
                        {
                            nextPiece = true;
                        }

                        int xoCenterX = foundXO[i][0];
                        int xoCenterY = foundXO[i][1];
                        int xoWidth   = foundXO[i][3];
                        int xoHeight  = foundXO[i][4];
                        int xoLeft    = xoCenterX - ((int)(xoWidth / 2));
                        int xoTop     = xoCenterY - ((int)(xoHeight / 2));

                        if (foundXO[i][2] == 1)
                        {
                            detectedPupettList.Add(new DetectedPuppet(new Rectangle(xoLeft, xoTop, xoWidth, xoHeight), "X", new Point(xoCenterX, xoCenterY)));
                        }
                        else if (foundXO[i][2] == 0)
                        {
                            detectedPupettList.Add(new DetectedPuppet(new Rectangle(xoLeft, xoTop, xoWidth, xoHeight), "O", new Point(xoCenterX, xoCenterY)));
                        }

                        /*
                         * if (foundXO[i][2] == 1)
                         * {
                         *  if ((foundXO[i][0] - maxLeft) >= 0 && (foundXO[i][0] - maxLeft) < width && (foundXO[i][1] - maxTop) >= 0 && (foundXO[i][1] - maxTop) < height)
                         *  {
                         *      board.setX((foundXO[i][0] - maxLeft) / cellWidth, (foundXO[i][1] - maxTop) / cellHeight);
                         *  }
                         * }
                         * else if (foundXO[i][2] == 0)
                         * {
                         *  if ((foundXO[i][0] - maxLeft) >= 0 && (foundXO[i][0] - maxLeft) < width && (foundXO[i][1] - maxTop) >= 0 && (foundXO[i][1] - maxTop) < height)
                         *  {
                         *      board.setO((foundXO[i][0] - maxLeft) / cellWidth, (foundXO[i][1] - maxTop) / cellHeight);
                         *  }
                         * }*/
                    }

                    //meg kell találni a pályát megint
                    int           dfWidth       = maxRight - maxLeft;
                    int           dfHeight      = maxBottom - maxTop;
                    DetectedField detectedField = new DetectedField(new MCvBox2D(new PointF(maxLeft + (dfWidth / 2), maxTop + (dfHeight / 2)),
                                                                                 new SizeF(dfWidth, dfHeight), 0),
                                                                    (maxRight - maxLeft) / oszlop);//crossCageFinder.GeneralField(OriginalImage);
                    if (detectedField.colCount > 0 && detectedField.rowCount > 0)
                    {
                        setBoard(detectedField, detectedPupettList);
                    }

                    //DetectedImage = CrossCageDrawer.DrawCage(DetectedImage, detectedField.frameBox, detectedField.avgBoxSize, new Bgr(0, 255, 255));
                }

                DetectedImage = DrawGrid(DetectedImage, new Point(maxLeft, maxTop), new Point(maxRight, maxTop), new Point(maxLeft, maxBottom), new Point(maxRight, maxBottom), sor, oszlop); //az előző adatok alapján rárajzolja a képre a játékmezőt

                if (!GameboardDrawer.Compare(prevBoard, board))
                {
                    OnTableStateChanged(board.GetTable()); //bábu kerül fel vagy le
                }

                if (nextPiece != prevPiece)
                {
                    if (nextPiece)
                    {
                        OnNextPieceChanged(Piece._NextPieceOk);
                    }
                    else
                    {
                        OnNextPieceChanged(Piece._NextPieceMissing);
                    }
                    prevPiece = nextPiece;
                }
            }

            //otthoni tesztelésnél normál nézet kell, nem kell forgatás
            //ibOriginal.Image = OriginalImage;
            //ibDetected.Image = DetectedImage;

            ibOriginal.Image = OriginalImage.Rotate(180, new Bgr(0, 0, 0));
            ibDetected.Image = DetectedImage.Rotate(180, new Bgr(0, 0, 0));
        }