Project of Model Driven Machine Vision (Course 206.2) course, lectured by Professor Louis Lettry.
This project aims to develop a robust solution for outputting a complete image file from a photograph containing dispersed puzzle pieces. From a broader perspective, such a task could arise in real-life situations when aiming to develop a solution for reassembling broken 2D artifacts by matching individual pieces together.
To approach this problem, we consider several key questions:
- Image Acquisition: How should we capture images of the pieces to ensure consistency and accuracy?
- Piece Definition: What constitutes a piece in our system? How do we define a complete puzzle?
- Differentiation: What characteristics distinguish one piece from another?
- Assembly Logic: What algorithms and methods can we use to reassemble pieces into a correct puzzle?
- Project Boundaries: What constraints exist regarding number of pieces, piece shapes, colors, etc.?
- Solution Scope: What are the limitations and capabilities of our proposed solution?
- Constraints: What implicit/explicit constraints must we consider?
When capturing images of puzzle pieces, we should:
- Minimize variability in photovolumetric parameters
- Use a unified background color that contrasts with the puzzle pieces (especially important for monochrome puzzles): our current choice is a black piece of fabric
- Ensure all pieces are visible and well-separated in the input image
The device chosen for taking the images is a Samsung Galaxy S24 Ultra.
Key definitions:
- Piece: A 2D form bounded by a defined perimeter
- Contour: A continuous and finite line forming the perimeter of a piece
- Border: An element formed from an n-cutting from the perimeter of a piece
- straight border: A border where the shape is a straight line
- tab border: A border where there is a bump coming out of a straight border
- pocket border: A border where there is a cavity into a straight border
- Piece Differentiation: Pieces can be distinguished by analyzing their boundaries and colors
- Puzzle: A finite ordered group of pieces whose boundaries match perfectly with each other without gaps
- Classic Puzzle: A puzzle with pieces of varied forms, bounded by 4 segments forming a rectangle, with the assembled pieces depicting a colored image
- Monochrome Puzzle: A puzzle with a unicolor motif created from the assembled pieces
- Form Puzzle: A puzzle whose pieces all have the same shape
Our enhanced solution implements a robust, adaptive detection system:
-
Multi-Channel Preprocessing
1.1. Analyze image characteristics to determine optimal processing
1.2. Process multiple color channels (RGB, HSV, LAB) in parallel
1.3. Select the best channel representation for piece detection
1.4. Apply adaptive contrast enhancement based on image properties -
Adaptive Parameter Optimization
2.1. Dynamically test multiple parameter combinations
2.2. Optimize settings based on image characteristics
2.3. Use statistical clustering to determine optimal thresholds -
Enhanced Contour Detection
3.1. Use multiple methods for finding contours
3.2. Apply statistical filtering with robust metrics
3.3. Implement two-stage filtering to recover valid pieces
3.4. Validate contours using shape characteristics -
Multi-Pass Detection
4.1. Run multiple detection passes with different settings
4.2. Combine results from different passes
4.3. Remove duplicate detections between passes -
Advanced Corner and Border Analysis
5.1. Detect corners using adaptive algorithms
5.2. Classify borders as straight, tab, or pocket
5.3. Calculate validation scores for piece quality assessment -
Image Reconstruction
6.1. Analyze potential matches between pieces
6.2. Realign pieces based on orientation
6.3. Generate final complete image
- OpenCV for image processing and computer vision tasks
- Python 3.13.2 as the primary programming language
- NumPy and SciPy for numerical computations and statistical analysis
- Multiprocessing for parallel execution of detection tasks
- Additional libraries as needed for specific algorithms (under the approval of Prof. Lettry)
- Accuracy of piece matching
- Completeness of puzzle reconstruction
- Quality of the final generated image
- Processing time
- Robustness across different puzzle types and piece configurations
- Ability to handle puzzles with varying numbers of pieces
Here is how to use the enhanced program:
pip install -r requirements.txt
# Basic usage
python main.py --image picture\puzzle_24-1\b-2.jpg
# Actual usage
python main.py --image picture\puzzle_24-1\b-2.jpg --pieces 24 --use-multiprocessing --processes 14 --use-cache --clear-cache --cache-dir cache --adaptive-preprocessing --comprehensive-verification --debug --save-results --profile --analyze-image --area-verification --max-cache-size 4000
Required parameters:
--image Path to the puzzle image (required)
Optional parameters:
--pieces Expected number of pieces in the puzzle
--debug-dir Directory to save debug outputs (default: "debug")
--extract Extract individual pieces to separate files
--extract-dir Directory to save extracted pieces (default: "extracted_pieces")
--use-multiprocessing Use multiprocessing for faster detection
--view View results in image windows
--auto-threshold Apply both Otsu and adaptive thresholding
--no-mean-filter Disable filtering by mean area
--mean-threshold Standard deviation threshold for mean filtering (default: 1.5)
Enhanced detection options:
--adaptive-preprocessing Use adaptive multi-channel preprocessing
--optimize-parameters Test multiple parameter combinations and select the best
--multi-pass Run multiple detection passes with different settings
--analyze-image Analyze image characteristics and optimize settings accordingly
The system analyzes the image characteristics and selects the optimal preprocessing approach from multiple color spaces and channels (RGB, HSV, LAB). This enables robust detection across varying lighting conditions and piece colors.
Instead of fixed parameters, the system can automatically test multiple parameter combinations to find the optimal settings for a specific image. This adapts to different puzzle types and image conditions.
By running multiple detection passes with different settings and combining the results, the system can detect pieces that might be missed by a single approach. This significantly improves detection rates.
The system uses robust statistical measures (median absolute deviation instead of standard deviation) for more accurate filtering of contours, reducing false negatives and false positives.
Each detected piece is assigned a validation score based on multiple criteria, allowing for quality assessment and potential filtering of invalid detections.
The final verification step provides an additional layer of filtering to ensure that only legitimate puzzle pieces are included in the results. This feature is particularly useful for challenging images where initial detection may include false positives (shadows, reflections, or other objects misidentified as puzzle pieces).
-
Area-based Verification
- Filters out pieces that deviate significantly from the mean area
- Configurable threshold to control strictness (in standard deviations)
- Especially useful for puzzles where all pieces should have similar sizes
-
Comprehensive Verification
- Combines multiple criteria: area, aspect ratio, and validation score
- Adaptive thresholds based on expected piece count
- Recovery mechanism if too many pieces are filtered out
-
Visualization
- Clear display of verified vs rejected pieces
- Detailed rejection reasons shown on the image
- Enhanced metrics visualization showing verification results
The following command line arguments have been added:
--area-verification Enable final area verification
--area-threshold FLOAT Standard deviation threshold (default: 2.0)
--comprehensive-verification Enable comprehensive verification
Basic area verification:
python main.py --image puzzle.jpg --pieces 24 --area-verification
Stricter area filtering:
python main.py --image puzzle.jpg --pieces 24 --area-verification --area-threshold 1.5
Comprehensive verification:
python main.py --image puzzle.jpg --pieces 24 --comprehensive-verification
When using the PuzzleProcessor
class directly:
# Basic area verification
results = processor.process_image(
"puzzle.jpg",
expected_pieces=24,
use_area_verification=True,
area_verification_threshold=2.0
)
# Comprehensive verification
results = processor.process_image(
"puzzle.jpg",
expected_pieces=24,
use_comprehensive_verification=True
)
The area verification calculates the mean and standard deviation of all detected piece areas, then rejects pieces whose areas fall outside the range:
[mean - threshold × std_dev, mean + threshold × std_dev]
Where:
mean
is the average area of all detected piecesstd_dev
is the standard deviation of piece areasthreshold
is the configurable parameter (default: 2.0)
The comprehensive verification applies multiple filters in sequence:
- Area filtering: Same as area verification
- Aspect ratio filtering: Rejects pieces with extreme aspect ratios
- Validation score filtering: Rejects pieces with low validation scores
If too many pieces are rejected relative to the expected count, the verification automatically relaxes its criteria to recover more pieces.
The verification process generates additional visualizations:
- Verification visualization: Shows verified pieces in green and rejected pieces in red
- Enhanced metrics: Shows statistics before and after verification
- Piece count comparison: Bar chart comparing original, verified, and expected counts
The verification is implemented in the following files:
src/utils/verification.py
: Core verification functionssrc/core/processor.py
: Integration into the processing pipelinemain.py
: Command line interface options
The verification step adds minimal processing overhead while significantly improving detection accuracy. The typical processing time increase is less than 5%.
The system generates comprehensive debug output in the debug directory:
- Visualizations of each processing stage
- Detected piece visualizations with border type classification
- Detailed metrics and analysis
- Parameter optimization results
Jeremy Duc & Alexandre Venturi
Project description, the provided PDF document in the course.