我正在尝试使用openCV的calcOpticalFlowPyrLK函数进行运动检测,但我收到错误消息:
OpenCV Error: Assertion failed ((npoints = prevPtsMat.checkVector(2, CV_32F, tru
e)) >= 0) in cv::calcOpticalFlowPyrLK, file ..\..\..\..\opencv\modules\video\src
\lkpyramid.cpp, line 845
我已经检查过存储在我的代码的corner1中的前一个角不是空的并且是有效点。“goodFeaturesToTrack”函数工作正常。 我被困在这里2天,任何帮助将不胜感激。
代码块:
int opticalflow()
{
Mat frame1, frame2, frame3, difference1, difference2, frame, mask;
VideoCapture capture;
vector <Point2d> corners1;
vector <Point2d> corners2;
int maxCorners = 200;
double qualityLevel = 0.01;
double minDistance = 5;
int blockSize = 3;
bool useHarrisDetector = false;
double k = 0.04;
vector <unsigned char> optical_flow_found_feature;
vector <float > optical_flow_feature_error;
//Read the video stream
capture.open(0);
if (!capture.isOpened())
{
printf("Error Opening Video Capture!!!\n");
}
waitKey(10);
capture.read(frame1);
cvtColor( frame1, frame1, COLOR_BGR2GRAY );
goodFeaturesToTrack( frame1, corners1, maxCorners, qualityLevel, minDistance, mask, blockSize, useHarrisDetector, k );
for(int i=0; i<corners1.size(); i++)
cout<<corners1[i].x<<" "<<corners1[i].y<<endl;
while (1)
{
capture.read(frame2);
cvtColor( frame2, frame2, COLOR_BGR2GRAY );
if(corners1.size()>10)
calcOpticalFlowPyrLK(frame1, frame2, corners1, corners2, optical_flow_found_feature, optical_flow_feature_error);// Size(21,21), 3, TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), 0, 1e-4);
cout<<"Reached Here\n";
for(int i=0; i<maxCorners; i++)
{
if(optical_flow_found_feature[i] == 0)
continue;
int line_thickness = 1;
CvScalar line_color = CV_RGB(255,0,0);
Point2d p,q;
p.x = (int) corners1[i].x;
p.y = (int) corners1[i].y;
q.x = (int) corners2[i].x;
q.y = (int) corners2[i].y;
double angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
double hypotenuse = sqrt((long double)(p.y - q.y)*(p.y - q.y) + (p.x - q.x)*(p.x - q.x) );
q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
p.x = (int) (q.x + 9 * cos(angle + pi / 4));
p.y = (int) (q.y + 9 * sin(angle + pi / 4));
line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
p.x = (int) (q.x + 9 * cos(angle - pi / 4));
p.y = (int) (q.y + 9 * sin(angle - pi / 4));
line( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
}
display(frame1);
int c = waitKey(25);
//Exit if escape is pressed
if((char)c == 27)
{
break;
}
}
return 0;
}
平台: 我正在使用Visual Studio 2010 OpenCV 2.4.9 在Windows 8上