优化qrcode: improve QR code parsing accuracy and speed#22
Draft
优化qrcode: improve QR code parsing accuracy and speed#22
Conversation
Open
Agent-Logs-Url: https://github.com/echosoar/imgpro/sessions/fdba7229-f072-4d47-b5c1-c3dcf9f31e0b Co-authored-by: echosoar <14832743+echosoar@users.noreply.github.com>
Copilot
AI
changed the title
[WIP] Optimize QR code decoding algorithm for accuracy and speed
优化qrcode: improve QR code parsing accuracy and speed
Mar 31, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
QR code parsing had two core problems: a scoring function that gave wrong signal to the perspective matrix optimizer, and an exhaustive matrix search that made ~12,750 expensive evaluations per decode regardless of whether they helped.
Accuracy
Fix
scoreAreascoring logic — the previous implementation rewarded black pixels in white areas and never scored black pixels in black areas, making the score a poor proxy for matrix correctness:Speed
Replace
autoAdjustmentMatrixexhaustive search with coordinate descent — the old algorithm tried all 255 index-combination × 50 scale steps against a frozen baseline matrix (improvements never compounded). Replaced with multi-scale coordinate descent (scales: 0.5 → 0.005) that updates the base on every improvement, converging in ~100 evaluations vs 12,750. The 256-rowautoAdjustmentMatrixChangeIndexstatic table is removed.Convert
fillRegionrecursive flood fill to iterative — eliminates the hard depth cap of 1024 that silently dropped pixels in large regions; only pushes the start of each new run onto the stack rather than every pixel.Pre-allocate
rowAverageinbinarization— single allocation reused across all rows instead of one allocation per row.Benchmark (
BenchmarkTestNX)Safety
Added
len(result) >= 3guard before UTF-8 BOM detection inread8BitByteDatato prevent a potential index-out-of-range panic on short payloads.