0

I'm running the sparse optical flow demo at link here and I'm simply trying to draw a line between a single point being tracked across all the frames in a video.

Within the for loop,

int LastX = 0;
int LastY = 0;
int px, py;


else if( !points[0].empty() )
  {
    vector<uchar> status;
     vector<float> err;
     if(prevGray.empty())
      gray.copyTo(prevGray);
      calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
                                     3, termcrit, 0, 0.001);
      size_t i, k;
    for( i = k = 0; i < points[1].size(); i++ )
       {
         if( addRemovePt )
           {
              if( norm(point - points[1][i]) <= 5 )
                 {
                    addRemovePt = false;
                    continue;
                  }
             }

          if( !status[i] )
          continue;
          points[1][k++] = points[1][i];

           px = points[1][i].x;
           py = points [1][i].y;

       if (LastX > 0)
          {
            line(image, Point(px, py), Point(LastX, LastY), Scalar(255, 0, 0), 5);
          }

         LastX = px;
         LastY = py;
         circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
      }
        points[1].resize(k);

    }

However, the line only appears in the current frame and the position of the line doesn't save in previous frames. Could someone please tell me what I'm doing wrong?

MaskedAfrican
  • 196
  • 2
  • 12
  • 1
    So to be a little more precise, on frame `n`, you want to display the current frame `n` and the lines from frame 0 to frame 1, frame 1 to frame 2, up to frame `n-1` to frame `n`. Is that correct? – alkasm Oct 03 '17 at 23:39
  • @AlexanderReynolds yes. That's exactly what I'm trying to do – MaskedAfrican Oct 04 '17 at 07:04
  • Then I'd go with the approach below; simply store the points in a `vector` and at each frame, iterate through the whole vector in a for loop and draw from the first point to second, second to third, etc. Then when you detect the next point you can simply `push_back`. – alkasm Oct 04 '17 at 18:16
  • Great idea! That seemed to work, thanks. – MaskedAfrican Oct 05 '17 at 11:34

1 Answers1

0

Try to use a vector<vector<Point>> to store the matched points pairs from the first frame to the last.

The matching process of the flow points is similar with the car tracking.

You can referer to this one Counting cars by Dan Mašek .

Kinght 金
  • 17,681
  • 4
  • 60
  • 74