Mercurial Hosting > traffic-intelligence
annotate c/optical-flow.cpp @ 59:f955e83da499
developed indicators in interactions
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 29 Oct 2010 01:55:00 -0400 |
parents | 3ead4bcd001c |
children | bc4ea09b1743 |
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)) { |
15 | 73 cvGoodFeaturesToTrack(frame1_1C, eig_image, temp_image, frame1_features, &nFeatures, 0.05 /*quality*/, 1/* min dist*/, NULL); |
12
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++) { |
15 | 77 if (optical_flow_found_feature[i] != 0) { |
78 int line_thickness = 1; | |
79 CvScalar line_color = CV_RGB(255,0,0); | |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
80 |
15 | 81 CvPoint p,q; |
82 p.x = lrintf(frame1_features[i].x); | |
83 p.y = lrintf(frame1_features[i].y); | |
84 q.x = lrintf(frame2_features[i].x); | |
85 q.y = lrintf(frame2_features[i].y); | |
86 | |
87 double dx = frame2_features[i].x-frame1_features[i].x; | |
88 double dy = frame2_features[i].y-frame1_features[i].y; | |
89 | |
90 double angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); | |
91 double dist = sqrt(square(dx) + square(dy)); | |
92 | |
93 if (dist > 2.) { // min motion distance | |
94 q.x = lrintf(frame1_features[i].x+dx*3); //(p.x - 3 * hypotenuse * cos(angle)); | |
95 q.y = lrintf(frame1_features[i].y+dy*3); //(p.y - 3 * hypotenuse * sin(angle)); | |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
96 |
15 | 97 /* Now we draw the main line of the arrow. */ |
98 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
99 /* Now draw the tips of the arrow. I do some scaling so that the | |
100 * tips look proportional to the main line of the arrow. | |
101 */ | |
102 float arrowSize = 6; | |
103 p.x = lrintf(q.x + arrowSize * cos(angle + pi / 4)); | |
104 p.y = lrintf(q.y + arrowSize * sin(angle + pi / 4)); | |
105 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
106 p.x = lrintf(q.x + arrowSize * cos(angle - pi / 4)); | |
107 p.y = lrintf(q.y + arrowSize * sin(angle - pi / 4)); | |
108 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
109 } | |
110 } | |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
111 } |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
112 cvShowImage("Optical Flow", frame1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
113 pressedKey = cvWaitKey(5); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
114 frame = cvQueryFrame(inputVideo); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
115 frameNum++; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
116 cout << frameNum << endl; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
117 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
118 cvCopy(frame2_1C, frame1_1C); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
119 cvCopy(pyramid2, pyramid1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
120 cvConvertImage(frame, frame2_1C); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
121 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
|
122 } |
068cf45c3f1b
added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
123 |
068cf45c3f1b
added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
124 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
|
125 } |