35 static void ProjectRot(CvMat* state, CvMat* projection,
void* x)
40 int count = projection->rows;
41 CvMat rot_mat = cvMat(3, 1, CV_64F, &(state->data.db[0+0]));
42 double zeros[3] = {0};
43 CvMat zero_tra = cvMat(3, 1, CV_64F, zeros);
44 cvReshape(projection, projection, 2, 1);
45 cvProjectPoints2(object_model, &rot_mat, &zero_tra, &(camera->
calib_K), &(camera->
calib_D), projection);
46 cvReshape(projection, projection, 1, count);
50 static void Project(CvMat* state, CvMat* projection,
void* x)
55 int count = projection->rows;
56 CvMat rot_mat = cvMat(3, 1, CV_64F, &(state->data.db[0+0]));
57 CvMat tra_mat = cvMat(3, 1, CV_64F, &(state->data.db[0+3]));
58 cvReshape(projection, projection, 2, 1);
59 cvProjectPoints2(object_model, &rot_mat, &tra_mat, &(camera->
calib_K), &(camera->
calib_D), projection);
60 cvReshape(projection, projection, 1, count);
64 double rot[3]; CvMat rotm = cvMat(3, 1, CV_64F, rot);
65 double tra[3]; CvMat tram = cvMat(3, 1, CV_64F, tra);
68 bool ret = UpdatePose(object_points, image_points, &rotm, &tram, weights);
74 bool CameraEC::UpdatePose(
const CvMat* object_points, CvMat* image_points, CvMat *rot, CvMat *tra, CvMat *weights) {
75 if (object_points->height < 4)
return false;
81 CvMat* par = cvCreateMat(6, 1, CV_64F);
82 memcpy(&(par->data.db[0+0]), rot->data.db, 3*
sizeof(
double));
83 memcpy(&(par->data.db[0+3]), tra->data.db, 3*
sizeof(
double));
92 memcpy(rot->data.db, &(par->data.db[0+0]), 3*
sizeof(
double));
93 memcpy(tra->data.db, &(par->data.db[0+3]), 3*
sizeof(
double));
103 double rot[3]; CvMat rotm = cvMat(3, 1, CV_64F, rot);
104 double tra[3]; CvMat tram = cvMat(3, 1, CV_64F, tra);
107 bool ret = UpdateRotation(object_points, image_points, &rotm, &tram);
115 CvMat* par = cvCreateMat(3, 1, CV_64F);
116 memcpy(&(par->data.db[0+0]), rot->data.db, 3*
sizeof(
double));
122 memcpy(rot->data.db, &(par->data.db[0+0]), 3*
sizeof(
double));
136 double kid[9], rotd[9], trad[3], ud[3] = {u->x, u->y, 1};
137 CvMat Ki = cvMat(3, 3, CV_64F, kid);
138 CvMat R = cvMat(3, 3, CV_64F, rotd);
139 CvMat T = cvMat(3, 1, CV_64F, trad);
140 CvMat U = cvMat(3, 1, CV_64F, ud);
143 cvInv(&(camera->
calib_K), &Ki);
144 cvMatMul(&R, &Ki, &Ki);
145 cvGEMM(&Ki, &U, 1, &T, 1, P, 0);
148 bool MidPointAlgorithm(CvMat* o1, CvMat* o2, CvMat* p1, CvMat* p2, CvPoint3D32f& X,
double limit)
150 double ud[3], vd[3], wd[3];
151 CvMat u = cvMat(3, 1, CV_64F, ud);
152 CvMat v = cvMat(3, 1, CV_64F, vd);
153 CvMat
w = cvMat(3, 1, CV_64F, wd);
159 double a = cvDotProduct(&u, &u);
160 double b = cvDotProduct(&u, &v);
161 double c = cvDotProduct(&v, &v);
162 double d = cvDotProduct(&u, &w);
163 double e = cvDotProduct(&v, &w);
164 double D = a*c - b*b;
173 tc = (b>c ? d/b : e/c);
177 sc = (b*e - c*
d) / D;
178 tc = (a*e - b*
d) / D;
181 double m1d[3], m2d[3];
182 CvMat m1 = cvMat(3, 1, CV_64F, m1d);
183 CvMat m2 = cvMat(3, 1, CV_64F, m2d);
184 cvAddWeighted(&u, sc, o1, 1.0, 0.0, &m1);
185 cvAddWeighted(&v, tc, o2, 1.0, 0.0, &m2);
186 cvAddWeighted(&m1, 0.5, &m2, 0.5, 0.0, &m1);
197 double o1d[3], o2d[3], p1d[3], p2d[3];
198 CvMat o1 = cvMat(3, 1, CV_64F, o1d);
199 CvMat o2 = cvMat(3, 1, CV_64F, o2d);
200 CvMat p1 = cvMat(3, 1, CV_64F, p1d);
201 CvMat p2 = cvMat(3, 1, CV_64F, p2d);
216 double pd[16], md[9], kd[9];
217 CvMat P = cvMat(4, 4, CV_64F, pd);
218 CvMat H = cvMat(3, 3, CV_64F, md);
219 CvMat Ki = cvMat(3, 3, CV_64F, kd);
222 cvInv(&(calib_K), &Ki);
225 int ind_s = 0, ind_c = 0;
226 for(
int i = 0; i < 3; ++i)
228 CvRect
r; r.x = ind_s; r.y = 0; r.height = 3; r.width = 1;
229 CvMat sub = cvMat(3, 1, CV_64F);
230 cvGetSubRect(&P, &sub, r);
231 CvMat col = cvMat(3, 1, CV_64F);
232 cvGetCol(&H, &col, ind_c);
241 double xd[3] = {p2d.x, p2d.y, 1};
242 CvMat X = cvMat(3, 1, CV_64F, xd);
243 cvMatMul(&Ki, &X, &X);
245 cvMatMul(&H, &X, &X);
247 p3d.x = (float)(xd[0] / xd[2]);
248 p3d.y = (float)(xd[1] / xd[2]);
261 wx = depth*(p2d.x-calib_K_data[0][2])/calib_K_data[0][0];
262 wy = depth*(p2d.y-calib_K_data[1][2])/calib_K_data[1][1];
269 double Xd[4] = {wx, wy, wz, 1};
270 CvMat Xdm = cvMat(4, 1, CV_64F, Xd);
272 CvMat Pdm = cvMat(4, 4, CV_64F, Pd);
274 cvMatMul(&Pdm, &Xdm, &Xdm);
275 p3d.x = float(Xd[0]/Xd[3]);
276 p3d.y = float(Xd[1]/Xd[3]);
277 p3d.z = float(Xd[2]/Xd[3]);
void GetRodriques(CvMat *mat) const
Returns the rotation in rotation vector form.
void GetOrigo(Pose *pose, CvMat *O)
bool MidPointAlgorithm(CvMat *o1, CvMat *o2, CvMat *p1, CvMat *p2, CvPoint3D32f &X, double limit)
bool UpdatePose(std::map< int, T > &container, Pose *pose, int type_id=-1, std::map< int, double > *weights=0)
Update the pose using the items with matching type_id.
void GetTranslation(CvMat *tra) const
void GetPointOnLine(const Pose *pose, Camera *camera, const CvPoint2D32f *u, CvMat *P)
void Get3dOnPlane(const Pose *pose, CvPoint2D32f p2d, CvPoint3D32f &p3d)
Get 3D-coordinate for 2D feature on the plane defined by the pose (z == 0)
double Optimize(CvMat *parameters, CvMat *measurements, double stop, int max_iter, EstimateCallback Estimate, void *param=0, OptimizeMethod method=LEVENBERGMARQUARDT, CvMat *parameters_mask=0, CvMat *J_mat=0, CvMat *weights=0)
Runs the optimization loop with selected parameters.
Non-linear optimization routines. There are three methods implemented that include Gauss-Newton...
Simple Camera class for calculating distortions, orientation or projections with pre-calibrated camer...
static void Project(CvMat *state, CvMat *projection, void *x)
This file implements several optimization algorithms.
bool ReconstructFeature(const Pose *pose1, const Pose *pose2, const CvPoint2D32f *u1, const CvPoint2D32f *u2, CvPoint3D32f *p3d, double limit)
Reconstruct 3D point using two camera poses and corresponding undistorted image feature positions...
Pose representation derived from the Rotation class
void Get3dOnDepth(const Pose *pose, CvPoint2D32f p2d, float depth, CvPoint3D32f &p3d)
Get 3D-coordinate for 2D feature assuming certain depth.
void SetRodriques(const CvMat *mat)
Sets the rotation from given rotation vector.
TFSIMD_FORCE_INLINE const tfScalar & w() const
void GetMatrix(CvMat *mat) const
This file implements a collection of External Container (EC) versions of many ALVAR classes...
void SetTranslation(const CvMat *tra)
bool UpdateRotation(std::map< int, T > &container, Pose *pose, int type_id=-1)
Update the rotation in pose using the items with matching type_id.
const CvMat * object_model
static void ProjectRot(CvMat *state, CvMat *projection, void *x)
void Undistort(std::vector< PointDouble > &points)
Unapplys the lens distortion for points on image plane.