Stack Exchange
No good results from my opencv optical flow code
Hi I went through several resources (like opencv website, opencv example codes etc) and wrote c++ code to track the path of a moving object(here it is human) from static camera. Here is my code
int main(int argc, char *argv[])
{

   int MAX_COUNT=100;
   Size SubPixelWinSize(10,10),winSize(30,30);
   TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,30,0.01);

   Mat frame_old;
   Mat gray_old;
   vector<Point2f>points;
   vector<Point2f>pointsnew;
   vector<uchar>status;
   vector<float>err;

   VideoCapture capture("C:\\Users\\Usre\\Desktop\\free\\1.mp4");
   capture.set(CV_CAP_PROP_POS_MSEC,1000);

   if(!capture.isOpened())
   {cout<<"error opening video"<<endl;return -1;}

   else
   {

      capture>>frame_old;

      cvtColor(frame_old,gray_old,COLOR_BGR2GRAY);
      goodFeaturesToTrack(gray_old,points,MAX_COUNT,0.001,1,Mat(),3,0,0.04);
      cornerSubPix(gray_old,points,SubPixelWinSize,Size(-1,-1),termcrit);

      for(;;)
      {
           Mat framenew;
           Mat gray;

          capture>>framenew;waitKey(10);
          cvtColor(framenew,gray,COLOR_BGR2GRAY);


          calcOpticalFlowPyrLK(gray_old,gray,points,pointsnew,status,err,winSize,3,termcrit,0,0.001);
          size_t i,k;
          for(i=k=0;i<pointsnew.size();i++)
          {

              if(!status[i])
                  continue;


              pointsnew[k++]=pointsnew[i];
              Point p1,p2;
              p1.x= (int)points[i].x;
              p1.y= (int)points[i].y;
              p2.x= (int)pointsnew[i].x;
              p2.y= (int)pointsnew[i].y;

              line(framenew,p1,p2,Scalar(0,255,0),3,8,0);


          }


         pointsnew.resize(k);
         swap(points,pointsnew);
         gray.copyTo(gray_old);


        imshow("new",framenew);
        waitKey(10);
        }

      }

return 0;
}

So when running this I got only moving dots with moving person no lines along the path. Some times for some people with no any dots. And the dots are there in the background(I mean on the static ground.It is not moving). I tried by changing parameters but no luck.
The video I used is https://www.youtube.com/watch?v=H7BrVzdOzc4
Questions
1.Why I'm getting these bad results? Why the tracking lines are not drawing(only dots are there)?
2.What are the changes need to be done to get the correct outcome.?
I used Qt creator with visual studio 2010 ,opencv 2.4.8,windows 7 environment.
Think my explanation is clear.
Any help will be highly appreciated .. Thanks!!!!

Optical flow method gives you exactly what it should give: motion vectors between TWO images. You can combine it in to multi-frame trajectories, but you have to do it by yourself.
"Some times for some people with no any dots" because you image is very blurry and people have no good features to track (it is clear from goodFeaturesToTrack(...) method name). You can try constant step mesh for this dots for the first frame (if you are going to combine vectors in to long trajectories).
