/
groundplane.cpp
229 lines (201 loc) · 5.97 KB
/
groundplane.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
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
#include "groundplane.h"
GroundPlane::GroundPlane()
{
readCameraMatrix();
eachDis=100;//centimetre
size1=2;//number in 1 line
size2=3;//number of lines
}
void GroundPlane::readCameraMatrix()
{
cout << endl << "Reading: " << endl;
FileStorage fs;
if (fs.open("out_camera_data.xml", FileStorage::READ)){
fs["Camera_Matrix"]>>cm;
fs["Distortion_Coefficients"] >> dc;
fs.release();
}else{
cout<<"cannot read file camera matrix"<<endl;
}
}
bool GroundPlane::readCameraMatrix(const string filename)
{
cout << endl << "Reading: "<<filename << endl;
FileStorage fs;
if (fs.open(filename, FileStorage::READ)){
fs["Camera_Matrix"]>>cm;
fs["Distortion_Coefficients"] >> dc;
fs.release();
return true;
}else{
cout<<"cannot read file camera matrix"<<endl;
return false;
}
}
vector<Point2f> GroundPlane::findCircle(Mat im)
{
Size patternsize(size1,size2); //interior number of corners
SimpleBlobDetector::Params params;
params.minArea = 50;
params.minDistBetweenBlobs = 5;
Ptr<SimpleBlobDetector> blobDetector = SimpleBlobDetector::create(params);
Mat gray;
cv::cvtColor(im,gray, CV_BGR2GRAY);
bool patternfound = cv::findCirclesGrid(gray, patternsize, corners,CALIB_CB_SYMMETRIC_GRID + CALIB_CB_CLUSTERING);//, blobDetector);
cout<<corners<<endl;
drawChessboardCorners(im, patternsize, Mat(corners), patternfound);
return corners;
}
void GroundPlane::calPlaneEquation()
{
//--prepare data
//read camera parameter
readCameraMatrix();
//prepare object cordinate
vector<Point3f> corner3d;
for (int i=0;i<size2;i++){
for(int j=size1-1;j>=0;j--){
corner3d.push_back(Point3f(i*eachDis,j*eachDis,0));
}
}
//solve equation to cameracordinate
solvePnP(corner3d,corners,cm,dc,rvec,tvec,false);
//prepare equation of T(rvec+tvec)
cv::Mat R;
cv::Rodrigues(rvec, R);
cv::Mat T(4, 4, R.type()); // T is 4x4
T( cv::Range(0,3), cv::Range(0,3) ) = R * 1; // copies R into T
T.row(3)=0;
T( cv::Range(0,3), cv::Range(3,4) )=tvec*1;
T.at<double>(3,3)=1;
cout<<"R"<<R<<endl;
cout<<"TV"<<tvec<<endl;
cout<<"T"<<T<<endl;
//--prepare to build groundplane
//prepare 3 point on ground (object point to camera point [calculate by rvec,tvec])
//3point angle of L
Mat testP1 = (Mat_<double>(4,1) << 0, 0, 0,1);
Mat testP2 = (Mat_<double>(4,1) << 0, eachDis, 0,1);
Mat testP3 = (Mat_<double>(4,1) << eachDis, eachDis,0,1);
Mat resu1=T*testP1;
Mat resu2=T*testP2;
Mat resu3=T*testP3;
cout<<"result1="<<resu1<<endl;
cout<<"result2="<<resu2<<endl;
cout<<"result3="<<resu3<<endl;
//--calculate groundplane
vector<double> temp12,temp23,tempN;
//temp12 vector 1 to 2
temp12.push_back(resu2.at<double>(0)-resu1.at<double>(0));
temp12.push_back(resu2.at<double>(1)-resu1.at<double>(1));
temp12.push_back(resu2.at<double>(2)-resu1.at<double>(2));
//temp23 vector 2 to 3
temp23.push_back(resu3.at<double>(0)-resu2.at<double>(0));
temp23.push_back(resu3.at<double>(1)-resu2.at<double>(1));
temp23.push_back(resu3.at<double>(2)-resu2.at<double>(2));
//det find norm vec
tempN.push_back(temp12[1]*temp23[2]-temp23[1]*temp12[2]);
tempN.push_back(temp12[2]*temp23[0]-temp23[2]*temp12[0]);
tempN.push_back(temp12[0]*temp23[1]-temp23[0]*temp12[1]);
//--result show as ax+by+cz+d=0
a=tempN[0];
b=tempN[1];
c=tempN[2];
d=tempN[0]*(resu2.at<double>(0))+tempN[1]*(resu2.at<double>(1))+tempN[2]*(resu2.at<double>(2));
cout<<"a="<<a<<endl;
cout<<"b="<<b<<endl;
cout<<"c="<<c<<endl;
cout<<"d="<<d<<endl;
}
Point3d GroundPlane::imgCord2CameraCord(Point2d imgCord)
{
Mat ttt1 = (Mat_<double>(3,1) << imgCord.x, imgCord.y,1);
ttt1=cm.inv()*ttt1;
return Point3d(ttt1.at<double>(0),ttt1.at<double>(1),ttt1.at<double>(2));
}
Point3d GroundPlane::getPointAtGround(Point2d imgCord)
{
Mat ttt1 = (Mat_<double>(3,1) << imgCord.x, imgCord.y,1);
ttt1=cm.inv()*ttt1;
double x=d/(a*ttt1.at<double>(0)+b*ttt1.at<double>(1)+c*ttt1.at<double>(2))*ttt1.at<double>(0);
double y=d/(a*ttt1.at<double>(0)+b*ttt1.at<double>(1)+c*ttt1.at<double>(2))*ttt1.at<double>(1);
double z=d/(a*ttt1.at<double>(0)+b*ttt1.at<double>(1)+c*ttt1.at<double>(2));
return Point3d(x,y,z);
}
double GroundPlane::calDis3D(Point3d a,Point3d b)
{
return norm(a-b);
}
double GroundPlane::calDis3DFrom2D(Point2d a,Point2d b)
{
return norm(getPointAtGround(a)-getPointAtGround(b));
}
bool GroundPlane::saveGroundParam()
{
FileStorage fs("ground.xml", FileStorage::WRITE );
fs<<"a"<<a;
fs<<"b"<<b;
fs<<"c"<<c;
fs<<"d"<<d;
fs.release();
return true;
}
bool GroundPlane::saveGroundParam(const string x)
{
FileStorage fs(x, FileStorage::WRITE );
fs<<"a"<<a;
fs<<"b"<<b;
fs<<"c"<<c;
fs<<"d"<<d;
fs.release();
return true;
}
bool GroundPlane::readGroundParam()
{
FileStorage fs;
if (fs.open("ground.xml", FileStorage::READ)){
fs["a"]>>a;
fs["b"]>>b;
fs["c"]>>c;
fs["d"]>>d;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
fs.release();
return true;
}else{
cout<<"cannot read ground plane"<<endl;
return false;
}
}
bool GroundPlane::readGroundParam(const string x)
{
FileStorage fs;
if (fs.open(x, FileStorage::READ)){
fs["a"]>>a;
fs["b"]>>b;
fs["c"]>>c;
fs["d"]>>d;
cout<<a<<endl;
cout<<b<<endl;
cout<<c<<endl;
cout<<d<<endl;
fs.release();
return true;
}else{
cout<<"cannot read ground plane"<<endl;
return false;
}
}
bool GroundPlane::writeSetting(const string x)
{
FileStorage fs(x, FileStorage::WRITE );
fs<<"Camera_Matrix"<<cm;
fs<<"distortion_coefficients"<<dc;
fs<<"a"<<a;
fs<<"b"<<b;
fs<<"c"<<c;
fs<<"d"<<d;
fs.release();
}