#include #include #include #include #include /*#include #include #include #include #include #include #include #include */ #include #include "pnp.h" #include "../Steger.h" using namespace std; using namespace cv; void find_feature_matches ( const Mat& img_1, const Mat& img_2, std::vector& keypoints_1, std::vector& keypoints_2, std::vector< DMatch >& matches ); void bundleAdjustment ( const vector points_3d, const vector points_2d, const Mat& K, Mat& R, Mat& t ); void verify_result(Mat R,Mat t,vector pts_2d,Mat K,vector pts_3d) { if(pts_2d.size()!=pts_3d.size()) { printf(" pts_2d.size()!=pts_3d.size() \n"); return; } Mat RINV=R.inv(); for(int i=0;i(0,0); float dy=pts_3d[i].y+t.at(1,0); float dz=pts_3d[i].z+t.at(2,0); float distance=sqrt(dx*dx+dy*dy+dz*dz); Point2d p1 = pixel2cam ( pts_2d[i], K ); Point3f pose3d ( Point3f ( p1.x*distance, p1.y*distance, distance ) ); Mat pose3d_m=Mat::zeros(3,1,R.type()); pose3d_m.at(0,0)=pose3d.x-t.at(0,0); pose3d_m.at(1,0)=pose3d.y-t.at(1,0); pose3d_m.at(2,0)=pose3d.z-t.at(2,0); Mat o_pose=RINV*pose3d_m; printf(" cal :%f, %f, %f org:%f, %f, %f\n",o_pose.at(0,0),o_pose.at(1,0),o_pose.at(2,0), pts_3d[i].x,pts_3d[i].y,pts_3d[i].z); } } void test_pnp(){ Mat K = ( Mat_ ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 ); cv::Mat R=(Mat_ ( 3,3 ) <<0.9979193252225095, -0.05138618904650328, 0.03894200717385427, 0.05033852907733768, 0.9983556574295407, 0.02742286944795593, -0.04028712992732941, -0.02540552801471822, 0.998865109165653); cv::Mat T=(Mat_(3,1)<<-0.1255867099750184,-0.007363525258815341,0.0609992658867812); P3d2d p3d2d(K); p3d2d.R_=R; p3d2d.t_=T; cv::Mat src = cv::imread("../images/3.bmp", 0); cv::Mat image = src(cv::Rect(0, 0, src.cols, src.rows)).clone(); for (int i = 0; i < image.rows; i++) { for (int j = 0; j < image.cols; j++) { if (image.at(i, j) < 20) image.at(i, j) = 0; } } cv::Mat out= cv::Mat::zeros(image.size(), CV_8UC1); CSteger steger; cv::GaussianBlur(image, image, cv::Size(3, 3), 3, 3); std::vector points = steger.StripCenter(image); auto t1=std::chrono::steady_clock::now(); p3d2d.execute(points); auto t2=std::chrono::steady_clock::now(); double ms=std::chrono::duration(t2-t1).count(); printf("points :%d time :%lf\n",points.size(),ms); } int main ( int argc, char** argv ) { test_pnp(); return 0; if ( argc != 4 ) { cout<<"usage: pose_estimation_3d2d img1 img2 depth1"< keypoints_1, keypoints_2; vector matches; find_feature_matches ( img_1, img_2, keypoints_1, keypoints_2, matches ); cout<<"一共找到了"< ( 3,3 ) << 520.9, 0, 325.1, 0, 521.0, 249.7, 0, 0, 1 ); vector pts_3d; vector pts_2d; for ( DMatch m:matches ) { ushort d = d1.ptr (int ( keypoints_1[m.queryIdx].pt.y )) [ int ( keypoints_1[m.queryIdx].pt.x ) ]; if ( d == 0 ) // bad depth continue; float dd = d/5000.0; Point2d p1 = pixel2cam ( keypoints_1[m.queryIdx].pt, K ); pts_3d.push_back ( Point3f ( p1.x*dd, p1.y*dd, dd ) ); pts_2d.push_back ( keypoints_2[m.trainIdx].pt ); printf("image1 key 3d :%f, %f, %f image2 key 2d:%f, %f\n",p1.x*dd, p1.y*dd, dd , keypoints_2[m.trainIdx].pt.x,keypoints_2[m.trainIdx].pt.y); } cout<<"3d-2d pairs: "<& keypoints_1, std::vector& keypoints_2, std::vector< DMatch >& matches ) { //-- 初始化 Mat descriptors_1, descriptors_2; // used in OpenCV3 Ptr detector = ORB::create(); Ptr descriptor = ORB::create(); // use this if you are in OpenCV2 // Ptr detector = FeatureDetector::create ( "ORB" ); // Ptr descriptor = DescriptorExtractor::create ( "ORB" ); Ptr matcher = DescriptorMatcher::create ( "BruteForce-Hamming" ); //-- 第一步:检测 Oriented FAST 角点位置 detector->detect ( img_1,keypoints_1 ); detector->detect ( img_2,keypoints_2 ); //-- 第二步:根据角点位置计算 BRIEF 描述子 descriptor->compute ( img_1, keypoints_1, descriptors_1 ); descriptor->compute ( img_2, keypoints_2, descriptors_2 ); //-- 第三步:对两幅图像中的BRIEF描述子进行匹配,使用 Hamming 距离 vector match; // BFMatcher matcher ( NORM_HAMMING ); matcher->match ( descriptors_1, descriptors_2, match ); //-- 第四步:匹配点对筛选 double min_dist=10000, max_dist=0; //找出所有匹配之间的最小距离和最大距离, 即是最相似的和最不相似的两组点之间的距离 for ( int i = 0; i < descriptors_1.rows; i++ ) { double dist = match[i].distance; if ( dist < min_dist ) min_dist = dist; if ( dist > max_dist ) max_dist = dist; } printf ( "-- Max dist : %f \n", max_dist ); printf ( "-- Min dist : %f \n", min_dist ); //当描述子之间的距离大于两倍的最小距离时,即认为匹配有误.但有时候最小距离会非常小,设置一个经验值30作为下限. for ( int i = 0; i < descriptors_1.rows; i++ ) { if ( match[i].distance <= max ( 2*min_dist, 30.0 ) ) { matches.push_back ( match[i] ); } } } void bundleAdjustment ( const vector< Point3f > points_3d, const vector< Point2f > points_2d, const Mat& K, Mat& R, Mat& t ) { // 初始化g2o /*typedef g2o::BlockSolver< g2o::BlockSolverTraits<6,3> > Block; // pose 维度为 6, landmark 维度为 3 Block::LinearSolverType* linearSolver = new g2o::LinearSolverCSparse(); // 线性方程求解器 Block* solver_ptr = new Block ( linearSolver ); // 矩阵块求解器 g2o::OptimizationAlgorithmLevenberg* solver = new g2o::OptimizationAlgorithmLevenberg ( solver_ptr ); g2o::SparseOptimizer optimizer; optimizer.setAlgorithm ( solver ); // vertex g2o::VertexSE3Expmap* pose = new g2o::VertexSE3Expmap(); // camera pose Eigen::Matrix3d R_mat; R_mat << R.at ( 0,0 ), R.at ( 0,1 ), R.at ( 0,2 ), R.at ( 1,0 ), R.at ( 1,1 ), R.at ( 1,2 ), R.at ( 2,0 ), R.at ( 2,1 ), R.at ( 2,2 ); pose->setId ( 0 ); pose->setEstimate ( g2o::SE3Quat ( R_mat, Eigen::Vector3d ( t.at ( 0,0 ), t.at ( 1,0 ), t.at ( 2,0 ) ) ) ); optimizer.addVertex ( pose ); int index = 1; for ( const Point3f p:points_3d ) // landmarks { g2o::VertexSBAPointXYZ* point = new g2o::VertexSBAPointXYZ(); point->setId ( index++ ); point->setEstimate ( Eigen::Vector3d ( p.x, p.y, p.z ) ); point->setMarginalized ( true ); // g2o 中必须设置 marg 参见第十讲内容 optimizer.addVertex ( point ); } // parameter: camera intrinsics g2o::CameraParameters* camera = new g2o::CameraParameters ( K.at ( 0,0 ), Eigen::Vector2d ( K.at ( 0,2 ), K.at ( 1,2 ) ), 0 ); camera->setId ( 0 ); optimizer.addParameter ( camera ); // edges index = 1; for ( const Point2f p:points_2d ) { g2o::EdgeProjectXYZ2UV* edge = new g2o::EdgeProjectXYZ2UV(); edge->setId ( index ); edge->setVertex ( 0, dynamic_cast ( optimizer.vertex ( index ) ) ); edge->setVertex ( 1, pose ); edge->setMeasurement ( Eigen::Vector2d ( p.x, p.y ) ); edge->setParameterId ( 0,0 ); edge->setInformation ( Eigen::Matrix2d::Identity() ); optimizer.addEdge ( edge ); index++; } chrono::steady_clock::time_point t1 = chrono::steady_clock::now(); optimizer.setVerbose ( true ); optimizer.initializeOptimization(); optimizer.optimize ( 100 ); chrono::steady_clock::time_point t2 = chrono::steady_clock::now(); chrono::duration time_used = chrono::duration_cast> ( t2-t1 ); cout<<"optimization costs time: "<