Skip to content

feat: compute the Hough accumulator by voting - #228

Merged
petercorke merged 4 commits into
mainfrom
feat/hough-accumulator-direct
Nov 22, 2026
Merged

petercorke merged 4 commits into
mainfrom
feat/hough-accumulator-direct

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Hough.accumulator() now computes the accumulator by voting, in NumPy, instead of reconstructing it from repeated calls of cv2.HoughLines.

OpenCV does not expose its accumulator, so the old method ran HoughLines at every threshold from 0 upwards and counted how often each line survived. That gave two problems. It was slow: about 10 seconds on the Canny edges of church.png (125,000 edge pixels). And A held only the peaks, because OpenCV reports only local maxima, so plot_accumulator() showed scattered dots (1,553 non-zero cells on a 100 × 100 image with three drawn lines, where voting gives 12,793) and not the surface of sinusoids described in the book. It is now 79 ms on the same image, and plot_accumulator() shows the full surface.

What changes:

  • A is the full integer vote array, with the same quantization as OpenCV (ntheta angles, and 2(w+h)+1 rows of rho with rho = 0 in the middle row).
  • extent is the edges of that array, so it spans the whole range of rho.
  • votes and t keep their meaning: the number of lines at each threshold, and the highest threshold that still finds one. A line is a cell above the threshold that is a local maximum, which is OpenCV's rule, so votes[t] equals len(lines(minvotes=t)) exactly on clean test lines, and to within 0.1% on the church image (33,518 against 33,554 at threshold 10), probably from OpenCV's single-precision arithmetic. The docstring warning says so.
  • The skip argument is removed, since every threshold is now counted. Nothing in the repository passes it, and the book notebook tests/RVC/chap12.ipynb calls plot_accumulator() and plots h.votes without it, so it needs no change. The docstring has a .. versionchanged:: 2.5.0 note.

lines(), lines_p(), plot_lines() and the constructor are not changed. The method no longer calls cv2.HoughLines, so it also no longer depends on whether OpenCV returns None or an empty array (see PR 224).

Testing

tests/test_image_line_features.py gets nine tests of the accumulator on drawn lines: A equals a pixel-by-pixel vote written out in the test, each set pixel votes once per angle, the peaks are at the known (theta, rho) with the known counts, votes[t] equals the number of lines from cv2.HoughLines at each threshold, votes never rises, an empty image gives an empty A and t == -1, the quantization options and extent, and skip raising TypeError. The accumulator tests that PR 220 added to tests/test_image_line_features_hough.py still hold, except the one for skip, which is removed. The title test in tests/test_plot_titles.py now computes the real accumulator instead of supplying one, since it is no longer slow. pytest tests: 1859 passed, 33 skipped, 13 xfailed. mypy reports no issues. The Hough tests also pass against real OpenCV 5.1, in a throwaway virtual environment. Not run: CI on Windows or OpenCV 4 (CI will).

This change was written with AI assistance (Claude Code) and the diff has been read by the maintainer.

🤖 Generated with Claude Code

petercorke and others added 3 commits November 22, 2026 20:47
Hough.accumulator() reconstructed the accumulator from repeated calls of
cv2.HoughLines, which was slow (10 s on a 125,000 pixel edge image) and gave
only the peaks. It now votes directly, so A is the full array, in 80 ms on the
same image. votes counts the cells that are local maxima above each threshold,
the rule OpenCV uses, so it matches lines(minvotes) to within 0.1%. The skip
argument is removed since every threshold is now counted.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
Drop the test of the removed skip argument, update two comments, and let the
plot title test compute the accumulator, which is no longer slow.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 15 complexity · 0 duplication

Metric Results
Complexity 15
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 90e21b4 into main Nov 22, 2026
34 checks passed
@petercorke
petercorke deleted the feat/hough-accumulator-direct branch November 22, 2026 12:43
@github-actions github-actions Bot mentioned this pull request Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant