安卓:使用calcOpticalFlowPyrLK与MatOfPoint2f [英] Android: Using calcOpticalFlowPyrLK with MatOfPoint2f
问题描述
我已经无法使用calcOpticalFlowPyrLK与MatOfPoint2f。我宣布我的类型如下:
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
和使用以下方法来查找和跟踪功能 (我的code是基于C语言的样本光流++应用程序由罗伯特Laganiere。)
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,变量分
是类型名单,其中,名单,其中;点&GT;&GT;
,我会通过实例化一个MatOfPoint2f与 fromlist里
,并递过来calcOpticalFlowPyrLK类型之间的转换。
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.
不过,我想不再做了,因为从并列出了该转换损失点之间的对应关系的初始
和分
。我想preserve这种对应关系,这样我就可以通过遍历同时在两个矩阵的项目画出光流线。
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)
奇怪的是,如果我打电话calcOpticalFlowPyrLK之前添加这种说法我自己,它不会失败。
Strangely enough, if I add this assertion myself, before calling calcOpticalFlowPyrLK, it does not fail.
我希望有人可以帮我找出其中的真正的问题在于,我怎么能preserve帧之间的履带点之间的这种关系。
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:
- 在我不知道的为什么
- 我现在有一个类似的问题Calib3d.solvePnP,但应用的ConvertTo到imagePoints和objectPoints不解决问题就在这里,既不符合
CvType.CV_32FC2
和CvType.CV_32F
或CvType.CV_64F
- 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
要纠正的情况下 calcOpticalFlowPyrLK
的,我已经改变了断言失败 points.get(0).push_back(初始);
为以下内容:
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);
所以我的问题已更改为:?有人可以解释一般情况下我,让我知道该怎么也解决了我的问题,Calib3d.solvePnP
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?
推荐答案
calcOpticalFlowPyrLK点参数的类型MatOfPoint2f它的内部是CV_32FC2的。
calcOpticalFlowPyrLK point parameters are of type MatOfPoint2f which internally is CV_32FC2.
solvePnP第一个参数是 MatOfPoint3f 这是内部的 CV_32FC3 。尝试转换为看它是否解决了。
solvePnP first parameter is MatOfPoint3f which internally is CV_32FC3. Try to convert to that to see if it fixes.
一些OpenCV函数期待具体的垫类型的,我猜猜/自动转换效率的原因,而不是OpenCV的,你必须手动选择是正确的。
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.
这篇关于安卓:使用calcOpticalFlowPyrLK与MatOfPoint2f的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!