A real-time QR code region detection tool based on OpenCV
. It is designed to solve the problem of QR code recognition where finder patterns are missing due to physical damage or occlusion. Unlike traditional decoders, this project can deduce the complete QR code region even when only two finder patterns are detected, and it outputs a corrected, clear image, providing high-quality input for subsequent decoding.
-
Finder Pattern Detection: Utilizes an optimized contour filtering algorithm to accurately identify QR code finder patterns within an image.
-
Geometric Deduction: When the three finder patterns are incomplete, it can deduce the missing corner points using the existing ones and their geometric relationships.
-
Automatic Perspective Correction: Corrects skewed and distorted QR code regions into a standard square image.
-
Image Enhancement: Applies sharpening and adjusts brightness/contrast to the extracted QR code, improving readability.
-
Real-time Interactive Interface: Captures footage from a camera in real-time and provides easy-to-use keyboard shortcuts for operation.
Recognizing QR codes with missing finder patterns due to physical damage or occlusion.
OpenCV
(cv2
)
NumPy
(numpy
)
Pillow
(PIL
)
You can install all dependencies using pip
:
pip install opencv-python numpy Pillow
python main.py
When the program starts, a camera window will open. The following keyboard shortcuts are available:
Key | Function | Description |
---|---|---|
f |
Freeze Frame | When a QR code is detected, press f to freeze the current frame. |
t |
Rotate Direction | While frozen, press t to rotate the detected QR code image. |
c |
Unfreeze | While frozen, press c to unfreeze and resume real-time detection. |
s |
Save Image | While frozen, press s to save the processed QR code image to your local drive. |
q |
Exit Program | Press q at any time to safely exit the program. |
-
Contour Detection: The input camera frame is binarized, and all contours are found using
cv2.findContours
. -
Finder Pattern Filtering: Using the hierarchical relationships of contours obtained with
cv2.RETR_TREE mode
, concentric squares with a three-layer nested structure are filtered out. These are identified as potential QR code finder patterns. -
Geometric Analysis:
-
Three-Point Case: If three finder patterns are found, the top-left, top-right, and bottom-left corner points are determined based on their relationship as an isosceles right triangle.
-
Two-Point Case: If only two finder patterns are found, they are assumed to be the top-left and top-right points, and the bottom-left and bottom-right corner points are deduced through vector rotation and addition.
-
-
Perspective Transformation: Using these four corner points as source coordinates,
cv2.getPerspectiveTransform
andcv2.warpPerspective
are used to flatten and correct the QR code region. -
Image Optimization: The corrected image is adjusted for brightness, contrast, and sharpened to obtain an optimal black-and-white binary image, which facilitates recognition by subsequent decoding libraries.
To correctly display Chinese text, you need to download a Chinese font file (e.g., NotoSansSC-VariableFont_wght.ttf) and place it in the project's root directory. If the font is not present, the program will automatically fall back to the default font, but Chinese characters may not display correctly.
This can be influenced by various factors, such as the level of sharpening, motion blur, etc. You may need to manually adjust parameters or the algorithm to resolve this.
This project is licensed under the MIT License.