Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 9, 2025

Users were calling cv.exceptionFromPtr(err) on the err parameter of calcOpticalFlowPyrLK, causing TypeError exceptions. The err parameter is an OutputArray (Mat) for error metrics, not an exception pointer.

Changes

  • Added JSDoc clarification to calcOpticalFlowPyrLK type definition

    • Explicitly documents that err is an OUTPUT Mat for error metrics per tracked point
    • Warns against passing it to exceptionFromPtr()
    • Includes complete usage example with proper exception handling via try-catch + translateException()
  • Added test coverage in test/OpticalFlow.test.ts

    • Demonstrates correct usage pattern with goodFeaturesToTrack + calcOpticalFlowPyrLK
    • Shows proper exception handling for invalid inputs
    • Illustrates accessing error metrics via err.data32F[i]

Correct Usage

try {
  const err = new cv.Mat(); // Stores error metrics, not exception pointers
  cv.calcOpticalFlowPyrLK(prevGray, curGray, prevPts, nextPts, status, err, ...);
  
  for (let i = 0; i < status.rows; i++) {
    if (status.data[i] === 1) {
      const errorMetric = err.data32F[i]; // Access per-point error metric
    }
  }
} catch (error) {
  throw translateException(error); // Handle OpenCV exceptions
}
Original prompt

This section details on the original issue you should resolve

<issue_title>Error with calcOpticalFlowPyrLK</issue_title>
<issue_description>I'm having trouble with calcOpticalFlowPyrLK. Sometimes it returns an error, sometimes it doesn't. I've double-checked the input data, and it seems to be correct.

Please help me, thank you.

cv.calcOpticalFlowPyrLK(
    preGray,
    curGray,
    p0,
    p1,
    st,
    err,
    winSize,
    maxLevel,
    criteria,
  );

  cv.exceptionFromPtr(err);

error
image
</issue_description>

Comments on the Issue (you are @copilot in this section)

@ttt43ttt try this ``` if (typeof err === "number") { throw cv.exceptionFromPtr(err); } ``` @ttt43ttt are the objects (passed as `cv.calcOpticalFlowPyrLK` parameters) reused between different function calls?

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI changed the title [WIP] Fix error handling in calcOpticalFlowPyrLK Document calcOpticalFlowPyrLK error parameter usage Nov 9, 2025
Copilot AI requested a review from ttt43ttt November 9, 2025 13:49
Copilot finished work on behalf of ttt43ttt November 9, 2025 13:49
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.

Error with calcOpticalFlowPyrLK

2 participants