This project focuses on automatically computing scores in the Mathable game using computer vision techniques.
Mathable is played on a 14x14 grid, where players place numbered pieces (0-90) to form valid mathematical equations. Equations must consist of three consecutive numbers placed horizontally or vertically. The player who accumulates the highest score wins.
The application is designed to handle three key tasks:
- Detecting the position of newly placed pieces on the board.
- Recognizing the values of the placed pieces.
- Calculating the score for each round based on valid equations.
For this implementation, the game is simplified to two players, with a fixed number of 50 rounds. The approach relies on image processing and OCR techniques to extract board data, combined with rule-based logic for score computation. This Python script is designed to help automate the process of detecting and scoring the Mathable game by analyzing images of the game board. It reads images, detects new pieces placed on the board, recognizes the numbers on those pieces, and calculates the scores based on valid equations formed by the pieces.
#Key Components:
- OpenCV (cv2): Used for handling images, detecting changes, and working with the board grid.
- NumPy (np): Helps manage and manipulate image data.
- Tesseract OCR (pytesseract): Used to recognize digits from the images of the game pieces.
- os: Manages file operations (e.g., reading and writing files).
- collections (defaultdict): Groups images by their game session for easier processing.
-
split_into_cells(image, rows=14, cols=14) This function splits the game board image into 14x14 grid cells and assigns each cell a unique identifier (e.g., A1, B3). It returns a list of cells with their coordinates.
-
detect_difference(image1, image2) Compares two images and finds the difference between them, identifying any changes made on the board, such as a new piece being placed.
-
find_cell(cells, x, y) Given a set of cells and an (x, y) coordinate, this function determines which specific cell the coordinate belongs to on the grid.
-
recognize_digit(cell_image) Converts the image of a cell to grayscale and uses Tesseract OCR to recognize and extract the digit (number) placed on the piece.
-
group_images_by_game(images) Groups images by their game session prefix (the part before the underscore in filenames), organizing them into different game rounds.
-
process_images_by_game(input_folder, output_folder, reference_image_path) This is the main function that processes the images. It reads the images from the input folder, compares them to detect any changes, and extracts the digits from the cells where changes occurred. It saves the results (cell ID and digit) into text files in the output folder.
- The script processes images one by one, comparing the current state of the game board to the previous one to detect new pieces.
- Once a change is detected, it identifies the exact cell where the change occurred and uses OCR to recognize the digit on the piece.
- The results are saved as text files with each file containing the cell ID and the recognized digit for each updated image.
#How It Works:
- Input Folder: Contains the images from different rounds of the game.
- Output Folder: Stores the results for each round, including detected cell IDs and digits.
- Reference Image: A reference image of the empty board is used to detect any changes in the game state (new pieces being placed).
This solution automates the process of tracking the Mathable game, making it easier to detect new pieces, recognize the numbers, and calculate the score without manual intervention.