lkdemo.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. #include "opencv2/video/tracking.hpp"
  2. #include "opencv2/imgproc.hpp"
  3. #include "opencv2/videoio.hpp"
  4. #include "opencv2/highgui.hpp"
  5. #include <iostream>
  6. #include <ctype.h>
  7. using namespace cv;
  8. using namespace std;
  9. static void help()
  10. {
  11. // print a welcome message, and the OpenCV version
  12. cout << "\nThis is a demo of Lukas-Kanade optical flow lkdemo(),\n"
  13. "Using OpenCV version " << CV_VERSION << endl;
  14. cout << "\nIt uses camera by default, but you can provide a path to video as an argument.\n";
  15. cout << "\nHot keys: \n"
  16. "\tESC - quit the program\n"
  17. "\tr - auto-initialize tracking\n"
  18. "\tc - delete all the points\n"
  19. "\tn - switch the \"night\" mode on/off\n"
  20. "To add/remove a feature point click it\n" << endl;
  21. }
  22. Point2f point;
  23. bool addRemovePt = false;
  24. static void onMouse( int event, int x, int y, int /*flags*/, void* /*param*/ )
  25. {
  26. if( event == EVENT_LBUTTONDOWN )
  27. {
  28. point = Point2f((float)x, (float)y);
  29. addRemovePt = true;
  30. }
  31. }
  32. int main( int argc, char** argv )
  33. {
  34. VideoCapture cap;
  35. TermCriteria termcrit(TermCriteria::COUNT|TermCriteria::EPS,20,0.03);
  36. Size subPixWinSize(10,10), winSize(31,31);
  37. const int MAX_COUNT = 500;
  38. bool needToInit = false;
  39. bool nightMode = false;
  40. help();
  41. cv::CommandLineParser parser(argc, argv, "{@input|0|}");
  42. string input = parser.get<string>("@input");
  43. if( input.size() == 1 && isdigit(input[0]) )
  44. cap.open(input[0] - '0');
  45. else
  46. cap.open(input);
  47. if( !cap.isOpened() )
  48. {
  49. cout << "Could not initialize capturing...\n";
  50. return 0;
  51. }
  52. namedWindow( "LK Demo", 1 );
  53. setMouseCallback( "LK Demo", onMouse, 0 );
  54. Mat gray, prevGray, image, frame;
  55. vector<Point2f> points[2];
  56. for(;;)
  57. {
  58. cap >> frame;
  59. if( frame.empty() )
  60. break;
  61. frame.copyTo(image);
  62. cvtColor(image, gray, COLOR_BGR2GRAY);
  63. if( nightMode )
  64. image = Scalar::all(0);
  65. if( needToInit )
  66. {
  67. // automatic initialization
  68. goodFeaturesToTrack(gray, points[1], MAX_COUNT, 0.01, 10, Mat(), 3, 3, 0, 0.04);
  69. cornerSubPix(gray, points[1], subPixWinSize, Size(-1,-1), termcrit);
  70. addRemovePt = false;
  71. }
  72. else if( !points[0].empty() )
  73. {
  74. vector<uchar> status;
  75. vector<float> err;
  76. if(prevGray.empty())
  77. gray.copyTo(prevGray);
  78. calcOpticalFlowPyrLK(prevGray, gray, points[0], points[1], status, err, winSize,
  79. 3, termcrit, 0, 0.001);
  80. size_t i, k;
  81. for( i = k = 0; i < points[1].size(); i++ )
  82. {
  83. if( addRemovePt )
  84. {
  85. if( norm(point - points[1][i]) <= 5 )
  86. {
  87. addRemovePt = false;
  88. continue;
  89. }
  90. }
  91. if( !status[i] )
  92. continue;
  93. points[1][k++] = points[1][i];
  94. circle( image, points[1][i], 3, Scalar(0,255,0), -1, 8);
  95. }
  96. points[1].resize(k);
  97. }
  98. if( addRemovePt && points[1].size() < (size_t)MAX_COUNT )
  99. {
  100. vector<Point2f> tmp;
  101. tmp.push_back(point);
  102. cornerSubPix( gray, tmp, winSize, Size(-1,-1), termcrit);
  103. points[1].push_back(tmp[0]);
  104. addRemovePt = false;
  105. }
  106. needToInit = false;
  107. imshow("LK Demo", image);
  108. char c = (char)waitKey(10);
  109. if( c == 27 )
  110. break;
  111. switch( c )
  112. {
  113. case 'r':
  114. needToInit = true;
  115. break;
  116. case 'c':
  117. points[0].clear();
  118. points[1].clear();
  119. break;
  120. case 'n':
  121. nightMode = !nightMode;
  122. break;
  123. }
  124. std::swap(points[1], points[0]);
  125. cv::swap(prevGray, gray);
  126. }
  127. return 0;
  128. }