QR Code Mask Penalties
QR codes use mask patterns to optimize readability and error correction. Each mask pattern is evaluated using a set of rules to determine its suitability. This document explains the evaluation process, including the rules and how penalties are calculated. It also provides an example of how a QR code mask is scored.
Mask Evaluation Overview
The evaluation process assigns a penalty score to a QR code based on its structure and content. The goal is to minimize this score, as lower penalties indicate better readability and robustness. The evaluation is based on four rules:
Rule 1: Consecutive Same-Color Modules
Penalizes long sequences of the same color in rows or columns.
Rule 2: 2x2 Blocks of Same-Color Modules
Penalizes 2x2 blocks of the same color.
Rule 3: Specific Patterns
Penalizes the presence of patterns like 1011101
in rows or columns.
Rule 4: Balance of Black and White Modules
Penalizes QR codes with an unbalanced ratio of black to white modules.
The total penalty is the sum of penalties from all four rules.
Evaluation Rules in Detail
Rule 1: Consecutive Same-Color Modules
Objective: Avoid long sequences of the same color in rows or columns.
Penalty Calculation:
- For every sequence of 5 or more consecutive modules of the same color, add a penalty of 3 + (length - 5).
Example: A sequence of 7 black modules incurs a penalty of 3 + (7 - 5) = 5
.
Rule 2: 2x2 Blocks of Same-Color Modules
Objective: Avoid 2x2 blocks of the same color.
Penalty Calculation:
- For every 2x2 block of the same color, add a penalty of 3.
Rule 3: Specific Patterns
Objective: Avoid specific patterns (1011101
or its reverse) in rows or columns.
Penalty Calculation:
- For every occurrence of the pattern 1011101, add a penalty of 40.
Rule 4: Balance of Black and White Modules
Objective: Ensure a balanced ratio of black to white modules.
Penalty Calculation:
- Calculate the percentage of black modules.
- Find the nearest multiple of 5 to this percentage.
- Compute the absolute difference between this multiple and 50.
- Multiply the difference by 10 to get the penalty.