zoukankan      html  css  js  c++  java
  • JoinMap

    #include <iostream>
    #include <fstream>
    using namespace std;
    #include <opencv2/core/core.hpp>
    #include <opencv2/highgui/highgui.hpp>
    #include <Eigen/Geometry> 
    #include <boost/format.hpp>  // for formating strings
    #include <pcl/point_types.h> 
    #include <pcl/io/pcd_io.h> 
    //#include <pcl/visualization/pcl_visualizer.h>
    
    //#define EIGEN_DONT_VECTORIZE
    //#define EIGEN_DISABLE_UNALIGNED_ARRAY_ASSERT
    
    int main( int argc, char** argv )
    {
        vector<cv::Mat> colorImgs, depthImgs;    // 彩色图和深度图
        vector<Eigen::Isometry3d, Eigen::aligned_allocator<Eigen::Isometry3d>> poses;         // 相机位姿
        
        ifstream fin("./pose.txt");//读取相机位姿 如果没有相机位姿呢?相机位姿在哪里?
        if (!fin)
        {
            cerr<<"请在有pose.txt的目录下运行此程序"<<endl;
            //return 1;
        }
        
        for ( int i=0; i<1; i++ )
        {
            boost::format fmt( "./%s/%d.%s" ); //图像文件格式
            colorImgs.push_back( cv::imread( (fmt%"color"%(i+1)%"jpg").str() ));
            depthImgs.push_back( cv::imread( (fmt%"depth"%(i+1)%"jpg").str(), -1 )); // 使用-1读取原始图像
            
            double data[7] = {0};
            for(int j=0;j<7;j++)
            {
              double d=0;
              fin>>d;
              data[j]=d;
            }
            //for ( auto& d:data )
                //fin>>d;
            Eigen::Quaterniond q( data[6], data[3], data[4], data[5] ); //旋转4变量
            Eigen::Isometry3d T(q);
            T.pretranslate( Eigen::Vector3d( data[0], data[1], data[2] )); //平移3变量
            poses.push_back( T );
        }
        
        // 计算点云并拼接
        // 相机内参 
        double cx = 315.9;// 319.5
        double cy = 230.3;// 239.5
        double fx = 371.5;// 490.3
        double fy = 374.2;// 490.3
        double depthScale = 1000.0;
        
        cout<<"正在将图像转换为点云..."<<endl;
        
        // 定义点云使用的格式:这里用的是XYZRGB
        typedef pcl::PointXYZRGB PointT; 
        typedef pcl::PointCloud<PointT> PointCloud;
        
        // 新建一个点云
        PointCloud::Ptr pointCloud( new PointCloud ); 
        for ( int i=0; i<1; i++ )
        {
            cout<<"转换图像中: "<<i+1<<endl; 
            cv::Mat color = colorImgs[i]; 
            cv::Mat depth = depthImgs[i];
            Eigen::Isometry3d T = poses[i]; //姿态
            for ( int v=0; v<color.rows; v++ )
                for ( int u=0; u<color.cols; u++ )
                {
                    unsigned int d = depth.ptr<unsigned short> ( v )[u]; // 深度值
                    if ( d==0 ) continue; // 为0表示没有测量到
                    Eigen::Vector3d point; 
                    point[2] = double(d)/depthScale;  //深度值除以1000
                    point[0] = (u-cx)*point[2]/fx; //X
                    point[1] = (v-cy)*point[2]/fy; //Y
                    Eigen::Vector3d pointWorld = 1*point; //转世界坐标
                    
                    PointT p ;
                    p.x = pointWorld[0]; //世界坐标x
                    p.y = pointWorld[1]; //世界坐标y
                    p.z = pointWorld[2]; //世界坐标z
                    p.b = color.data[ v*color.step+u*color.channels() ]; //颜色b
                    p.g = color.data[ v*color.step+u*color.channels()+1 ]; //颜色g
                    p.r = color.data[ v*color.step+u*color.channels()+2 ]; //颜色r
                    pointCloud->points.push_back( p );
                }
         }
        
        pointCloud->is_dense = false;
        cout<<"点云共有"<<pointCloud->size()<<"个点."<<endl;
        pcl::io::savePCDFileBinary("map.pcd", *pointCloud );
        system("pause"); 
        return 0;
    }

    VS生成的C++项目常用目录设置: https://blog.csdn.net/CreateUserName_Keep/article/details/78884195

    VS2010项目属性的默认包含路径设置方法:

    可是用cloud_viewer.exe map.pcd在命令行却说“应用程序无法正常启动0xc000007b”,这一定是因为pcl的外部链接库没有添加到path路径或者是缺少dll文件导致的。但是在pcl目录下却可以

    这个很明显不大对

    https://blog.csdn.net/u012423865/article/details/78036543

    https://blog.csdn.net/blademan1234/article/details/79796901

     问题:真实距离是多少?为什么绘制的效果很差。。。噪声太多?

    操作:

  • 相关阅读:
    Docker 笔记
    Win10 Docker 安装使用
    golang struct转map
    Golang 中错误与异常需要重新认识
    Golang 中三种读取文件发放性能对比
    GoLang中如何使用多参数属性传参
    GoLang中flag标签使用
    Windows本地搭建Edusoho环境
    edusoho上传视频弹出abort之解决方案
    XAMPP启动mysql遇到的问题
  • 原文地址:https://www.cnblogs.com/2008nmj/p/10291601.html
Copyright © 2011-2022 走看看