Mercurial > hg > nsaunier > traffic-intelligence
annotate c/cvutils.cpp @ 999:8bcac18c2b55 OpenCV3 and Python3
Removed tag OpenCV 3.1
| author | Nicolas Saunier <nicolas.saunier@polymtl.ca> |
|---|---|
| date | Sun, 27 May 2018 23:23:45 -0400 |
| parents | 8ac7f61c6e4f |
| children | 05ccd8ef150c |
| 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 |
|
147
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
14 Point2f project(const Point2f& p, const Mat& homography) { |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
15 //Mat homogeneous(3, 1, CV_32FC1); |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
16 float x, y; |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
17 float w = homography.at<float>(2,0)*p.x+homography.at<float>(2,1)*p.y+homography.at<float>(2,2); |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
18 if (w != 0) { |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
19 x = (homography.at<float>(0,0)*p.x+homography.at<float>(0,1)*p.y+homography.at<float>(0,2))/w; |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
20 y = (homography.at<float>(1,0)*p.x+homography.at<float>(1,1)*p.y+homography.at<float>(1,2))/w; |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
21 } else { |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
22 x = 0; |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
23 y = 0; |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
24 } |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
25 return Point2f(x, y); |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
26 } |
|
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
27 |
|
933
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
28 Point2f cameraProject(const Point2f& p, const Mat& cameraMatrix) { |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
29 //Mat homogeneous(3, 1, CV_32FC1); |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
30 float x, y; |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
31 x = cameraMatrix.at<double>(0,0)*p.x+cameraMatrix.at<double>(0,2); |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
32 y = cameraMatrix.at<double>(1,1)*p.y+cameraMatrix.at<double>(1,2); |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
33 return Point2f(x, y); |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
34 } |
|
8ac7f61c6e4f
major rework of homography calibration, no in ideal points if correcting for distortion
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
427
diff
changeset
|
35 |
|
147
0089fb29cd26
added projection of points and reprojection for display
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
144
diff
changeset
|
36 Mat loadMat(const string& filename, const string& separator) { |
|
144
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
37 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
|
38 |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
39 Mat mat; |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
40 |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
41 if (!numbers.empty()) { |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
42 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
|
43 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
|
44 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
|
45 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
|
46 } |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
47 |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
48 return mat; |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
49 } |
|
b32947b002da
added the code to read matrices from text files
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
131
diff
changeset
|
50 |
|
128
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
51 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
|
52 if (clearPts) |
|
536510f60854
new features generated as needed
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
127
diff
changeset
|
53 pts.clear(); |
|
127
d19d6e63dd77
simple feature tracking and drawing working
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
126
diff
changeset
|
54 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
|
55 |
|
336926453b28
added conversion function from keypoint vector to point vector and cleaned headers
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
12
diff
changeset
|
56 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
|
57 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
|
58 } |
| 9 | 59 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
60 // IplImage* allocateImage(const int& width, const int& height, const int& depth, const int& channels) { return ::allocateImage(cvSize(width, height), depth, channels);} |
| 9 | 61 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
62 // IplImage* allocateImage(const CvSize& size, const int& depth, const int& channels) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
63 // IplImage* image = cvCreateImage(size, depth, channels); |
| 9 | 64 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
65 // if (!image) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
66 // cerr << "Error: Couldn't allocate image. Out of memory?\n" << endl; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
67 // exit(-1); |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
68 // } |
| 9 | 69 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
70 // return image; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
71 // } |
|
11
e77e2fd69b02
modularized code (not compiling)
Nicolas Saunier <nico@confins.net>
parents:
9
diff
changeset
|
72 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
73 // int goToFrameNum(CvCapture* inputVideo, const int& currentFrameNum, const int& targetFrameNum) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
74 // int frameNum = currentFrameNum; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
75 // if (currentFrameNum > targetFrameNum) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
76 // cerr << "Current frame number " << currentFrameNum << " is after the target frame number " << targetFrameNum << "." << endl; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
77 // } else if (currentFrameNum < targetFrameNum) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
78 // IplImage* frame = cvQueryFrame(inputVideo); |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
79 // frameNum++; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
80 // while (frame && frameNum<targetFrameNum) { |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
81 // frame = cvQueryFrame(inputVideo); |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
82 // frameNum++; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
83 // } |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
84 // } |
|
12
ff5403319cec
optical flow demo working
Nicolas Saunier <nico@confins.net>
parents:
11
diff
changeset
|
85 |
|
407
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
86 // return frameNum; |
|
5eeb3b9fb568
commented problem code (opencv 2.4.6)
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
147
diff
changeset
|
87 // } |
| 131 | 88 |
| 89 const Scalar Colors::color[] = {Colors::red(), | |
| 90 Colors::green(), | |
| 91 Colors::blue(), | |
| 92 Colors::cyan(), | |
| 93 Colors::magenta(), | |
| 94 Colors::yellow(), | |
| 95 Colors::white(), | |
| 96 Colors::black()}; | |
| 97 | |
|
427
1e3c7fe21a38
changed colors to 3.0 version
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
407
diff
changeset
|
98 // Blue, Green, Red |
| 131 | 99 Scalar Colors::black(void) { return Scalar(0,0,0);} |
|
427
1e3c7fe21a38
changed colors to 3.0 version
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
407
diff
changeset
|
100 Scalar Colors::blue(void) { return Scalar(255,0,0);} |
| 131 | 101 Scalar Colors::green(void) { return Scalar(0,255,0);} |
|
427
1e3c7fe21a38
changed colors to 3.0 version
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
407
diff
changeset
|
102 Scalar Colors::red(void) { return Scalar(0,0,255);} |
| 131 | 103 Scalar Colors::white(void) { return Scalar(255,255,255);} |
| 104 Scalar Colors::magenta(void) { return Scalar(255,0,255);} | |
|
427
1e3c7fe21a38
changed colors to 3.0 version
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
407
diff
changeset
|
105 Scalar Colors::yellow(void) { return Scalar(0,255,255);} |
|
1e3c7fe21a38
changed colors to 3.0 version
Nicolas Saunier <nicolas.saunier@polymtl.ca>
parents:
407
diff
changeset
|
106 Scalar Colors::cyan(void) { return Scalar(255,255,0);} |
| 131 | 107 |
| 108 Scalar Colors::color3(const int& num) { return Colors::color[num%3];} | |
| 109 Scalar Colors::color8(const int& num) { return Colors::color[num%Colors::nColors];} |
