https://github.com/alsult/compvision_puzzle
A computer vision pipeline that reconstructs a shuffled puzzle using SIFT feature matching, RANSAC, and image preprocessing.
https://github.com/alsult/compvision_puzzle
computer-vision contour-detection feature-matching homography image-processing image-segmentation opencv peppa-pig puzzle-reconstruction python ransac-algorithm sift-features
Last synced: 12 days ago
JSON representation
A computer vision pipeline that reconstructs a shuffled puzzle using SIFT feature matching, RANSAC, and image preprocessing.
- Host: GitHub
- URL: https://github.com/alsult/compvision_puzzle
- Owner: AlSult
- License: mit
- Created: 2025-03-25T11:02:18.000Z (about 2 months ago)
- Default Branch: main
- Last Pushed: 2025-04-23T08:05:46.000Z (26 days ago)
- Last Synced: 2025-04-23T09:21:44.849Z (26 days ago)
- Topics: computer-vision, contour-detection, feature-matching, homography, image-processing, image-segmentation, opencv, peppa-pig, puzzle-reconstruction, python, ransac-algorithm, sift-features
- Language: Python
- Homepage:
- Size: 5.84 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Peppa Puzzle Reconstructor
This project automatically reconstructs a shuffled image of a Peppa Pig puzzle using computer vision techniques. The algorithm identifies and extracts puzzle pieces, matches them with their correct positions in a reference image using feature matching and RANSAC, and overlays them to restore the original image.
![]()
![]()
![]()
## Preprocessing
Before reconstruction can take place, the shuffled puzzle image undergoes several preprocessing steps to isolate individual puzzle pieces:
1. **Grayscale Conversion**
The input image is converted to grayscale to simplify edge detection and reduce computational load.2. **Edge Detection**
Canny edge detection is applied to highlight the contours of each puzzle piece.3. **Morphological Operations**
Dilation and closing operations are used to strengthen the edges and close small gaps, making contours more distinct.4. **Contour Detection**
External contours are identified to extract bounding boxes around each puzzle piece.5. **Filtering by Area**
Small contours (noise or irrelevant fragments) are filtered out using a minimum area threshold to retain only meaningful puzzle segments.The result of this preprocessing step is a set of cropped images — each representing a potential puzzle piece — which are then passed on for feature matching and reconstruction.
### Visual Output of Preprocessing
![]()
![]()
![]()
## How It Works1. **Piece Extraction**
Detects individual puzzle pieces from a shuffled image using edge detection and contour analysis via OpenCV.
![]()
![]()
![]()
3. **Feature Matching with RANSAC**
Each puzzle piece is matched to a reference image using SIFT keypoints and descriptors. RANSAC is applied to estimate a homography matrix for accurate alignment.
![]()
![]()
4. **Transparent Background**
White areas in the puzzle pieces are made transparent to blend seamlessly during reconstruction.
![]()
5. **Puzzle Reconstruction**
Each aligned piece is overlayed onto a transparent canvas, rebuilding the complete puzzle.
![]()
## Results
The reconstruction works even when the pieces are rotated or slightly misaligned, thanks to the robustness of the SIFT + RANSAC approach. While it performs well on clean images with distinct features, accuracy may decrease with blurry or low-contrast images.## Limitations
While the reconstruction pipeline is generally accurate, there are cases where two connected puzzle pieces are mistakenly treated as a single object during contour detection. This typically happens when:
- The gap between pieces is too small or visually subtle
- Morphological operations (dilation, closing) merge adjacent edges
- The white background isn't fully separated from piece boundariesAs a result, a few puzzle segments may be incorrectly aligned or misplaced in the final reconstruction.
Some extracted regions contained multiple puzzle pieces, as it's shown during the detection phase:
Future improvements could include:
- Adaptive contour separation techniques
- Color-based segmentation to better split adjacent regions
- Manual validation for ambiguous cases## Files Used
- `peppa/peppa.png`: The original complete puzzle image (reference).
- `peppa/pz.png`: The image containing randomly placed/shuffled puzzle pieces.## Requirements
- Python 3
- OpenCV (with `cv2.SIFT_create()` support, e.g., via `opencv-contrib-python`)
- NumPyInstall dependencies:
```bash
pip install opencv-contrib-python numpy