like @berak said and using lkdemo.cpp and using @LorenaGdL answer it works and using this video
int main(int argc, char* argv[])
{
VideoCapture cap;
string filename1 = "baby_mp4.mp4";
cap.open(filename1);
if (!cap.isOpened())
{
cout << "Could not initialize capturing for camera...\n";
return 0;
}
cout<< "\nCAP_PROP_POS_AVI_RATIO "<< cap.get(CAP_PROP_POS_AVI_RATIO);
cout<< "\nCAP_PROP_POS_FRAMES "<< cap.get(CAP_PROP_POS_FRAMES);
cout<< "\nCAP_PROP_FPS "<< cap.get(CAP_PROP_FPS);
int fourcc = cap.get(CAP_PROP_FOURCC);
string fourcc_str = format("%c%c%c%c", fourcc & 255, (fourcc >> 8) & 255, (fourcc >> 16) & 255, (fourcc >> 24) & 255);
cout << "\n--->CAP_PROP_FOURCC: " << fourcc_str << endl;
cout<< "\nCAP_PROP_FRAME_COUNT "<< cap.get(CAP_PROP_FRAME_COUNT);
cout<< "\nCAP_PROP_FORMAT "<< cap.get(CAP_PROP_FORMAT);
cout<< "\nCAP_PROP_MODE "<< cap.get(CAP_PROP_MODE);
cout<< "\nCAP_PROP_FORMAT "<< cap.get(CAP_PROP_FORMAT);
cout<< "\nCAP_PROP_POS_MSEC "<< cap.get(CAP_PROP_POS_MSEC);
TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
Size subPixWinSize(10,10), winSize(31,31);
const int MAX_COUNT = 500;
bool needToInit = true;
bool nightMode = false;
namedWindow( "LK Demo", 1 );
setMouseCallback( "LK Demo", onMouse, 0 );
Mat gray, prevGray, image, frame;
vector<Point2f> points[2];
vector<vector<Point2f>> pointsFrame;
for(;;)
{
cap >> frame;
if( frame.empty() )
break;
frame.copyTo(image);
cvtColor(image, gray, COLOR_BGR2GRAY);
if( nightMode )
image = Scalar::all(0);
if( needToInit )
{
// automatic initialization
goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 0, 0.04);
cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
pointsFrame.push_back(points[1]);
needToInit=false;
}
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;
if( !status[i] )
continue;
points[1][k++] = points[1][i];
circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
putText(image,format("%d",i),points[1][i],1,1,Vec3b(0,0,0));
}
points[1].resize(k);
pointsFrame.push_back(points[1]);
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
std::swap(points[1], points[0]);
cv::swap(prevGray, gray);
}
cout << "\nFrame : " << pointsFrame.size();
for (int i=0;i<pointsFrame.size();i++)
cout <<"\nFrame "<<i<<"--> points :"<< pointsFrame[i].size();
for (int i=0;i<pointsFrame.size();i++)
cout <<"\nFrame "<<i<<"--> points 143 :"<< pointsFrame[i][143];
return 0;
}
since there will be different number of points in each iteration, you can't use a 2d Mat for this (which requires equal length for each row)
rather use a
vector<vector<Point2f>> allpoints
, andallpoints.push_back(points[0]);
to store them