test_optflowpyrlk.cpp

The source file of the test program - Takamichi Tashiro, 2012-06-29 06:38 am

Download (1.5 kB)

 
1
#include <iostream>
2
#include <vector>
3
#include <opencv2/core/core.hpp>
4
#include <opencv2/video/tracking.hpp>
5
#include <opencv2/imgproc/imgproc.hpp>
6
#include <opencv2/highgui/highgui.hpp>
7
8
int main(void)
9
{
10
  // Load the prev image
11
  cv::Mat prev = cv::imread("blocks1.png", 1);
12
  if(prev.empty()) return -1; 
13
  // Load the next image
14
  cv::Mat next = cv::imread("blocks2.png", 1);
15
  if(next.empty()) return -1; 
16
17
  std::vector<cv::Point2f> prev_pts;
18
  std::vector<cv::Point2f> next_pts;
19
  
20
  // Initialize
21
  cv::Size flowSize(30,30);
22
  cv::Point2f center = cv::Point(prev.cols/2., prev.rows/2.);
23
  for(int i=0; i<flowSize.width; ++i) {
24
    for(int j=0; j<flowSize.width; ++j) {
25
      cv::Point2f p(i*float(prev.cols)/(flowSize.width-1), 
26
                    j*float(prev.rows)/(flowSize.height-1));
27
      prev_pts.push_back((p-center)*0.9f+center);
28
    }
29
  }
30
31
  cv::Mat status, error;
32
33
  // Calc optical flows.
34
  cv::calcOpticalFlowPyrLK(prev, next, prev_pts, next_pts, status, error);
35
36
  // Show results.
37
  // If you need necessary, please remove the comment.
38
  /*
39
  std::vector<cv::Point2f>::const_iterator p = prev_pts.begin();
40
  std::vector<cv::Point2f>::const_iterator n = next_pts.begin();
41
  for(; n!=next_pts.end(); ++n,++p) {
42
    cv::line(prev, *p, *n, cv::Scalar(150,0,0),2);
43
  }
44
  
45
  cv::namedWindow("optical flow", CV_WINDOW_AUTOSIZE|CV_WINDOW_FREERATIO);
46
  cv::imshow("optical flow", prev);
47
  cv::waitKey(0);
48
  */
49
50
  return 0;
51
52
  // The heap error occurs after the end of main function.
53
}
54