DIP+CV · Programming Exercise

EP06_08 — ⚫ QRCode Segmentation and Real Decoding with OpenCV

6.14.8 EP06_08 ⚫ QRCode Segmentation and Real Decoding with OpenCV

In the previous exercises, the intermediate quantities of the image processing pipeline—such as areas, perimeters, variances, and displacements—were provided directly or calculated from numerical matrices, without the need for specialized Computer Vision libraries. In this chapter-closing exercise, this restriction is intentionally removed: the OpenCV library (cv2) will be used to locate and decode a real QRCode present in a scene.

The proposal reproduces a simplified workflow of systems employed in visual inspection, industrial automation, and automatic document reading. To keep the data input accessible to the educational context, image loading will be integrated into the didactic library morph, through the mm.readImg function.

The scene is provided in ASCII PGM (P2) format and contains a single valid QRCode, in addition to several distractor objects, such as rectangles, textured noise regions, and isolated blocks. Segmentation based solely on geometric properties—such as area and approximately square shape—is necessary to reduce the search space, but it is not sufficient to identify the correct code. The final confirmation will be performed exclusively by attempting to decode using cv2.QRCodeDetector, a procedure compatible with real automatic recognition applications.

6.14.8.1 📋 Implementation Guidelines

  1. Reading dimensions and parameters

    Read, in this order, from standard input:

    • one line containing the number of rows \(L\);
    • one line containing the number of columns \(C\);
    • one line containing the four algorithm parameters separated by spaces:
      • binarization threshold \(T\) (integer);
      • minimum area \(A_{\text{min}}\) (integer);
      • aspect tolerance \(\text{tol}\) (real);
      • margin \(M\) (integer, in pixels).
  2. Image loading

    Use the didactic function f = mm.readImg(L, C) to read the \(L \times C\) values of the grayscale image, obtaining a NumPy array of type uint8.

  3. Binarization

    Apply inverted binary thresholding using the threshold \(T\). Every pixel of the original image with intensity strictly greater than \(T\) must be converted to 255, while the remaining ones must assume the value 0.

  4. Contour detection

    Extract the external connected components using cv2.findContours(...) with the parameters:

    • cv2.RETR_EXTERNAL;
    • cv2.CHAIN_APPROX_SIMPLE.
  5. Geometric filtering

    For each contour found:

    • compute the bounding rectangle (x, y, w, h) using cv2.boundingRect;

    • keep only candidates that simultaneously satisfy:

      Minimum area

      \[ w \times h > A_{\text{min}} \]

      Aspect ratio

      \[ \left|\frac{w}{h}-1\right| \le \text{tol} \]

  6. Candidate ordering

    Sort the candidates by the bounding rectangle area

    \[ w \times h \]

    in descending order.

    In case of a tie, preserve the order originally returned by cv2.findContours.

  7. Verification by decoding

    For each candidate, following the established order:

    • expand the rectangle by \(M\) pixels in all four directions;
    • clamp the indices to remain within the image;
    • extract the crop directly from the original image f;
    • apply cv2.QRCodeDetector().detectAndDecode(...) to that crop.
  8. Stopping criterion

    Immediately stop processing when the first candidate yields a non-empty decoded string.

  9. Case not found

    If no candidate is successfully decoded, print exactly: QRCODE_NAO_ENCONTRADO

  10. Output (found case)

    Print two lines.

    First line: linha coluna altura largura using the original bounding rectangle, before the expansion by the margin \(M\).

    Second line: texto_decodificado

6.14.8.2 📌 Computational Restrictions

  • Use OpenCV functions to perform binarization, contour detection, bounding rectangle computation, and QRCode decoding.
  • Geometric filtering must necessarily occur before the decoding step.
  • Use exclusively the fixed threshold \(T\) provided in the input. It is not allowed to use automatic thresholding methods, such as Otsu or adaptive thresholding.
  • Ensure that the crops sent to the decoder remain within the image boundaries.

6.14.8.3 🧠 Theoretical Foundation

Step Role in the pipeline Consequence if omitted
Geometric filtering Reduces the search space by selecting only regions compatible with the expected QRCode geometry. The decoder would process all contours, including noise and distractor objects.
Decoding Semantically confirms whether the candidate contains a valid QRCode. Geometrically similar objects could be incorrectly classified as a QRCode.
Margin \(M\) Preserves the quiet zone around the code, facilitating its detection. The absence of this margin may prevent proper alignment and correct reading of the code.

This exercise integrates concepts studied throughout the chapter into a single Computer Vision pipeline. Segmentation reduces the set of candidate regions through geometric characteristics, while the decoding step validates the content of the region using a specialized recognition algorithm.

6.14.8.4 📦 Input and Output Specification (VPL)

Input Structure

L
C
T A_min tol M
[image matrix]

Output Structure (Success)

linha coluna altura largura
texto_decodificado

Output Structure (Failure)

QRCODE_NAO_ENCONTRADO

6.14.8.5 📌 Reference Files (.pgm)

For validation purposes, local debugging, and analysis of real pixel matrices, the image files generated in ASCII P2 format are available in the project directory. You may use them to test the adherence of your code by decoding them with your cell phone (save the *.pgm files locally to view them):

  • 📥 Case 1: Normal Pattern – Contains a single perfectly centered code with simple geometric distractors on the periphery.
  • 📥 Case 2: Complex Scenario – Presents a higher density of textured noise and multiple candidate distractors that test the limits of aspect filtering.
  • 📥 Case 3: Expanded Message – Contains a QRCode structured from a longer character string, generating a higher density of internal modules.
  • 📥 Case 4: Compact Geometry – Evaluates the pipeline behavior under optimized contrast conditions and borderline positioning.
  • 📥 Case 5: Exclusion Scenario – An image composed purely of high-area distractor elements, designed to validate the controlled failure behavior of the program.
📋 Simulator EP06_08: QR Code Segmentation and Decoding Geometric Filter→Semantic Stopping

Interactively adjust the algorithm's input parameters (A_min and tol) to check which components are geometrically filtered and how the semantic analysis stopping criterion interrupts the queue scan.

250
0.22
Scene Visualization (Matrix f)
Connected Components in Queue
Figure 6.28: Simulador EP06_08: Geometric Segmentation + Verification by QRCode Decoding
%%writefile EP06_08.py
# Python Code
Overwriting EP06_08.py
TestSuite("EP06_08.py").run()
✔️ EP06_08.cases already exists in casos/
📋 5 case(s) loaded from casos/EP06_08.cases

🔍 Testing Python: EP06_08.py
⚠️ EP06_08.py: Empty file (fewer than 3 lines). Tests skipped.