This Python script performs advanced image processing to detect, extract, and rectify documents from an image, transforming them into a clean, scanned-like document.
- Document detection using edge detection
- Contour identification
- Corner detection
- Perspective transformation (document rectification)
- Grayscale and thresholded output generation
- Python 3.x
- OpenCV (
cv2) - NumPy
- Matplotlib
pip install opencv-python numpy matplotlib- Place your input image (
matttest1.jpg) in the same directory as the script - Run the script
- The script will generate several output images:
- Resized grayscale image
- Canny edge detection result
- Hough transform lines
- Detected document corners
- Rectified high-quality image
- Rectified grayscale image
- Rectified 'scanned' image with adaptive thresholding
- Read and resize input image
- Convert to grayscale
- Apply Gaussian blur
- Perform Canny edge detection
- Find document contour
- Detect document corners
- Apply perspective transformation
- Generate various output representations
resized_image.jpg: Resized grayscale version of original image- Multiple visualization plots showing intermediate processing steps
- Final rectified document images
You can modify parameters like:
sigmafor Gaussian blur- Canny edge detection thresholds
- Morphological operation kernel size
- Document digitization
- Paper scanning
- Image preprocessing for OCR
- Perspective correction for document images
- Ensure input image is clear and document edges are visible
- Adjust blur and edge detection parameters if automatic detection fails
- Verify all required libraries are installed
MIT License
Copyright (c) 2024 Nosherwan Babar
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Nosherwan Babar