Kinect Fusion V2导出网格,颜色为STL或OBJ文件

时间:2015-12-14 15:53:07

标签: kinect kinect-sdk

我正在使用Kinect V2和Kinect Fusion Explorer V2来记录3D扫描。我也正在记录颜色,但如果我尝试将颜色导出网格作为STL或OBJ文件,则网格没有颜色。仅当我导出PLY文件时,网格才有颜色。我需要一个STL文件或OBJ文件,我认为MTL文件丢失了。我在Kinect文档中找不到只有PLY文件包含颜色的任何内容。

1 个答案:

答案 0 :(得分:0)

尝试使用Windows应用商店中的 3D扫描,扩展程序 .3mf 并且为Color,或者自己编写:

可能值得花时间查看SDK中的Kinect Fusion示例:https://www.microsoft.com/en-au/download/details.aspx?id=44561

ColorMesh mesh = null;

Win32.SaveFileDialog dialog = new Win32.SaveFileDialog();

if (true == this.objFormat.IsChecked)
{
dialog.FileName = "MeshedReconstruction.obj";
dialog.Filter = "OBJ Mesh Files|*.obj|All Files|*.*";
}

using (StreamWriter writer = new StreamWriter(dialog.FileName))
{
// Default to flip Y,Z coordinates on save
KinectFusionHelper.SaveAsciiObjMesh(mesh, writer, true);
}

在助手班:

/// <summary>
/// Save mesh in ASCII WaveFront .OBJ file
/// </summary>
/// <param name="mesh">Calculated mesh object</param>
/// <param name="writer">The text writer</param>
/// <param name="flipAxes">Flag to determine whether the Y and Z values are flipped on save,
/// default should be true.</param>
public static void SaveAsciiObjMesh(ColorMesh mesh, TextWriter writer, bool flipAxes)
{
if (null == mesh || null == writer)
{
return;
}

var vertices = mesh.GetVertices();
var normals = mesh.GetNormals();
var indices = mesh.GetTriangleIndexes();

// Check mesh arguments
if (0 == vertices.Count || 0 != vertices.Count % 3 || vertices.Count != indices.Count)
{
throw new ArgumentException(Properties.Resources.InvalidMeshArgument);
}

// Write the header lines
writer.WriteLine("#");
writer.WriteLine("# OBJ file created by Microsoft Kinect Fusion");
writer.WriteLine("#");

// Sequentially write the 3 vertices of the triangle, for each triangle
for (int i = 0; i < vertices.Count; i++)
{
var vertex = vertices[i];

string vertexString = "v " + vertex.X.ToString(CultureInfo.InvariantCulture) + " ";

if (flipAxes)
{
vertexString += (-vertex.Y).ToString(CultureInfo.InvariantCulture) + " " + (-vertex.Z).ToString(CultureInfo.InvariantCulture);
}
else
{
vertexString += vertex.Y.ToString(CultureInfo.InvariantCulture) + " " + vertex.Z.ToString(CultureInfo.InvariantCulture);
}

writer.WriteLine(vertexString);
}

// Sequentially write the 3 normals of the triangle, for each triangle
for (int i = 0; i < normals.Count; i++)
{
var normal = normals[i];

string normalString = "vn " + normal.X.ToString(CultureInfo.InvariantCulture) + " ";

if (flipAxes)
{
normalString += (-normal.Y).ToString(CultureInfo.InvariantCulture) + " " + (-normal.Z).ToString(CultureInfo.InvariantCulture);
}
else
{
normalString += normal.Y.ToString(CultureInfo.InvariantCulture) + " " + normal.Z.ToString(CultureInfo.InvariantCulture);
}

writer.WriteLine(normalString);
}

// Sequentially write the 3 vertex indices of the triangle face, for each triangle
// Note this is typically 1-indexed in an OBJ file when using absolute referencing!
for (int i = 0; i < vertices.Count / 3; i++)
{
string baseIndex0 = ((i * 3) + 1).ToString(CultureInfo.InvariantCulture);
string baseIndex1 = ((i * 3) + 2).ToString(CultureInfo.InvariantCulture);
string baseIndex2 = ((i * 3) + 3).ToString(CultureInfo.InvariantCulture);

string faceString = "f " + baseIndex0 + "//" + baseIndex0 + " " + baseIndex1 + "//" + baseIndex1 + " " + baseIndex2 + "//" + baseIndex2;
writer.WriteLine(faceString);
}
}

您可以看到,默认情况下不包含.OBJ顶点颜色 - 您需要在.MTL文件中包含.OBJ的顶点颜色信息。

mesh.VertexColors; 

请参阅:https://msdn.microsoft.com/en-us/library/microsoft.kinect.fusion.mesh.vertexcolors.aspx

batman.OBJ 标题:

mtllib batman.mtl

可能包含一些数据:

# 
# 

newmtl batman
Ns 96.078431
Ka 0.000000 0.000000 0.000000
Kd 0.640000 0.640000 0.640000
Ks 0.100000 0.100000 0.100000
Ni 1.000000
d 1.000000

...

或许请查看:https://msdn.microsoft.com/en-us/library/dn435687.aspx了解更多信息。

相关问题