annotate c/optical-flow.cpp @ 12:ff5403319cec

optical flow demo working
author Nicolas Saunier <nico@confins.net>
date Wed, 11 Nov 2009 23:25:23 -0500
parents e77e2fd69b02
children 3ead4bcd001c
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
1 #include "cvutils.hpp"
12
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
2 #include "utils.hpp"
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
3
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
4 #include "opencv/cv.h"
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
5 #include "opencv/highgui.h"
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
6
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
7 #include <iostream>
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
8 #include <ctime>
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
9
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
10 using namespace std;
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
11
12
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
12 void videoTiming(CvCapture* inputVideo) {
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
13 IplImage* frame = cvQueryFrame(inputVideo);
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
14 //IplImage* bwFrame = allocateImage(frame->width, frame->height, IPL_DEPTH_8U, 1);
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
15
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
16 int frameNum = 0;
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
17 time_t seconds;
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
18 time_t t0 = time(NULL);
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
19 while (frame) {
12
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
20 frameNum = ::goToFrameNum(inputVideo, frameNum, frameNum+1000);
11
e77e2fd69b02 modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents: 10
diff changeset
21 seconds = time(NULL)-t0;
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
22
11
e77e2fd69b02 modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents: 10
diff changeset
23 cout << frameNum << " " << seconds << endl;
12
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
24 }
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
25 }
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
26
12
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
27 int main(int argc, char *argv[]) {
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
28 //cout << "Hello World" << endl;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
29
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
30 CvCapture *inputVideo = 0;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
31 if (argc == 1)
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
32 inputVideo = cvCreateCameraCapture(-1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
33 else
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
34 inputVideo = cvCaptureFromFile(argv[1]);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
35
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
36 int frameNum = 0;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
37 cvNamedWindow("Optical Flow", CV_WINDOW_AUTOSIZE);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
38
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
39 // allocate space for pyramids
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
40 IplImage* frame = cvQueryFrame(inputVideo);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
41 CvSize frameSize = cvSize(frame->width, frame->height);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
42
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
43 IplImage* frame1_1C = ::allocateImage(frameSize, IPL_DEPTH_8U, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
44 cvConvertImage(frame, frame1_1C);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
45
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
46 IplImage *frame1 = ::allocateImage(frameSize, IPL_DEPTH_8U, 3);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
47 cvConvertImage(frame, frame1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
48
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
49 frame = cvQueryFrame(inputVideo);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
50
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
51 IplImage* frame2_1C = ::allocateImage(frameSize, IPL_DEPTH_8U, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
52 cvConvertImage(frame, frame2_1C);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
53
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
54 IplImage *eig_image = ::allocateImage(frameSize, IPL_DEPTH_32F, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
55 IplImage *temp_image = ::allocateImage(frameSize, IPL_DEPTH_32F, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
56
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
57 int nFeatures = 1000;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
58 CvPoint2D32f frame1_features[1000];
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
59
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
60 CvPoint2D32f frame2_features[1000];
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
61
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
62 char optical_flow_found_feature[1000];
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
63 float optical_flow_feature_error[1000];
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
64
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
65 CvSize optical_flow_window = cvSize(3,3);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
66
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
67 CvTermCriteria optical_flow_termination_criteria = cvTermCriteria( CV_TERMCRIT_ITER | CV_TERMCRIT_EPS, 20, .3 );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
68 IplImage* pyramid1 = ::allocateImage(frameSize, IPL_DEPTH_8U, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
69 IplImage* pyramid2 = ::allocateImage(frameSize, IPL_DEPTH_8U, 1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
70
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
71 int pressedKey = '?';
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
72 while (frame && !::interruptionKey(pressedKey)) {
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
73 cvGoodFeaturesToTrack(frame1_1C, eig_image, temp_image, frame1_features, &nFeatures, .01, .01, NULL);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
74 cvCalcOpticalFlowPyrLK(frame1_1C, frame2_1C, pyramid1, pyramid2, frame1_features, frame2_features, nFeatures, optical_flow_window, 5, optical_flow_found_feature, optical_flow_feature_error, optical_flow_termination_criteria, 0 );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
75
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
76 for(int i = 0; i < nFeatures; i++) {
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
77 /* If Pyramidal Lucas Kanade didn't really find the feature, skip it. */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
78 if ( optical_flow_found_feature[i] == 0 ) continue;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
79
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
80 int line_thickness; line_thickness = 1;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
81 /* CV_RGB(red, green, blue) is the red, green, and blue components
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
82 * of the color you want, each out of 255.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
83 */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
84 CvScalar line_color; line_color = CV_RGB(255,0,0);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
85
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
86 /* Let's make the flow field look nice with arrows. */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
87
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
88 /* The arrows will be a bit too short for a nice visualization because of the high framerate
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
89 * (ie: there's not much motion between the frames). So let's lengthen them by a factor of 3.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
90 */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
91 CvPoint p,q;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
92 p.x = (int) frame1_features[i].x;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
93 p.y = (int) frame1_features[i].y;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
94 q.x = (int) frame2_features[i].x;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
95 q.y = (int) frame2_features[i].y;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
96
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
97 double angle; angle = atan2( (double) p.y - q.y, (double) p.x - q.x );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
98 double hypotenuse; hypotenuse = sqrt( square(p.y - q.y) + square(p.x - q.x) );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
99
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
100 /* Here we lengthen the arrow by a factor of three. */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
101 q.x = (int) (p.x - 3 * hypotenuse * cos(angle));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
102 q.y = (int) (p.y - 3 * hypotenuse * sin(angle));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
103
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
104 /* Now we draw the main line of the arrow. */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
105 /* "frame1" is the frame to draw on.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
106 * "p" is the point where the line begins.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
107 * "q" is the point where the line stops.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
108 * "CV_AA" means antialiased drawing.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
109 * "0" means no fractional bits in the center cooridinate or radius.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
110 */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
111 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
112 /* Now draw the tips of the arrow. I do some scaling so that the
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
113 * tips look proportional to the main line of the arrow.
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
114 */
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
115 p.x = (int) (q.x + 9 * cos(angle + pi / 4));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
116 p.y = (int) (q.y + 9 * sin(angle + pi / 4));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
117 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
118 p.x = (int) (q.x + 9 * cos(angle - pi / 4));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
119 p.y = (int) (q.y + 9 * sin(angle - pi / 4));
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
120 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 );
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
121 }
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
122 cvShowImage("Optical Flow", frame1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
123 //cvWaitKey(5);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
124 pressedKey = cvWaitKey(5);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
125 frame = cvQueryFrame(inputVideo);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
126 frameNum++;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
127 cout << frameNum << endl;
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
128
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
129 cvCopy(frame2_1C, frame1_1C);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
130 cvCopy(pyramid2, pyramid1);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
131 cvConvertImage(frame, frame2_1C);
ff5403319cec optical flow demo working
Nicolas Saunier <nico@confins.net>
parents: 11
diff changeset
132 cvConvertImage(frame, frame1);
10
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
133 }
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
134
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
135 return 1;
068cf45c3f1b added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff changeset
136 }