wrl文件转换成obj文件
在处理3D数据时发现有些文件是wrl的格式的。但要求输入obj格式的文件,所以需要转换一下。看了网上的一些教程,然后现在可以做到把点云和面的信息提取出来。纹理信息还不知道如何对应。
wrl格式举例:
#VRML V2.0 utf8
#3Q Technologies Ltd. Copyright 2002. http://www.3q.com
DEF _3Q_object Transform {
children [
Shape {
appearance Appearance {
texture ImageTexture {
url "170716093333.bmp"
repeatS FALSE
repeatT FALSE
}
}
geometry IndexedFaceSet {
ccw TRUE
solid FALSE
convex TRUE
creaseAngle 1.57
coord Coordinate {
point [
-2.73104 -17.342 62.2825,
62.9104 24.8217 -18.5737
]
}
coordIndex [
56240 56352 55946 -1,
56444 56352 56240 -1
]
texCoord TextureCoordinate {
point [
0.866707 0.480934,
0.861055 0.485866
]
}
texCoordIndex [
56240 56352 55946 -1,
59187 60307 60308 -1
]
}
}
]
}
DEF _3Q_Camera_Front Viewpoint {
position 1.79461 27.4489 562.665
orientation 0 0 0 3.14159
fieldOfView 0.5
description "3Q Front"
}
DEF _3Q_Camera_Back Viewpoint {
position 1.79461 27.4489 -472.268
orientation 0 1 0 3.14159
fieldOfView 0.5
description "3Q Back"
}
DEF _3Q_Camera_Left Viewpoint {
position -750.735 27.4489 45.1983
orientation 0 1 0 -1.5708
fieldOfView 0.5
description "3Q Left"
}
DEF _3Q_Camera_Right Viewpoint {
position 754.324 27.4489 45.1983
orientation 0 1 0 1.5708
fieldOfView 0.5
description "3Q Right"
}
DEF _3Q_Camera_Top Viewpoint {
position 1.79461 660.64 45.1983
orientation 1 0 0 -1.5708
fieldOfView 0.5
description "3Q Top"
}
DEF _3Q_Camera_Bottom Viewpoint {
position 1.79461 -605.742 45.1983
orientation 1 0 0 1.5708
fieldOfView 0.5
description "3Q Bottom"
}
可以看到从coord Coordinate开始,先是点云信息(point),然后是面信息(coordIndex)其中面的信息在obj中是从1开始的,我们这份wrl是从0开始的,所以在等下处理时要+1。但是我也不知道其他wrl文件是否需要加1。之后是texCoord TextureCoordinate,里面分别有纹理坐标和信息(现在还不知道怎么对应)。
所以就提了pointcloud和faces的信息,用作后续处理。
贴代码:
# -*- coding: utf-8 -*-
"""
Created on Fri Mar 29 10:23:59 2019
@author: DSY
"""
import numpy as np
import pandas as pd
import glob
import os
#from re import *
import re
#import linecache
#%%
#points
wrls=glob.glob(os.path.join('E:\\3Dface\ZZ_3D','*.wrl'))
outpath="E:\\3Dface\\ZZ_3D\\ZZobj\\"
wrls.sort()
for wrl in wrls:
f=open(wrl,"r")
lines=f.readlines()
f.close()
keywords="coord Coordinate"
for i in range(0,len(lines)):
i+=1
if re.search(keywords, lines[i]):
break
#skip point[
i=i+2
points=[]
keywords2='coordIndex'
for i in range(i,len(lines)):
if re.search(keywords2, lines[i]):
break
points.append(lines[i].split(',')[0].split( ))
points=points[0:-2]
#%%
faces=[]
i=i+2
keywords3='texCoord TextureCoordinate'
for i in range(i,len(lines)):
if re.search(keywords3,lines[i]):
break
faces.append(lines[i].split(',')[0].split( )[0:3])
faces=faces[0:-1]
faces=np.array(faces,dtype=int)+1
#+1来符合obj的face格式
#%%
a=np.zeros((len(points),1),dtype='str')
a[:]='v'
points=np.hstack((a,points))
a=np.zeros((len(faces),1),dtype='str')
a[:]='f'
faces=np.hstack((a,faces))
files=np.vstack((points,faces))
np.savetxt(outpath+wrl.split("\\")[-1].split(".")[0]+'.obj',files,delimiter=' ',fmt='%s')
不打注释了…简单的说就是通过三个关键词来框选定区域,找到pointcloud和faces的信息,然后保存成obj的格式。
obj格式:
v x1 y1 z1
v x2 y2 z2
f i1 i2 i3
f i4 i5 i6
求打赏!!!谢谢老板!