Skip to content

fix(cmc): prevent cv2.resize crash on tiny downscaled images#488

Open
pranaysb wants to merge 4 commits into
roboflow:developfrom
pranaysb:fix/cmc-downscale-crash
Open

fix(cmc): prevent cv2.resize crash on tiny downscaled images#488
pranaysb wants to merge 4 commits into
roboflow:developfrom
pranaysb:fix/cmc-downscale-crash

Conversation

@pranaysb

@pranaysb pranaysb commented Jul 2, 2026

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes a robustness bug in the Camera Motion Compensation (CMC) module where providing a very small frame or bounding box crop would cause an unhandled crash.

When img_w or img_h was smaller than the downscale factor (default 2), the dimension evaluated to 0. Passing (0, 0) into cv2.resize triggered an unhandled OpenCV C++ assertion error (cv2.error).

This PR:

  1. Adds defensive dimension bounding (max(1, img_w // self.downscale)) across _estimate_feature_affine, _estimate_sparse_optflow, and _estimate_ecc to ensure valid dimensions.
  2. Wraps the SIFT/ORB feature computation in a try/except cv2.error block to cleanly catch a secondary native crash that occurs when SIFT is asked to process an image smaller than 3x3 pixels.

Related Issue(s): Fixes #487

Type of Change

  • Bug fix (non-breaking change that fixes an issue)

Testing

  • I have tested this change locally
  • I have added/updated tests for this change

Test details:
Added test_cmc_downscale_zero_dimension parameterized over all CMC methods (sparseOptFlow, orb, sift, ecc) to assert that feeding a 1x1 image to the estimator gracefully returns the identity matrix instead of crashing.

uv run pytest tests/ → 855 passed, 2 skipped, 0 failures.

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in hard-to-understand areas
  • My changes generate no new warnings or errors
  • I have updated the documentation accordingly (if applicable)

Additional Context

See the linked issue #487 for full tracebacks and a reproduction script demonstrating the cv2.error.
Attached is a screenshot demonstrating the fix in action on this branch. As shown, the reproduction script now executes successfully without throwing the cv2.error exception, and the newly added unit tests successfully pass for all four CMC estimation methods
After resolving this in new branch:-
image
image

@pranaysb pranaysb requested a review from SkalskiP as a code owner July 2, 2026 18:02
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.

[Bug] Unhandled cv2.error crash in CMC resize on tiny downscaled images

1 participant