Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Issue with calcOpticalFlowPyrLK()

Hello,

I am trying to implement visual odometry via my webcam and the steps I have followed so far are the following:

  1. Capture video from webcam (2 following frames each loop).
  2. Undistort the frames with the callibration parameters I got.
  3. Convert frames to gray scale.
  4. Apply FAST algorithm for feature detection.
  5. And now I am at the calcOpticalFlowPyrLK() step.

My issue is that at step 4 when I print the number of keypoints found in the 2 following frames I get different numbers between the frames (which is logical), but when I do the same thing after the calling of calcOpticalFlowPyrLK() I get the exact same number.

To be more specific, I have wrote:

 vector<KeyPoint> kpointsframe1;
 vector<KeyPoint> kpointsframe2;
 vector<Point2f> pointsframe1;
 vector<Point2f> pointsframe2;
 vector<uchar> status;
 vector<float> err;
 Size winSize = Size(21,21);
 TermCriteria termcrit = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30,0.01);
 Mat Essential;
 capture.read(frame1);
 capture.read(frame2);
 undistort(frame1,frame1undist,CameraMatrix,distCoeffs);
 undistort(frame2,frame2undist,CameraMatrix,distCoeffs);
 cvtColor(frame1undist,frame1gray,CV_BGR2GRAY);
 cvtColor(frame2undist,frame2gray,CV_BGR2GRAY);
 FAST(frame1gray,kpointsframe1,THRESHOLD_DIFFERENCE,false,FastFeatureDetector::TYPE_9_16);
 FAST(frame2gray,kpointsframe2,THRESHOLD_DIFFERENCE,false,FastFeatureDetector::TYPE_9_16);
 for(int i=0;i<kpointsframe1.size();i++)    pointsframe1.push_back(kpointsframe1.at(i).pt);
 for(int i=0;i<kpointsframe2.size();i++)    pointsframe2.push_back(kpointsframe2.at(i).pt);
 calcOpticalFlowPyrLK(frame1gray,frame2gray,pointsframe1,pointsframe2,status,err,winSize,3,termcrit,0,0.0001);

and I print the following in each loop:

 cout<<status.size()<<endl;
 cout<<pointsframe1.size()<<endl;
 cout<<pointsframe2.size()<<endl;
 cout<<"-------------------"<<endl;

If I comment the line

calcOpticalFlowPyrLK(frame1gray,frame2gray,pointsframe1,pointsframe2,status,err,winSize,3,termcrit,0,0.0001);

I get different numbers, but if I leave it like that I get 3 numbers which are all the same in each loop. All the above code is in a while loop. I am wondering why this happens? Shouldn't the calcOpticalFlowPyrLK() try to find the matching between the keypoints of the two frames and give the status of their differences or I am understanding something wrong?

Thanks for answering and for your time in advance,

Chris

Issue with calcOpticalFlowPyrLK()

Hello,

I am trying to implement visual odometry via my webcam and the steps I have followed so far are the following:

  1. Capture video from webcam (2 following frames each loop).
  2. Undistort the frames with the callibration parameters I got.
  3. Convert frames to gray scale.
  4. Apply FAST algorithm for feature detection.
  5. And now I am at the calcOpticalFlowPyrLK() step.

My issue is that at step 4 when I print the number of keypoints found in the 2 following frames I get different numbers between the frames (which is logical), but when I do the same thing after the calling of calcOpticalFlowPyrLK() I get the exact same number.

To be more specific, I have wrote:

 vector<KeyPoint> kpointsframe1;
 vector<KeyPoint> kpointsframe2;
 vector<Point2f> pointsframe1;
 vector<Point2f> pointsframe2;
 vector<uchar> status;
 vector<float> err;
 Size winSize = Size(21,21);
 TermCriteria termcrit = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS,30,0.01);
 Mat Essential;
 capture.read(frame1);
 capture.read(frame2);
 undistort(frame1,frame1undist,CameraMatrix,distCoeffs);
 undistort(frame2,frame2undist,CameraMatrix,distCoeffs);
 cvtColor(frame1undist,frame1gray,CV_BGR2GRAY);
 cvtColor(frame2undist,frame2gray,CV_BGR2GRAY);
 FAST(frame1gray,kpointsframe1,THRESHOLD_DIFFERENCE,false,FastFeatureDetector::TYPE_9_16);
 FAST(frame2gray,kpointsframe2,THRESHOLD_DIFFERENCE,false,FastFeatureDetector::TYPE_9_16);
 for(int i=0;i<kpointsframe1.size();i++)    pointsframe1.push_back(kpointsframe1.at(i).pt);
 for(int i=0;i<kpointsframe2.size();i++)    pointsframe2.push_back(kpointsframe2.at(i).pt);
 calcOpticalFlowPyrLK(frame1gray,frame2gray,pointsframe1,pointsframe2,status,err,winSize,3,termcrit,0,0.0001);

and I print the following in each loop:

 cout<<status.size()<<endl;
 cout<<pointsframe1.size()<<endl;
 cout<<pointsframe2.size()<<endl;
 cout<<"-------------------"<<endl;

If I comment the line

calcOpticalFlowPyrLK(frame1gray,frame2gray,pointsframe1,pointsframe2,status,err,winSize,3,termcrit,0,0.0001);

I get different numbers, but if I leave it like that I get 3 numbers which are all the same in each loop. All the above code is in a while loop. I am wondering why this happens? Shouldn't the calcOpticalFlowPyrLK() try to find the matching between the keypoints of the two frames and give the status of their differences or I am understanding something wrong?

Thanks I am new to image processing so try to bare with me for any potential questions that may arise.

Thank you for answering and for your time in advance,

Chris