Mercurial Hosting > traffic-intelligence
annotate c/optical-flow.cpp @ 1189:ccab20f85710
changed to better parameters
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Sat, 25 Jun 2022 01:09:20 +0200 |
parents | bc4ea09b1743 |
children |
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 |
230
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
10 /* MSVC does not have lrintf */ |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
11 #ifdef _MSC_VER |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
12 static inline long lrintf(float f){ |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
13 /* x64 does not supported embedded assembly */ |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
14 #ifdef _M_X64 |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
15 return (long)((f>0.0f) ? (f + 0.5f):(f -0.5f)); |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
16 #else |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
17 int i; |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
18 |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
19 _asm{ |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
20 fld f |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
21 fistp i |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
22 }; |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
23 |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
24 return i; |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
25 #endif |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
26 } |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
27 #endif |
bc4ea09b1743
compatibility modifications for visual studio compilation
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
15
diff
changeset
|
28 |
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
|
29 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
|
30 |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
31 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
|
32 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
|
33 //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
|
34 |
068cf45c3f1b
added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
35 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
|
36 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
|
37 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
|
38 while (frame) { |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
39 frameNum = ::goToFrameNum(inputVideo, frameNum, frameNum+1000); |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
10
diff
changeset
|
40 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
|
41 |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
10
diff
changeset
|
42 cout << frameNum << " " << seconds << endl; |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
43 } |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
44 } |
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
|
45 |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
46 int main(int argc, char *argv[]) { |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
47 //cout << "Hello World" << endl; |
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 CvCapture *inputVideo = 0; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
50 if (argc == 1) |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
51 inputVideo = cvCreateCameraCapture(-1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
52 else |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
53 inputVideo = cvCaptureFromFile(argv[1]); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
54 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
55 int frameNum = 0; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
56 cvNamedWindow("Optical Flow", CV_WINDOW_AUTOSIZE); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
57 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
58 // allocate space for pyramids |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
59 IplImage* frame = cvQueryFrame(inputVideo); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
60 CvSize frameSize = cvSize(frame->width, frame->height); |
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 IplImage* frame1_1C = ::allocateImage(frameSize, IPL_DEPTH_8U, 1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
63 cvConvertImage(frame, frame1_1C); |
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 IplImage *frame1 = ::allocateImage(frameSize, IPL_DEPTH_8U, 3); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
66 cvConvertImage(frame, frame1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
67 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
68 frame = cvQueryFrame(inputVideo); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
69 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
70 IplImage* frame2_1C = ::allocateImage(frameSize, IPL_DEPTH_8U, 1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
71 cvConvertImage(frame, frame2_1C); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
72 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
73 IplImage *eig_image = ::allocateImage(frameSize, IPL_DEPTH_32F, 1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
74 IplImage *temp_image = ::allocateImage(frameSize, IPL_DEPTH_32F, 1); |
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 int nFeatures = 1000; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
77 CvPoint2D32f frame1_features[1000]; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
78 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
79 CvPoint2D32f frame2_features[1000]; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
80 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
81 char optical_flow_found_feature[1000]; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
82 float optical_flow_feature_error[1000]; |
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 CvSize optical_flow_window = cvSize(3,3); |
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 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
|
87 IplImage* pyramid1 = ::allocateImage(frameSize, IPL_DEPTH_8U, 1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
88 IplImage* pyramid2 = ::allocateImage(frameSize, IPL_DEPTH_8U, 1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
89 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
90 int pressedKey = '?'; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
91 while (frame && !::interruptionKey(pressedKey)) { |
15 | 92 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
|
93 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
|
94 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
95 for(int i = 0; i < nFeatures; i++) { |
15 | 96 if (optical_flow_found_feature[i] != 0) { |
97 int line_thickness = 1; | |
98 CvScalar line_color = CV_RGB(255,0,0); | |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
99 |
15 | 100 CvPoint p,q; |
101 p.x = lrintf(frame1_features[i].x); | |
102 p.y = lrintf(frame1_features[i].y); | |
103 q.x = lrintf(frame2_features[i].x); | |
104 q.y = lrintf(frame2_features[i].y); | |
105 | |
106 double dx = frame2_features[i].x-frame1_features[i].x; | |
107 double dy = frame2_features[i].y-frame1_features[i].y; | |
108 | |
109 double angle = atan2( (double) p.y - q.y, (double) p.x - q.x ); | |
110 double dist = sqrt(square(dx) + square(dy)); | |
111 | |
112 if (dist > 2.) { // min motion distance | |
113 q.x = lrintf(frame1_features[i].x+dx*3); //(p.x - 3 * hypotenuse * cos(angle)); | |
114 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
|
115 |
15 | 116 /* Now we draw the main line of the arrow. */ |
117 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
118 /* Now draw the tips of the arrow. I do some scaling so that the | |
119 * tips look proportional to the main line of the arrow. | |
120 */ | |
121 float arrowSize = 6; | |
122 p.x = lrintf(q.x + arrowSize * cos(angle + pi / 4)); | |
123 p.y = lrintf(q.y + arrowSize * sin(angle + pi / 4)); | |
124 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
125 p.x = lrintf(q.x + arrowSize * cos(angle - pi / 4)); | |
126 p.y = lrintf(q.y + arrowSize * sin(angle - pi / 4)); | |
127 cvLine( frame1, p, q, line_color, line_thickness, CV_AA, 0 ); | |
128 } | |
129 } | |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
130 } |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
131 cvShowImage("Optical Flow", frame1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
132 pressedKey = cvWaitKey(5); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
133 frame = cvQueryFrame(inputVideo); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
134 frameNum++; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
135 cout << frameNum << endl; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
136 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
137 cvCopy(frame2_1C, frame1_1C); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
138 cvCopy(pyramid2, pyramid1); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
139 cvConvertImage(frame, frame2_1C); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
140 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
|
141 } |
068cf45c3f1b
added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
142 |
068cf45c3f1b
added optical flow skeleton (test to reach a frame with cvqueryframe (fast for small resolutions)
Nicolas Saunier <nico@confins.net>
parents:
diff
changeset
|
143 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
|
144 } |