问题
I have been unable to use calcOpticalFlowPyrLK with MatOfPoint2f. I declared my types as follows:
private Mat mPreviousGray; // previous gray-level image
private List<MatOfPoint2f> points; // tracked features
private MatOfPoint initial; // initial position of tracked points
And use the following to find and track features (My code is based on a sample optical flow application in C++ by Robert Laganiere.)
// Called whenever a new frame m is captured
private void OpticalFlow(Mat m, int maxDetectionCount, double qualityLevel,
double minDistance) {
if (points.get(0).total() < maxDetectionCount/2) // Check if new points need to be added
{
// maxDetectionCount = 500
// qualityLevel = 0.01
// minDistance = 10
Imgproc.goodFeaturesToTrack(m, initial, maxDetectionCount, qualityLevel, minDistance);
// add the detected features to the currently tracked features
points.get(0).push_back(initial);
// Have checked length of points.get(0), is not zero.
}
// for first image of the sequence
if(mPreviousGray.empty())
m.copyTo(mPreviousGray);
if( points.get(0).total() > 0 ) // EMG - 09/22/11 - fix optical flow crashing bug
{
// 2. track features
Video.calcOpticalFlowPyrLK(mPreviousGray, m, // 2 consecutive images
points.get(0), // input point position in first image
points.get(1), // output point postion in the second image
status, // tracking success
error); // tracking error
}
...
m.copyTo(mPreviousGray);
...
}
Previously, variable points
was of type List<List<Point>>
and I would convert between the types by instantiating a MatOfPoint2f with fromList
and passing that to calcOpticalFlowPyrLK.
However, I wanted to no longer do that, because this conversion from and to lists loses the correspondence between points in initial
and points
. I want to preserve this correspondence, so I can draw optical flow lines by iterating over the items in both matrices simultaneously.
Unfortunately, now I have the following assertion failure:
09-24 10:04:30.400: E/cv::error()(8216): OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0) in void cv::calcOpticalFlowPyrLK(const cv::_InputArray&, const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, const cv::_OutputArray&, const cv::_OutputArray&, cv::Size, int, cv::TermCriteria, int, double), file X:\Dev\git\opencv-2.4\modules\video\src\lkpyramid.cpp, line 593
09-24 10:04:30.400: E/AndroidRuntime(8216): FATAL EXCEPTION: Thread-321
09-24 10:04:30.400: E/AndroidRuntime(8216): CvException [org.opencv.core.CvException: X:\Dev\git\opencv-2.4\modules\video\src\lkpyramid.cpp:593: error: (-215) (npoints = prevPtsMat.checkVector(2, CV_32F, true)) >= 0 in function void cv::calcOpticalFlowPyrLK(const cv::_InputArray&, const cv::_InputArray&, const cv::_InputArray&, const cv::_OutputArray&, const cv::_OutputArray&, const cv::_OutputArray&, cv::Size, int, cv::TermCriteria, int, double)
09-24 10:04:30.400: E/AndroidRuntime(8216): ]
09-24 10:04:30.400: E/AndroidRuntime(8216): at org.opencv.video.Video.calcOpticalFlowPyrLK_2(Native Method)
09-24 10:04:30.400: E/AndroidRuntime(8216): at org.opencv.video.Video.calcOpticalFlowPyrLK(Video.java:445)
Strangely enough, if I add this assertion myself, before calling calcOpticalFlowPyrLK, it does not fail.
I'm hoping someone can help me figure out where the real problem lies, and how I can preserve this relationship between the tracked points between frames.
Edit: I've discovered what needs to be done to avoid this assertion error, and the application then behaves correctly, but:
- I don't know why.
- I have a similar problem now for Calib3d.solvePnP, but applying convertTo to the imagePoints and objectPoints does not solve the problem here, neither with
CvType.CV_32FC2
, norCvType.CV_32F
orCvType.CV_64F
To correct the assertion failure in case of calcOpticalFlowPyrLK
, I've changed points.get(0).push_back(initial);
to the following:
Imgproc.goodFeaturesToTrack(m, initial, maxDetectionCount, qualityLevel, minDistance);
MatOfPoint2f initial2f = new MatOfPoint2f();
initial.convertTo(initial2f, CvType.CV_32FC2);
// add the detected features to the currently tracked features
trackedpoints.get(0).push_back(initial2f);
So my question has changed to: Can someone explain the general case for me, so that I know how to solve also my problem with Calib3d.solvePnP?
回答1:
calcOpticalFlowPyrLK point parameters are of type MatOfPoint2f which internally is CV_32FC2.
solvePnP first parameter is MatOfPoint3f which internally is CV_32FC3. Try to convert to that to see if it fixes.
Some opencv functions are expecting specific Mat types, I guess for efficiency reasons you have to manually choose the right ones, instead of opencv guessing/converting automatically.
回答2:
someone in other thread had this problem before. I recently had this problem (Assertion) in the optical flow function of OpenCV4Android. this solution only works when you could not handle previous points matrix:
instead of using convertTo() method you should put each element of Keypoint[] array you've got from matOfKeyPoints.ToArray() into the matOfPoints2f array.
the reason is explained in this page:
KeyPoint[] arrayOfkp = keypoints.toArray();
org.opencv.core.Point[] arrayOfp = new org.opencv.core.Point[arrayOfkp.length];
for(int j =0;j<arrayOfkp.length;j++)
{
arrayOfp[j] = new org.opencv.core.Point(0, 0);
arrayOfp[j].x = (int) arrayOfkp[j].pt.x;
arrayOfp[j].y = (int) arrayOfkp[j].pt.y;
}
prevPts.fromArray(arrayOfp);
来源:https://stackoverflow.com/questions/12561292/android-using-calcopticalflowpyrlk-with-matofpoint2f