forked from LabProdam/ContadorDeCiclistas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCamera.cpp
146 lines (129 loc) · 3.67 KB
/
Camera.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
#include <math.h>
#include <fstream>
#include <iostream>
#include "Camera.hpp"
#include "CoordTransform.hpp"
#define PI (4 * atan(tan(1.)))
Camera::Camera() {}
Camera::Camera(std::string confFileName) {
std::ifstream confFile;
confFile.open(confFileName);
if(!confFile.is_open()) {
std::cout << "Could not load camera configuration file: " <<
confFileName << std::endl;
return;
}
confFile >> this->frameRows;
confFile >> this->frameCols;
confFile >> this->theta;
confFile >> this->cosTheta;
confFile >> this->sinTheta;
confFile >> this->focalDistance;
confFile >> this->height;
confFile >> this->Rz;
confFile.close();
}
Camera::~Camera() {}
void Camera::SetFrameRows(int rows) {
this->frameRows = rows;
}
int Camera::GetFrameRows(void) {
return this->frameRows;
}
void Camera::SetFrameCols(int cols) {
this->frameCols = cols;
}
int Camera::GetFrameCols(void) {
return this->frameCols;
}
void Camera::SetFocalDistance(float d, float h, cv::Point2f P1,
cv::Point2f P2) {
this->focalDistance = d * norm(P1 - P2) / h; //point - point is a vector
}
float Camera::GetFocalDistance(void) {
return this->focalDistance;
}
void Camera::SetHeight(float lr, cv::Point2f P1, cv::Point2f P2) {
this->height = this->focalDistance * lr / norm(P1 - P2);
}
float Camera::GetHeight(void) {
return this->height;
}
//it does not matter if SetTheta receives P1 and P2 crossing
// y = cam.frame.rows / 2
void Camera::SetTheta(float l, cv::Point2f P1, cv::Point2f P2, float h) {
double k, error, error2, theta, theta_increment;
double fd = this->focalDistance;
int i;
P1.y = CoordTransform::OpencvYCoord2PaperYCoord(*this, P1);
P2.y = CoordTransform::OpencvYCoord2PaperYCoord(*this, P2);
theta = 0.;
theta_increment = PI / 180.;
error2 = 0.;
k = fabs(l / (h - this->height));
for(i = 0, error = DBL_MAX;
(error > 10e-10) && (fabs(theta) <= PI / 2.) && (i < 1000);
i++) {
error2 = (fd*cos(theta) + P1.y*sin(theta)) /
(fd*sin(theta) - P1.y*cos(theta));
error2 -= (fd*cos(theta) + P2.y*sin(theta)) /
(fd*sin(theta) - P2.y*cos(theta));
error2 = fabs(error2);
error2 = fabs(k - error2);
// std::cout << "theta = " << theta*180./PI << "\ti = " << i <<
// " error = " << error << " error2 =" << error2 << std::endl;
if(error2 > error) {
theta_increment *= -.5;
// std::cout << "\t" << theta_increment*180/PI <<"°"<< std::endl;
}
theta += theta_increment;
error = error2;
}
if(theta < 0.) {
std::cout << "theta = " << theta << " < 0." << std::endl <<
"Aborting." << std::endl;
exit(EXIT_FAILURE);
}
this->theta = theta;
this->cosTheta = cos(theta);
this->sinTheta = sin(theta);
}
float Camera::GetTheta(void) {
return this->theta;
}
float Camera::GetCosTheta(void) {
return this->cosTheta;
}
float Camera::GetSinTheta(void) {
return this->sinTheta;
}
void Camera::SetRz(cv::Point2f P, float h) {
float Rz;
P.y = CoordTransform::OpencvYCoord2PaperYCoord(*this, P);
Rz = (this->focalDistance * this->cosTheta + P.y * this->sinTheta);
Rz /= (this->focalDistance * this->sinTheta - P.y * this->cosTheta);
Rz *= (this->height - h);
this->Rz = Rz;
}
float Camera::GetRz(void) {
return this->Rz;
}
void Camera::SaveConf(std::string confFileName) {
std::ofstream confFile;
confFile.open(confFileName, std::ios::trunc);
if(!confFile.is_open()) {
std::cout << "Could not save camera configuration file: " <<
confFileName<< std::endl;
exit(EXIT_FAILURE);
}
confFile <<
this->frameRows << std::endl <<
this->frameCols << std::endl <<
this->theta << std::endl <<
this->cosTheta << std::endl <<
this->sinTheta << std::endl <<
this->focalDistance << std::endl <<
this->height << std::endl <<
this->Rz << std::endl;
confFile.close();
}