Skip to content

fix: esttheta() -- broken pickregion() helper and missing theta-sweep body - #82

Merged
petercorke merged 1 commit into
mainfrom
fix/esttheta-plot-poly-leftover
Aug 14, 2026
Merged

petercorke merged 1 commit into
mainfrom
fix/esttheta-plot-poly-leftover

Conversation

@petercorke

Copy link
Copy Markdown
Owner

Summary

Multiple compounding bugs in esttheta()/pickregion(), all part of the same incomplete migration/port, found while root-causing RVC3-python's chap10.ipynb notebook test suite:

  • pickregion() called smb.plot_poly() (removed from spatialmath-python) immediately before an already-migrated replacement two lines later (Polygon2(...).plot(...)) that draws the identical thing -- deleted the redundant broken leftover.
  • The "already-migrated" replacement was itself broken: smb.Polygon2 doesn't exist (Polygon2 lives in top-level spatialmath, not spatialmath.base) -- added the import, matching every other call site in the codebase (ImageConstants.py, Sources.py, ImageBlobs.py, ImageCore.py, tagtool.py all do from spatialmath import Polygon2).
  • polygon.plot("g") passed "g" positionally, binding to Polygon2.plot's ax parameter (not color) -- a leftover calling convention from the old plot_poly(xy, "g", close=True) API. Fixed to polygon.plot(color="g").
  • esttheta()'s body never computed or returned theta at all -- only the interactive region-picking half of the original MATLAB function (toolbox/esttheta.m) was ported; the theta-sweep/variance-minimization loop was entirely missing, so the function always implicitly returned None despite its own docstring promising a float. Implemented the sweep using the already-ported shadow_invariant() (MVTB's existing Python port of MATLAB's invariant()/shadowRemoval()), matching the original algorithm: sweep theta in [0, pi), compute the invariant image at each candidate angle, minimize std() over non-inf/nan pixels, return the best angle. Added a marker at the minimum and a grid to the diagnostic plot.
  • im.column() -> im.view1d(): column() is already deprecated.
  • pickregion() now calls h.figure.ginput(...) on the actual Figure handle returned by im.disp(), instead of the module-level plt.ginput() (which resolves its target through gcf()'s implicit global state). More robust regardless of backend.

Note: ginput()'s click events are not reliably delivered under %matplotlib widget (ipympl) in Jupyter -- this is a known, long-standing upstream ipympl limitation (ipympl#400), not something fixable here. Documented in the RVC3-python notebook as needing %matplotlib tk/qt for that specific cell.

Test plan

  • Existing test suites (tests/base/test_base_color.py, tests/test_image_color.py) pass, no regressions
  • End-to-end headless test with a synthetic image and simulated clicks: region selection, theta sweep, and return value all verified correct
  • Verified live from rvctool and Jupyter against the real parks.png image (region picking, plotting, h.figure.ginput() fix)

… body

Multiple compounding bugs in esttheta()/pickregion(), all part of the
same incomplete migration/port:

- pickregion() called smb.plot_poly() (removed from spatialmath-python)
  immediately before an already-migrated replacement two lines later
  (Polygon2(...).plot(...)) that draws the identical thing -- delete
  the redundant broken leftover rather than fix it.
- The "already-migrated" replacement was itself broken: smb.Polygon2
  doesn't exist (Polygon2 lives in top-level spatialmath, not
  spatialmath.base) -- add the import, matching every other call site
  in the codebase (ImageConstants.py, Sources.py, ImageBlobs.py,
  ImageCore.py, tagtool.py all do `from spatialmath import Polygon2`).
- polygon.plot("g") passed "g" positionally, binding to Polygon2.plot's
  ax parameter (not color) -- a leftover calling convention from the
  old plot_poly(xy, "g", close=True) API. Fixed to polygon.plot(color="g").
- esttheta()'s body never computed or returned theta at all -- only the
  interactive region-picking half of the original MATLAB function
  (toolbox/esttheta.m) was ported; the theta-sweep/variance-minimization
  loop was entirely missing, so the function always implicitly returned
  None despite its own docstring promising a float. Implemented the
  sweep using the already-ported shadow_invariant() (MVTB's existing
  Python port of MATLAB's invariant()/shadowRemoval()), matching the
  original: sweep theta in [0, pi), compute the invariant image at each
  candidate angle, minimize std() over non-inf/nan pixels, return the
  best angle. Added a marker at the minimum and grid to the diagnostic
  plot.
- im.column() -> im.view1d(): column() is already deprecated.
- pickregion() now calls h.figure.ginput(...) on the actual Figure
  handle returned by im.disp(), instead of the module-level
  plt.ginput() (which resolves its target through gcf()'s implicit
  global state). More robust regardless of backend.

Root-caused while working through RVC3-python's chap10.ipynb notebook
test suite. Note: ginput()'s click events are not reliably delivered
under %matplotlib widget (ipympl) in Jupyter -- this is a known,
long-standing upstream ipympl limitation (ipympl#400), not something
fixable here; documented in the notebook as needing %matplotlib tk/qt
for that cell.

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

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 complexity · 0 duplication

Metric Results
Complexity 0
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 940dd49 into main Aug 14, 2026
31 checks passed
@petercorke
petercorke deleted the fix/esttheta-plot-poly-leftover branch October 3, 2026 14:47
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