1 | initial version |
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);
addRemovePt = false;
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;
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];
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]);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
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;
}
2 | No.2 Revision |
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);
addRemovePt = false;
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;
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];
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]);
}
if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
{
vector<Point2f> tmp;
tmp.push_back(point);
cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
points[1].push_back(tmp[0]);
addRemovePt = false;
}
needToInit = false;
imshow("LK Demo", image);
char c = (char)waitKey(10);
if( c == 27 )
break;
switch( c )
{
case 'r':
needToInit = true;
break;
case 'c':
points[0].clear();
points[1].clear();
break;
case 'n':
nightMode = !nightMode;
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;
}