Repository navigation
feat: compute the Hough accumulator by voting - #228
Merged
Merged
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 15 |
| Duplication | 0 |
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.
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.
Summary
Hough.accumulator()now computes the accumulator by voting, in NumPy, instead of reconstructing it from repeated calls ofcv2.HoughLines.OpenCV does not expose its accumulator, so the old method ran
HoughLinesat 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 ofchurch.png(125,000 edge pixels). AndAheld only the peaks, because OpenCV reports only local maxima, soplot_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, andplot_accumulator()shows the full surface.What changes:
Ais the full integer vote array, with the same quantization as OpenCV (nthetaangles, and2(w+h)+1rows ofrhowithrho = 0in the middle row).extentis the edges of that array, so it spans the whole range ofrho.votesandtkeep 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, sovotes[t]equalslen(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.skipargument is removed, since every threshold is now counted. Nothing in the repository passes it, and the book notebooktests/RVC/chap12.ipynbcallsplot_accumulator()and plotsh.voteswithout it, so it needs no change. The docstring has a.. versionchanged:: 2.5.0note.lines(),lines_p(),plot_lines()and the constructor are not changed. The method no longer callscv2.HoughLines, so it also no longer depends on whether OpenCV returnsNoneor an empty array (see PR 224).Testing
tests/test_image_line_features.pygets nine tests of the accumulator on drawn lines:Aequals 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 fromcv2.HoughLinesat each threshold,votesnever rises, an empty image gives an emptyAandt == -1, the quantization options andextent, andskipraisingTypeError. The accumulator tests that PR 220 added totests/test_image_line_features_hough.pystill hold, except the one forskip, which is removed. The title test intests/test_plot_titles.pynow computes the real accumulator instead of supplying one, since it is no longer slow.pytest tests: 1859 passed, 33 skipped, 13 xfailed.mypyreports 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