public void Do(IVideo video)
        {
            Mat frame = video.GetNextFrame();

            while (frame != null)
            {
                Cv2.Subtract(frame, _histChessboardModel.BackgroundModel, difference, _histChessboardModel.SquaresMask);
                diffWin.ShowImage(difference);

                var classif = CalcSquareClassifications(frame);
                squareStatesRenderer.Render(classif);

                frame = video.GetNextFrame();
            }
        }
        public void Do(IVideo video)
        {
            Mat frame = video.GetNextFrame();

            while (frame != null)
            {
                frame.ConvertTo(frame, MatType.CV_16SC3);
                Cv2.Subtract(frame, background, difference, _histChessboardModel.SquaresMask);
                difference = difference.Abs().ToMat();
                ShowDiff();

                bool[,] squareState = EstimateSquareStates(difference);
                stateRenderer.Render(squareState);

                frame = video.GetNextFrame();
            }
        }
示例#3
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);
        }