-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathSolvePNP.cxx
120 lines (83 loc) · 2.75 KB
/
SolvePNP.cxx
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <string>
std::vector<cv::Point2f> Generate2DPoints();
std::vector<cv::Point3f> Generate3DPoints();
int main( int argc, char* argv[])
{
// Read points
std::vector<cv::Point2f> imagePoints = Generate2DPoints();
std::vector<cv::Point3f> objectPoints = Generate3DPoints();
std::cout << "There are " << imagePoints.size() << " imagePoints and " << objectPoints.size() << " objectPoints." << std::endl;
cv::Mat cameraMatrix(3,3,cv::DataType<double>::type);
cv::setIdentity(cameraMatrix);
std::cout << "Initial cameraMatrix: " << cameraMatrix << std::endl;
cv::Mat distCoeffs(4,1,cv::DataType<double>::type);
distCoeffs.at<double>(0) = 0;
distCoeffs.at<double>(1) = 0;
distCoeffs.at<double>(2) = 0;
distCoeffs.at<double>(3) = 0;
cv::Mat rvec(3,1,cv::DataType<double>::type);
cv::Mat tvec(3,1,cv::DataType<double>::type);
cv::solvePnP(objectPoints, imagePoints, cameraMatrix, distCoeffs, rvec, tvec);
std::cout << "rvec: " << rvec << std::endl;
std::cout << "tvec: " << tvec << std::endl;
std::vector<cv::Point2f> projectedPoints;
cv::projectPoints(objectPoints, rvec, tvec, cameraMatrix, distCoeffs, projectedPoints);
for(unsigned int i = 0; i < projectedPoints.size(); ++i)
{
std::cout << "Image point: " << imagePoints[i] << " Projected to " << projectedPoints[i] << std::endl;
}
return 0;
}
std::vector<cv::Point2f> Generate2DPoints()
{
std::vector<cv::Point2f> points;
float x,y;
x=282;y=274;
points.push_back(cv::Point2f(x,y));
x=397;y=227;
points.push_back(cv::Point2f(x,y));
x=577;y=271;
points.push_back(cv::Point2f(x,y));
x=462;y=318;
points.push_back(cv::Point2f(x,y));
x=270;y=479;
points.push_back(cv::Point2f(x,y));
x=450;y=523;
points.push_back(cv::Point2f(x,y));
x=566;y=475;
points.push_back(cv::Point2f(x,y));
for(unsigned int i = 0; i < points.size(); ++i)
{
std::cout << points[i] << std::endl;
}
return points;
}
std::vector<cv::Point3f> Generate3DPoints()
{
std::vector<cv::Point3f> points;
float x,y,z;
x=.5;y=.5;z=-.5;
points.push_back(cv::Point3f(x,y,z));
x=.5;y=.5;z=.5;
points.push_back(cv::Point3f(x,y,z));
x=-.5;y=.5;z=.5;
points.push_back(cv::Point3f(x,y,z));
x=-.5;y=.5;z=-.5;
points.push_back(cv::Point3f(x,y,z));
x=.5;y=-.5;z=-.5;
points.push_back(cv::Point3f(x,y,z));
x=-.5;y=-.5;z=-.5;
points.push_back(cv::Point3f(x,y,z));
x=-.5;y=-.5;z=.5;
points.push_back(cv::Point3f(x,y,z));
for(unsigned int i = 0; i < points.size(); ++i)
{
std::cout << points[i] << std::endl;
}
return points;
}