Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
3 | PointedEar | 1 | /*------------------------------------------------------------------------------------------*\ |
2 | This file contains material supporting chapter 10 of the cookbook: |
||
3 | Computer Vision Programming using the OpenCV Library. |
||
4 | by Robert Laganiere, Packt Publishing, 2011. |
||
5 | |||
6 | This program is free software; permission is hereby granted to use, copy, modify, |
||
7 | and distribute this source code, or portions thereof, for any purpose, without fee, |
||
8 | subject to the restriction that the copyright notice may not be removed |
||
9 | or altered from any source or altered source distribution. |
||
10 | The software is released on an as-is basis and without any warranties of any kind. |
||
11 | In particular, the software is not guaranteed to be fault-tolerant or free from failure. |
||
12 | The author disclaims all warranties with regard to this software, any use, |
||
13 | and any consequent failure, is purely the responsibility of the user. |
||
14 | |||
15 | Copyright (C) 2010-2011 Robert Laganiere, www.laganiere.name |
||
16 | \*------------------------------------------------------------------------------------------*/ |
||
17 | |||
18 | #include <iostream> |
||
19 | #include <opencv2/core/core.hpp> |
||
20 | #include <opencv2/highgui/highgui.hpp> |
||
21 | #include <opencv2/imgproc/imgproc.hpp> |
||
22 | #include <opencv2/features2d/features2d.hpp> |
||
23 | #include <opencv2/video/tracking.hpp> |
||
24 | |||
25 | #include "featuretracker.h" |
||
26 | |||
27 | int main() |
||
28 | { |
||
29 | // Create video procesor instance |
||
30 | VideoProcessor processor; |
||
31 | |||
32 | // Create feature tracker instance |
||
33 | FeatureTracker tracker; |
||
34 | |||
35 | // Open video file |
||
36 | processor.setInput("../bike.avi"); |
||
37 | |||
38 | // set frame processor |
||
39 | processor.setFrameProcessor(&tracker); |
||
40 | |||
41 | // Declare a window to display the video |
||
42 | processor.displayOutput("Tracked Features"); |
||
43 | |||
44 | // Play the video at the original frame rate |
||
45 | processor.setDelay(1000./processor.getFrameRate()); |
||
46 | |||
47 | // Start the process |
||
48 | processor.run(); |
||
49 | |||
50 | cv::waitKey(); |
||
51 | /* |
||
52 | // Open the video file |
||
53 | cv::VideoCapture capture("../bike.avi"); |
||
54 | // check if video successfully opened |
||
55 | if (!capture.isOpened()) |
||
56 | return 1; |
||
57 | |||
58 | // Get the frame rate |
||
59 | double rate= capture.get(CV_CAP_PROP_FPS); |
||
60 | |||
61 | bool stop(false); |
||
62 | cv::Mat frame; // current video frame |
||
63 | cv::namedWindow("Extracted Frame"); |
||
64 | |||
65 | // Delay between each frame |
||
66 | // corresponds to video frame rate |
||
67 | int delay= 1000/rate/2; |
||
68 | |||
69 | |||
70 | |||
71 | cv::Mat gray,gray_prev; |
||
72 | std::vector<cv::Point2f> points[2]; |
||
73 | std::vector<cv::Point2f> features; |
||
74 | const int MAX_COUNT = 500; |
||
75 | cv::Size winSize(10,10); |
||
76 | cv::TermCriteria termcrit(CV_TERMCRIT_ITER|CV_TERMCRIT_EPS,20,0.03); |
||
77 | bool firstframe =true; |
||
78 | |||
79 | |||
80 | |||
81 | // for all frames in video |
||
82 | while (!stop) { |
||
83 | |||
84 | // read next frame if any |
||
85 | if (!capture.read(frame)) |
||
86 | break; |
||
87 | |||
88 | |||
89 | cv::cvtColor(frame, gray, CV_BGR2GRAY); |
||
90 | if(firstframe) |
||
91 | { |
||
92 | cv::goodFeaturesToTrack(gray, features, MAX_COUNT, 0.01, 10, cv::Mat(), 3, 0, 0.04); |
||
93 | cv::cornerSubPix(gray, features, winSize, cv::Size(-1,-1), termcrit); |
||
94 | points[0].insert(points[0].end(),features.begin(),features.end()); |
||
95 | firstframe=false; |
||
96 | } |
||
97 | |||
98 | { |
||
99 | std::vector<uchar> status; |
||
100 | std::vector<float> err; |
||
101 | if(gray_prev.empty()) |
||
102 | gray.copyTo(gray_prev); |
||
103 | cv::calcOpticalFlowPyrLK(gray_prev, gray, points[0], points[1], status, err, winSize,3, termcrit, 0); |
||
104 | size_t i, k; |
||
105 | for( i = k = 0; i < points[1].size(); i++ ) |
||
106 | { |
||
107 | if( !status[i] ) |
||
108 | continue; |
||
109 | if ((abs(points[0][i].x-points[1][i].x)+(abs(points[0][i].y-points[1][i].y))<2)) |
||
110 | continue; |
||
111 | |||
112 | cv::line( frame, points[0][i],points[1][i], cv::Scalar(0,255,0)); |
||
113 | points[1][k++] = points[1][i]; |
||
114 | cv::circle( frame, points[1][i], 3, cv::Scalar(0,255,0), -1, 8); |
||
115 | } |
||
116 | points[1].resize(k); |
||
117 | } |
||
118 | if(points[1].size()<=10) |
||
119 | firstframe=true; |
||
120 | std::swap(points[1], points[0]); |
||
121 | cv::swap(gray_prev, gray); |
||
122 | |||
123 | |||
124 | cv::imshow("Extracted Frame",frame); |
||
125 | |||
126 | // introduce a delay |
||
127 | // or press key to stop |
||
128 | if (cv::waitKey(delay)>=0) |
||
129 | stop= true; |
||
130 | } |
||
131 | |||
132 | // Close the video file |
||
133 | capture.release(); |
||
134 | |||
135 | cv::waitKey(); |
||
136 | |||
137 | |||
138 | // Create instance |
||
139 | VideoProcessor processor; |
||
140 | // Open video file |
||
141 | processor.setInput("../bike.avi"); |
||
142 | // Output filename |
||
143 | // processor.setOutput("../output/bikeOut",".jpg"); |
||
144 | |||
145 | // Declare a window to display the video |
||
146 | processor.displayInput("Current Frame"); |
||
147 | processor.displayOutput("Output Frame"); |
||
148 | |||
149 | // Play the video at the original frame rate |
||
150 | processor.setDelay(1000./processor.getFrameRate()); |
||
151 | |||
152 | // Set the frame processor callback function |
||
153 | processor.setFrameProcessor(draw); |
||
154 | |||
155 | // Start the process |
||
156 | processor.run(); |
||
157 | |||
158 | cv::waitKey(5000);*/ |
||
159 | } |