使用opencv将3d点转换为.ply格式

时间:2018-08-31 13:37:14

标签: c++ opencv 3d

我使用此文档link并获得3d点

vector<String> images_paths;
images_paths.pushBack(...);
float f  = 400,
cx = 200, cy = 200;
Matx33d K = Matx33d( f, 0, cx,
                   0, f, cy,
                   0, 0,  1);
bool is_projective = true;
vector<Mat> Rs_est, ts_est, points3d_estimated;
reconstruct(images_paths, Rs_est, ts_est, K, points3d_estimated, is_projective);

points3d_estimated变量中存在3d点,但我希望将点导出到.ply文件

我使用此步骤,但要点是错误的:

std::vector<cv::Point3f> _points;
std::vector<std::vector<int>> _faces;
for (int i = 0; i < points3d_estimated.size(); ++i)
{
   _points.push_back(Point3f((points3d_estimated[i])));
}


// create cubic for each point.
std::vector<cv::Point3f> p_tmp;
    for (auto &p : _points)
    {
        p_tmp.push_back(p);

        p_tmp.push_back(p);
        p_tmp.back().z += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().z += 0.1;
        p_tmp.back().y += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().y += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().x += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().x += 0.1;
        p_tmp.back().z += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().x += 0.1;
        p_tmp.back().y += 0.1;
        p_tmp.back().z += 0.1;
        p_tmp.push_back(p);
        p_tmp.back().x += 0.1;
        p_tmp.back().y += 0.1;
    }

    _points = p_tmp;

    //===

    int indx = 1;
    size_t size = _points.size();
    for(auto &p: _points)
    {
        int elp = indx % 8;
        if(indx > 7 && elp == 0 && indx < size-8)
        {
            _faces.push_back(vector<int>{4, indx, indx+1 ,indx+2, indx+3});
            _faces.push_back(vector<int>{4, indx+7, indx+6 ,indx+5, indx+4});
            _faces.push_back(vector<int>{4, indx, indx+4 ,indx+5, indx+1});
            _faces.push_back(vector<int>{4, indx+1, indx+5 ,indx+6, indx+2});
            _faces.push_back(vector<int>{4, indx+2, indx+6 ,indx+7, indx+3});
            _faces.push_back(vector<int>{4, indx+3, indx+7 ,indx+4, indx+0});
        }
        indx++;
    }

然后导出到.ply文件:

std::ofstream filestream;
filestream.open("output.ply",std::ios::out);


int p_size = (int)(_points.size());
int f_size = (int)(_faces.size());

// MARK: Header writing
filestream << "ply" << std::endl <<
           "format " << "ascii" << " 1.0" << std::endl <<
           "comment file " << std::endl <<
           "element vertex " << p_size << std::endl <<
           "property float x" << std::endl <<
           "property float y" << std::endl <<
           "property float z" << std::endl <<
           "element face " << f_size <<std::endl <<
           "property list uchar int vertex_indices" <<std::endl <<
           "end_header" << std::endl;


for(auto &p : _points) // Loop through all elements
{
    filestream.precision(4);
    filestream << std::fixed << p.x << " " << p.y << " " << p.z << endl;
}
for(auto &f : _faces)
{
    size_t f_s = f.size();
    for(int i=0; i<f_s; i++)
    {
        auto _f = f[i];
        filestream << std::setprecision(2) << _f << (i==(f_s-1)?"":" ");
    }
    filestream << std::endl;
}

filestream.close();

和.ply文件始终是相同的:

enter image description here

0 个答案:

没有答案