phase_corr.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "opencv2/core.hpp"
  2. #include "opencv2/videoio.hpp"
  3. #include "opencv2/highgui.hpp"
  4. #include "opencv2/imgproc.hpp"
  5. using namespace cv;
  6. int main(int, char* [])
  7. {
  8. VideoCapture video(0);
  9. Mat frame, curr, prev, curr64f, prev64f, hann;
  10. char key;
  11. do
  12. {
  13. video >> frame;
  14. cvtColor(frame, curr, COLOR_RGB2GRAY);
  15. if(prev.empty())
  16. {
  17. prev = curr.clone();
  18. createHanningWindow(hann, curr.size(), CV_64F);
  19. }
  20. prev.convertTo(prev64f, CV_64F);
  21. curr.convertTo(curr64f, CV_64F);
  22. Point2d shift = phaseCorrelate(prev64f, curr64f, hann);
  23. double radius = std::sqrt(shift.x*shift.x + shift.y*shift.y);
  24. if(radius > 5)
  25. {
  26. // draw a circle and line indicating the shift direction...
  27. Point center(curr.cols >> 1, curr.rows >> 1);
  28. circle(frame, center, (int)radius, Scalar(0, 255, 0), 3, LINE_AA);
  29. line(frame, center, Point(center.x + (int)shift.x, center.y + (int)shift.y), Scalar(0, 255, 0), 3, LINE_AA);
  30. }
  31. imshow("phase shift", frame);
  32. key = (char)waitKey(2);
  33. prev = curr.clone();
  34. } while(key != 27); // Esc to exit...
  35. return 0;
  36. }