Building mesh and texture in MeshLab using point cloud produced by stereo reconstruction

本文介绍了如何利用OpenCV进行立体重建得到的点云数据,在MeshLab中生成3D网格并进行纹理映射。首先,讨论了将点云导出为PLY文件的过程,包括三维空间重新投影、维度检查、顶点信息和UV坐标等。接着,提到了构建MeshLab项目文件的方法。手动网格生成和参数化以及纹理应用的步骤也进行了讲解,包括面法线计算、表面重建、清理和修改网格,以及参数化和纹理贴图。文章还涉及了使用MeshLab Server进行批处理的可能性,以及遇到的问题和解决方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

对不起诸位,我实在没有时间写中文的了,直接贴已经写好的英文版微笑

 

 

Building mesh and texture in MeshLab using point cloud produced by stereo reconstruction

 

The point cloud from a stereo reconstruction could be used in many ways. One of them is to be used to generate a 3D mesh and, further, the 3D model of the object in the real world. In this post, I will share my experience on mesh generation and texturing.

 

These days, I was working on generating meshes from 3D point cloud obtained from stereo reconstruction. The tools I am using is as follows:

 

 

  • Stereo calibration and reconstruction by OpenCV.

  • Mesh generation and texturing by MeshLab.

 

 

This post is arranged as

 

 

  • Exporting the point cloud as a PLY file.

  • Composing a MeshLab project (.mlp) file.

  • Manual mesh generation and texturing.

  • Batch mesh generation and texturing.

 

 

I experienced a lot of try-and-error loops as I walking through these processes. I would like to share those experiences here because somebody else may be working on similar projects and get frustrated about the situation that there are not enough tutorials that we could just watch and learn.

 

 

For this document, I was using Ubuntu 16.04. The MeshLab version is V1.3.2_64bit (Feb 19 2016), and MeshLab_64bit_fp v2016.12. Sorry for mixing the versions. I was working on my laptop and desktop at the same time. Well, I mean working at the lab and working from home.

 

1 Exporting the point cloud as a PLY file

In the sample code of OpenCV, it outputs the 3D point cloud into a PLY file. There are a couple of things that we should take care when we doing this.

 

1.1 Reprojecting to 3D space

 

If the OpenCV function reprojectImageTo3D( ) is used, we need the Q matrix that produced by the stereoRectify( ) function. This may means that we have to do the calibration ourselves by OpenCV. The other things that matter are that, as learned from the sample code of OpenCV, certain elements of Q should be modified to make the 3D point cloud lying along the right direction. If a zero-based indexing is used here, the following elements should be modified:

 

Q[1, 1] *= -1

 

Q[1, 3] *= -1

Q[2, 3] *= -1

 

The modified Q matrix makes the point cloud using a Y-axis parallel to the global Y-axis.

 

 

1.2 Dimension check

 

It is always a good idea to check the dimension inside MeshLab to see if the reprojected point cloud has the right special size. I use the “Measuring Tool” in figure 1 to measure the known distance between two points.

 

 

Figure 1 The “Measuring Tool” of MeshLab.

 

1.3 Per-vertex information and UV coordinate

 

Sometimes we would like the PLY file to contain additional information. I face two issues when I was trying to figure out what I could put inside a PLY file. One is what kind of information is allowed to put into a PLY file. The other is what information MeshLab is expecting or MeshLab could make use of. Unfortunately, I have clear answers on none of them.

 

First of all, it seems to me that the PLY file format only makes constraints on the data type we could use, but not on the data itself. You could put anything you want as long as the data type is defined and the downstream program could recognize the data. But somehow, we always would like a list which makes it clear that what the “pre-defined” data people are using.

 

 

After some search, I found some useful information. One documentation of MATLAB summarized the data types often encountered in a PLY file. I think most of them MeshLab could recognize, but I did not test all of them. Particularly for our project, we want to bake a texture onto a mesh. The pipeline looks like this:

 

 

Table 1 Processing pipeline of this project.

 

Stereo images -> disparity map -> reprojected 3D point cloud -> mesh -> textured mesh

 

The mesh and texture will be generated in MeshLab. So it becomes important how we could provide information about the texture along with the point cloud before the mesh is generated. In the beginning, we are looking for methods to do some sort of a manual UV mapping. The idea is simply that since the point cloud is generated by stereo reconstruction, we could just us the image from the base camera as the textu

非常抱歉,我犯了一个错误。在OpenMesh中,确实没有`mesh.texture()`函数。为了获取带有纹理坐标的三角网格模型中使用的纹理文件名,可以使用以下代码: ```cpp #include <OpenMesh/Core/IO/MeshIO.hh> #include <OpenMesh/Core/Mesh/TriMesh_ArrayKernelT.hh> #include <iostream> #include <unordered_set> struct MyTraits : public OpenMesh::DefaultTraits { // Add texture coordinate property to mesh traits VertexAttributes(OpenMesh::Attributes::TexCoord2D); }; typedef OpenMesh::TriMesh_ArrayKernelT<MyTraits> MyMesh; int main() { MyMesh mesh; std::string filename = "model.obj"; if (!OpenMesh::IO::read_mesh(mesh, filename)) { std::cerr << "Error: Cannot read mesh from file " << filename << std::endl; return 1; } // Use an unordered_set to store unique texture file names std::unordered_set<std::string> texture_files; // Iterate over all faces and their vertices for (MyMesh::ConstFaceIter f_it = mesh.faces_begin(); f_it != mesh.faces_end(); ++f_it) { for (MyMesh::ConstFaceVertexIter fv_it = mesh.cfv_iter(*f_it); fv_it.is_valid(); ++fv_it) { if (mesh.has_vertex_texcoords2D()) { OpenMesh::Vec2f texcoord = mesh.texcoord2D(*fv_it); MyMesh::TexCoord2D tex = mesh.texcoord2D(*fv_it); int tex_idx = tex[1] * mesh.n_faces() + tex[0]; std::string texture_file = mesh.texture(tex_idx).fileName(); if (!texture_file.empty()) { texture_files.insert(texture_file); } } } } // Print names of all unique texture files used by the mesh for (const std::string& texture_file : texture_files) { std::cout << "Texture file used: " << texture_file << std::endl; } return 0; } ``` 此代码与之前的代码类似,但使用了一个`std::unordered_set`来存储所有唯一的纹理文件名。对于每个顶点,它使用纹理坐标计算纹理索引,并使用`mesh.texture()`函数获取与该索引对应的纹理对象。然后,它从纹理对象中获取文件名,并将其插入到`texture_files`中,以便最终输出所有唯一的纹理文件名。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值