GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — code/Chapter4_StructureFromMotion/Triangulation.cpp at master · u11/code Expand file tree/ Copy pathTriangulation.cppMore file actions executable file ·237 lines (200 loc) · 7.36 KB / Copy pathTriangulation.cppFile metadata and controlsexecutable file ·237 lines (200 loc) · 7.36 KB 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 230 231 232 233 234 235 236 237 /***************************************************************************** * ExploringSfMWithOpenCV ****************************************************************************** * by Roy Shilkrot, 5th Dec 2012 * http://www.morethantechnical.com/ ****************************************************************************** * Ch4 of the book "Mastering OpenCV with Practical Computer Vision Projects" * Copyright Packt Publishing 2012. * http://www.packtpub.com/cool-projects-with-opencv/book *****************************************************************************/ #include "Triangulation.h" #include <iostream> using namespace std; using namespace cv; #undef __SFM__DEBUG__ /** From "Triangulation", Hartley, R.I. and Sturm, P., Computer vision and image understanding, 1997 */ Mat_<double> LinearLSTriangulation(Point3d u, //homogenous image point (u,v,1) Matx34d P, //camera 1 matrix Point3d u1, //homogenous image point in 2nd camera Matx34d P1 //camera 2 matrix ) { //build matrix A for homogenous equation system Ax = 0 //assume X = (x,y,z,1), for Linear-LS method //which turns it into a AX = B system, where A is 4x3, X is 3x1 and B is 4x1 // cout << "u " << u <<", u1 " << u1 << endl; // Matx<double,6,4> A; //this is for the AX=0 case, and with linear dependence.. // A(0) = u.x*P(2)-P(0); // A(1) = u.y*P(2)-P(1); // A(2) = u.x*P(1)-u.y*P(0); // A(3) = u1.x*P1(2)-P1(0); // A(4) = u1.y*P1(2)-P1(1); // A(5) = u1.x*P(1)-u1.y*P1(0); // Matx43d A; //not working for some reason... // A(0) = u.x*P(2)-P(0); // A(1) = u.y*P(2)-P(1); // A(2) = u1.x*P1(2)-P1(0); // A(3) = u1.y*P1(2)-P1(1); Matx43d A(u.x*P(2,0)-P(0,0), u.x*P(2,1)-P(0,1), u.x*P(2,2)-P(0,2), u.y*P(2,0)-P(1,0), u.y*P(2,1)-P(1,1), u.y*P(2,2)-P(1,2), u1.x*P1(2,0)-P1(0,0), u1.x*P1(2,1)-P1(0,1), u1.x*P1(2,2)-P1(0,2), u1.y*P1(2,0)-P1(1,0), u1.y*P1(2,1)-P1(1,1), u1.y*P1(2,2)-P1(1,2) ); Matx41d B(-(u.x*P(2,3) -P(0,3)), -(u.y*P(2,3) -P(1,3)), -(u1.x*P1(2,3) -P1(0,3)), -(u1.y*P1(2,3) -P1(1,3))); Mat_<double> X; solve(A,B,X,DECOMP_SVD); return X; } /** From "Triangulation", Hartley, R.I. and Sturm, P., Computer vision and image understanding, 1997 */ Mat_<double> IterativeLinearLSTriangulation(Point3d u, //homogenous image point (u,v,1) Matx34d P, //camera 1 matrix Point3d u1, //homogenous image point in 2nd camera Matx34d P1 //camera 2 matrix ) { double wi = 1, wi1 = 1; Mat_<double> X(4,1); Mat_<double> X_ = LinearLSTriangulation(u,P,u1,P1); X(0) = X_(0); X(1) = X_(1); X(2) = X_(2); X(3) = 1.0; for (int i=0; i<10; i++) { //Hartley suggests 10 iterations at most //recalculate weights double p2x = Mat_<double>(Mat_<double>(P).row(2)*X)(0); double p2x1 = Mat_<double>(Mat_<double>(P1).row(2)*X)(0); //breaking point if(fabsf(wi - p2x) <= EPSILON && fabsf(wi1 - p2x1) <= EPSILON) break; wi = p2x; wi1 = p2x1; //reweight equations and solve Matx43d A((u.x*P(2,0)-P(0,0))/wi, (u.x*P(2,1)-P(0,1))/wi, (u.x*P(2,2)-P(0,2))/wi, (u.y*P(2,0)-P(1,0))/wi, (u.y*P(2,1)-P(1,1))/wi, (u.y*P(2,2)-P(1,2))/wi, (u1.x*P1(2,0)-P1(0,0))/wi1, (u1.x*P1(2,1)-P1(0,1))/wi1, (u1.x*P1(2,2)-P1(0,2))/wi1, (u1.y*P1(2,0)-P1(1,0))/wi1, (u1.y*P1(2,1)-P1(1,1))/wi1, (u1.y*P1(2,2)-P1(1,2))/wi1 ); Mat_<double> B = (Mat_<double>(4,1) << -(u.x*P(2,3) -P(0,3))/wi, -(u.y*P(2,3) -P(1,3))/wi, -(u1.x*P1(2,3) -P1(0,3))/wi1, -(u1.y*P1(2,3) -P1(1,3))/wi1 ); solve(A,B,X_,DECOMP_SVD); X(0) = X_(0); X(1) = X_(1); X(2) = X_(2); X(3) = 1.0; } return X; } //Triagulate points double TriangulatePoints(const vector<KeyPoint>& pt_set1, const vector<KeyPoint>& pt_set2, const Mat& K, const Mat& Kinv, const Mat& distcoeff, const Matx34d& P, const Matx34d& P1, vector<CloudPoint>& pointcloud, vector<KeyPoint>& correspImg1Pt) { #ifdef __SFM__DEBUG__ vector<double> depths; #endif // pointcloud.clear(); correspImg1Pt.clear(); Matx44d P1_(P1(0,0),P1(0,1),P1(0,2),P1(0,3), P1(1,0),P1(1,1),P1(1,2),P1(1,3), P1(2,0),P1(2,1),P1(2,2),P1(2,3), 0, 0, 0, 1); Matx44d P1inv(P1_.inv()); cout << "Triangulating..."; double t = getTickCount(); vector<double> reproj_error; unsigned int pts_size = pt_set1.size(); #if 0 //Using OpenCV's triangulation //convert to Point2f vector<Point2f> _pt_set1_pt,_pt_set2_pt; KeyPointsToPoints(pt_set1,_pt_set1_pt); KeyPointsToPoints(pt_set2,_pt_set2_pt); //undistort Mat pt_set1_pt,pt_set2_pt; undistortPoints(_pt_set1_pt, pt_set1_pt, K, distcoeff); undistortPoints(_pt_set2_pt, pt_set2_pt, K, distcoeff); //triangulate Mat pt_set1_pt_2r = pt_set1_pt.reshape(1, 2); Mat pt_set2_pt_2r = pt_set2_pt.reshape(1, 2); Mat pt_3d_h(1,pts_size,CV_32FC4); cv::triangulatePoints(P,P1,pt_set1_pt_2r,pt_set2_pt_2r,pt_3d_h); //calculate reprojection vector<Point3f> pt_3d; convertPointsHomogeneous(pt_3d_h.reshape(4, 1), pt_3d); cv::Mat_<double> R = (cv::Mat_<double>(3,3) << P(0,0),P(0,1),P(0,2), P(1,0),P(1,1),P(1,2), P(2,0),P(2,1),P(2,2)); Vec3d rvec; Rodrigues(R ,rvec); Vec3d tvec(P(0,3),P(1,3),P(2,3)); vector<Point2f> reprojected_pt_set1; projectPoints(pt_3d,rvec,tvec,K,distcoeff,reprojected_pt_set1); for (unsigned int i=0; i<pts_size; i++) { CloudPoint cp; cp.pt = pt_3d[i]; pointcloud.push_back(cp); reproj_error.push_back(norm(_pt_set1_pt[i]-reprojected_pt_set1[i])); } #else Mat_<double> KP1 = K * Mat(P1); #pragma omp parallel for num_threads(1) for (int i=0; i<pts_size; i++) { Point2f kp = pt_set1[i].pt; Point3d u(kp.x,kp.y,1.0); Mat_<double> um = Kinv * Mat_<double>(u); u.x = um(0); u.y = um(1); u.z = um(2); Point2f kp1 = pt_set2[i].pt; Point3d u1(kp1.x,kp1.y,1.0); Mat_<double> um1 = Kinv * Mat_<double>(u1); u1.x = um1(0); u1.y = um1(1); u1.z = um1(2); Mat_<double> X = IterativeLinearLSTriangulation(u,P,u1,P1); // cout << "3D Point: " << X << endl; // Mat_<double> x = Mat(P1) * X; // cout << "P1 * Point: " << x << endl; // Mat_<double> xPt = (Mat_<double>(3,1) << x(0),x(1),x(2)); // cout << "Point: " << xPt << endl; Mat_<double> xPt_img = KP1 * X; //reproject // cout << "Point * K: " << xPt_img << endl; Point2f xPt_img_(xPt_img(0)/xPt_img(2),xPt_img(1)/xPt_img(2)); #pragma omp critical { double reprj_err = norm(xPt_img_-kp1); reproj_error.push_back(reprj_err); CloudPoint cp; cp.pt = Point3d(X(0),X(1),X(2)); cp.reprojection_error = reprj_err; pointcloud.push_back(cp); correspImg1Pt.push_back(pt_set1[i]); #ifdef __SFM__DEBUG__ depths.push_back(X(2)); #endif } } #endif Scalar mse = mean(reproj_error); t = ((double)getTickCount() - t)/getTickFrequency(); cout << "Done. ("<<pointcloud.size()<<"points, " << t <<"s, mean reproj err = " << mse[0] << ")"<< endl; //show "range image" #ifdef __SFM__DEBUG__ { double minVal,maxVal; minMaxLoc(depths, &minVal, &maxVal); Mat tmp(240,320,CV_8UC3,Scalar(0,0,0)); //cvtColor(img_1_orig, tmp, CV_BGR2HSV); for (unsigned int i=0; i<pointcloud.size(); i++) { double _d = MAX(MIN((pointcloud[i].z-minVal)/(maxVal-minVal),1.0),0.0); circle(tmp, correspImg1Pt[i].pt, 1, Scalar(255 * (1.0-(_d)),255,255), CV_FILLED); } cvtColor(tmp, tmp, CV_HSV2BGR); imshow("Depth Map", tmp); waitKey(0); destroyWindow("Depth Map"); } #endif return mse[0]; } |
| You are visitor 01402775 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |