fix: use eigh instead of eig for blob orientation/ellipse moments - #25
Open
petercorke wants to merge 2 commits into
Open
fix: use eigh instead of eig for blob orientation/ellipse moments#25petercorke wants to merge 2 commits into
petercorke wants to merge 2 commits into
Conversation
Blob.orientation (and a/b, the equivalent-ellipse axes) computed the eigendecomposition of the second-moment matrix with np.linalg.eig. That matrix is always real-symmetric by construction, but eig is the general non-symmetric solver: for near-degenerate cases (e.g. a square or near-circular blob, where both eigenvalues are equal) it can return complex128 eigenvalues/eigenvectors purely from floating-point noise, even though the true eigenvalues are real. That then made np.arctan2(x[1], x[0]) raise TypeError, crashing Image.blobs() outright. np.linalg.eigh is the correct solver for symmetric matrices and always returns real output, so it doesn't have this failure mode. Found via the existing image_class.rst sidebar fix: Image.blobs() had no docs page before, so its live example never executed during a docs build and this crash went unnoticed.
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 0 |
| 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.
2 tasks
Pull in #26's ci.yml fix so this PR's own workflow file (loaded from this branch's head, not main, for pull_request-triggered runs) actually tests against opencv4 instead of the unpinned conda-forge opencv5 drift.
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
Image.blobs()computed the blob's second-moment matrix eigendecomposition withnp.linalg.eig, the general non-symmetric solver. That matrix is always real-symmetric, but for degenerate cases (a square blob, or any near-circular blob where both eigenvalues are equal — e.g.Image.Squares(...))eigcan returncomplex128output purely from floating-point noise.np.arctan2()then raisedTypeError, crashingblobs()outright for these shapes.np.linalg.eigh, the correct solver for symmetric matrices, which always returns real output.test_orientation_square_blob_degenerate_moments) documenting the failure mode. The existingTestBlobScalarProperties::test_orientationalso already exercised this via theImage.Squares(1, ...)fixture and was failing before this fix on this machine (Apple Silicon / Accelerate BLAS) — may not have surfaced on CI depending on the LAPACK backend there.Image.blobs()had no generated docs page before a separate fix restored it, so its live doctest example had never actually executed during a docs build, and this crash went unnoticed.Test plan
pytest tests/— 816 passed, 0 failed, 15 skipped (pre-existing skips)mainbefore the fix, confirmed it's gone after