예제 #1
0
        // TODO: Optional<Mat>
        public HistChessboardModel Do(IVideo video)
        {
            HistChessboardModel model = null;
            Mat frame;
            Mat gray        = new Mat();
            Mat thresh      = new Mat();
            var corners     = new Point2f[4];
            var patternSize = new Size(7, 3);
            var threshWin   = new Window("Adaptive Threshold");

            // TODO: each iteration, try different block sizes for the adaptive threshold (height / 4, height / 2, etc)
            do
            {
                frame = video.GetNextFrame();
                if (frame != null && frame.Width + frame.Height > 0)
                {
                    Cv2.CvtColor(frame, gray, ColorConversionCodes.BGR2GRAY);
                    //Cv2.MedianBlur(gray, gray, 5); // Disallows chessboard to be found, because of how it opens/closes corners
                    Cv2.AdaptiveThreshold(gray, thresh,
                                          maxValue: 255.0,
                                          adaptiveMethod: AdaptiveThresholdTypes.GaussianC,
                                          thresholdType: ThresholdTypes.Binary,
                                          blockSize: (gray.Height / 4) | 1,
                                          c: 0.0);
                    threshWin.ShowImage(thresh);

                    var found = Cv2.FindChessboardCorners(thresh, patternSize, out corners,
                                                          ChessboardFlags.None); //, ChessboardFlags.AdaptiveThresh | ChessboardFlags.NormalizeImage);

                    //frame.CopyTo(output);
                    //Cv2.DrawChessboardCorners(output, patternSize, corners, found);
                    //if (!found) Console.Out.WriteLine("Chessboard not found :( ");

                    if (found)
                    {
                        var       boardPoints = new Point2d[21];
                        Point2d[] foundPoints = OCVUtil.Point2fTo2d(corners);
                        for (int c = 0; c < 7; c++)
                        {
                            for (int r = 0; r < 3; r++)
                            {
                                boardPoints[r * 7 + c] = new Point2d((c + 1.0), (r + 3.0));
                            }
                        }

                        var boardToImageTransform = Cv2.FindHomography(boardPoints, foundPoints);
                        var imageToBoardTransform =
                            boardToImageTransform.Inv(); //Cv2.FindHomography(foundPoints, boardPoints);
                        model = new HistChessboardModel(boardToImageTransform, imageToBoardTransform, frame);
                    }
                }
            } while (frame != null && model == null);

            return(model);
        }
예제 #2
0
        Mat DrawSquaresMask(Mat frame)
        {
            var result = new Mat(frame.Size(), MatType.CV_8U, Scalar.Black);

            for (int col = 0; col < 8; col++)
            {
                for (int row = 0; row < 8; row++)
                {
                    var squarePoints = OCVUtil.ToPoints(GetSquarePoints(row, col, SquareMaskScale));
                    Cv2.FillConvexPoly(result, squarePoints, Scalar.White);
                }
            }
            return(result);
        }
예제 #3
0
        Mat DrawBackgroundModel(Mat frame)
        {
            var result = new Mat(frame.Size(), frame.Type());

            for (int col = 0; col < 8; col++)
            {
                for (int row = 0; row < 8; row++)
                {
                    var color        = IsWhiteSquare(row, col) ? WhiteSquareColor : BlackSquareColor;
                    var squarePoints = OCVUtil.ToPoints(GetSquarePoints(row, col));
                    Cv2.FillConvexPoly(result, squarePoints, color);
                }
            }
            return(result);
        }
예제 #4
0
        public static void DrawSquareMask(int row, int col, Mat dest, Mat boardToImageTransform, double scale = 1.0)
        {
            var points = OCVUtil.ToPoints(GetSquarePoints(row, col, boardToImageTransform, scale));

            Cv2.FillConvexPoly(dest, points, Scalar.White);
        }