Mercurial Hosting > traffic-intelligence
annotate c/cvutils.cpp @ 144:b32947b002da
added the code to read matrices from text files
author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
---|---|
date | Fri, 26 Aug 2011 19:38:11 -0400 |
parents | 3a11dba30655 |
children | 0089fb29cd26 |
rev | line source |
---|---|
9 | 1 #include "cvutils.hpp" |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
2 #include "utils.hpp" |
9 | 3 |
131 | 4 #include "opencv2/core/core.hpp" |
126
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
5 #include "opencv2/highgui/highgui.hpp" |
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
6 #include "opencv2/features2d/features2d.hpp" |
9 | 7 |
8 #include <iostream> | |
126
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
9 #include <vector> |
9 | 10 |
11 using namespace std; | |
126
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
12 using namespace cv; |
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
13 |
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
14 cv::Mat loadMat(const string& filename, const string& separator) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
15 vector<vector<float> > numbers = ::loadNumbers(filename, separator); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
16 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
17 Mat mat; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
18 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
19 if (!numbers.empty()) { |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
20 mat = Mat(numbers.size(),numbers[0].size(), CV_32FC1); |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
21 for (unsigned int i=0; i<numbers.size(); i++) |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
22 for (unsigned int j=0; j<numbers[0].size(); j++) |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
23 mat.at<float>(i,j) = numbers[i][j]; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
24 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
25 |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
26 return mat; |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
27 } |
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
28 |
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
29 void keyPoints2Points(const vector<KeyPoint>& kpts, vector<Point2f>& pts, const bool& clearPts /* = true */) { |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
30 if (clearPts) |
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
31 pts.clear(); |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
126
diff
changeset
|
32 pts.reserve(kpts.size()); |
126
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
33 |
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
34 for (unsigned int i=0; i<kpts.size(); i++) |
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
126
diff
changeset
|
35 pts.push_back(kpts[i].pt); |
126
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
36 } |
9 | 37 |
38 IplImage* allocateImage(const int& width, const int& height, const int& depth, const int& channels) { return ::allocateImage(cvSize(width, height), depth, channels);} | |
39 | |
40 IplImage* allocateImage(const CvSize& size, const int& depth, const int& channels) { | |
41 IplImage* image = cvCreateImage(size, depth, channels); | |
42 | |
43 if (!image) { | |
44 cerr << "Error: Couldn't allocate image. Out of memory?\n" << endl; | |
45 exit(-1); | |
46 } | |
47 | |
48 return image; | |
49 } | |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
50 |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
51 int goToFrameNum(CvCapture* inputVideo, const int& currentFrameNum, const int& targetFrameNum) { |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
52 int frameNum = currentFrameNum; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
53 if (currentFrameNum > targetFrameNum) { |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
54 cerr << "Current frame number " << currentFrameNum << " is after the target frame number " << targetFrameNum << "." << endl; |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
55 } else if (currentFrameNum < targetFrameNum) { |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
56 IplImage* frame = cvQueryFrame(inputVideo); |
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
57 frameNum++; |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
58 while (frame && frameNum<targetFrameNum) { |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
59 frame = cvQueryFrame(inputVideo); |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
60 frameNum++; |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
61 } |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
62 } |
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
63 |
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
64 return frameNum; |
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
65 } |
131 | 66 |
67 const Scalar Colors::color[] = {Colors::red(), | |
68 Colors::green(), | |
69 Colors::blue(), | |
70 Colors::cyan(), | |
71 Colors::magenta(), | |
72 Colors::yellow(), | |
73 Colors::white(), | |
74 Colors::black()}; | |
75 | |
76 Scalar Colors::black(void) { return Scalar(0,0,0);} | |
77 Scalar Colors::red(void) { return Scalar(255,0,0);} | |
78 Scalar Colors::green(void) { return Scalar(0,255,0);} | |
79 Scalar Colors::blue(void) { return Scalar(0,0,255);} | |
80 Scalar Colors::white(void) { return Scalar(255,255,255);} | |
81 Scalar Colors::magenta(void) { return Scalar(255,0,255);} | |
82 Scalar Colors::cyan(void) { return Scalar(0,255,255);} | |
83 Scalar Colors::yellow(void) { return Scalar(255,255,0);} | |
84 | |
85 Scalar Colors::color3(const int& num) { return Colors::color[num%3];} | |
86 Scalar Colors::color8(const int& num) { return Colors::color[num%Colors::nColors];} |