/// <summary> /// Extracts the next piece visible on the screenshot. /// </summary> /// <param name="screenshot">The screenshot to extract the piece from.</param> /// <returns>The next Tetrimino.</returns> public ProbabilisticResult <Tetrimino> ExtractNextPieceFuzzy(IScreenshot screenshot) { // relevant tiles on the screen: x : 14 - 17, y : 13 - 16 double bestProbability = double.NegativeInfinity; Tetrimino?bestTetrimino = null; foreach (var tetrimino in Tetriminos.All) { //var piece = new Piece(tetromino, 0, TetrisConstants.NextPieceTemplateTileCoordinates.X, TetrisConstants.NextPieceTemplateTileCoordinates.Y); //var probability = _templateMatcher.GetProbabilityCurrentPiece(screenshot, piece); var probability = _templateMatcher.GetProbabilityNextPiece(screenshot, tetrimino); if (probability > bestProbability) { bestProbability = probability; bestTetrimino = tetrimino; } } if (!bestTetrimino.HasValue) { throw new Exception("No possible value for tetrimino found!"); } return(new ProbabilisticResult <Tetrimino>(bestTetrimino.Value, bestProbability)); }
public void PieceMatcher() { // source image string path = "Screenshots/white.png"; var sourceImage = new Mat(path, LoadImageType.Grayscale); var screenshot = new EmguScreenshot(sourceImage, DateTime.Now.Subtract(DateTime.MinValue)); var pieceMatcher = new TemplateMatcher(); pieceMatcher.GetProbabilityNextPiece(screenshot, Tetrimino.I); }