-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathmain.cpp
81 lines (71 loc) · 2.5 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include <opencv2/opencv.hpp>
#include "TimeMeasurement.h"
#include "Line.h"
#include "CourtLinePixelDetector.h"
#include "CourtLineCandidateDetector.h"
#include "TennisCourtFitter.h"
#include "DebugHelpers.h"
using namespace cv;
int main(int argc, char** argv)
{
TimeMeasurement::debug = false;
CourtLinePixelDetector::debug = false;
CourtLineCandidateDetector::debug = false;
TennisCourtFitter::debug = false;
if (argc < 2 || argc > 3)
{
std::cout << "Usage: ./detect video_path [output_path]" << std::endl;
std::cout << " video_path: path to an input avi file." << std::endl;
std::cout << " output_path: path to an output file where the xy court point coordinates will be written." << std::endl;
std::cout << " This argument is optional. If not present, then a window with the result will be opened." << std::endl;
return -1;
}
std::string filename(argv[1]);
std::cout << "Reading file " << filename << std::endl;
VideoCapture vc(filename);
if (!vc.isOpened())
{
std::cerr << "Cannot open file " << filename << std::endl;
return 1;
}
printVideoInfo(vc);
Mat frame;
int frameIndex = int(vc.get(CV_CAP_PROP_FRAME_COUNT)) / 2;
vc.set(CV_CAP_PROP_POS_FRAMES, frameIndex);
if (!vc.read(frame))
{
std::cerr << "Failed to read frame with index " << frameIndex << std::endl;
return 2;
}
std::cout << "Reading frame with index " << frameIndex << std::endl;
CourtLinePixelDetector courtLinePixelDetector;
CourtLineCandidateDetector courtLineCandidateDetector;
TennisCourtFitter tennisCourtFitter;
std::cout << "Starting court line detection algorithm..." << std::endl;
try
{
TimeMeasurement::start("LineDetection");
Mat binaryImage = courtLinePixelDetector.run(frame);
std::vector<Line> candidateLines = courtLineCandidateDetector.run(binaryImage, frame);
TennisCourtModel model = tennisCourtFitter.run(candidateLines, binaryImage, frame);
int elapsed_seconds = TimeMeasurement::stop("LineDetection");
std::cout << "Elapsed time: " << elapsed_seconds << "s." << std::endl;
if (argc == 2)
{
model.drawModel(frame);
displayImage("Result - press key to exit", frame);
}
else if (argc == 3)
{
std::string outFilename(argv[2]);
model.writeToFile(outFilename);
std::cout << "Result written to " << outFilename << std::endl;
}
}
catch (std::runtime_error& e)
{
std::cout << "Processing error: " << e.what() << std::endl;
return 3;
}
return 0;
}