opencv-js icon indicating copy to clipboard operation
opencv-js copied to clipboard

Document calcOpticalFlowPyrLK error parameter usage

Open Copilot opened this issue 3 months ago • 0 comments

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?
  • Fixes TechStark/opencv-js#59

✨ 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 avatar Nov 09 '25 13:11 Copilot