【三维重建】之 matlab读取 .wrl 三维图像文件

matlab读取 .wrl 三维图像文件

1. wrl 文件

  • wrl(虚拟现实语言): wrl文件是一种虚拟现实文本格式文件。也是VRML的场景模型文件的扩展名。
  • wrl文件是纯ASCII文件,所以可以用文本编辑器打开和编辑。虽然这样,但是对于大型的3D文件。用纯手工的方法是不可取的。应该选择用三维软件(maya,max等)进行场景建模和输出。
  • 可通过SolidWorks制作导出
  • 可通过matlab打开可视化

例如scene.wrl 文件,其内容如下:

#VRML V2.0 utf8
Transform {
	children [
		DEF model1 Transform {
			children [
				DEF body1 Transform {
					translation -1 0 0
					children [
						DEF shape1 Shape {
							appearance Appearance {
								material Material {
									diffuseColor 1.0 1.0 0.0
								}
							}
							geometry Box {
								size 1.5 1.5 1.5
							}
						}
					]
				}
			]
		}
		DEF model2 Transform {
			children [
				DEF body2 Transform {
					translation 1 0 0
					children [
						DEF shape2 Shape {
							appearance Appearance {
								material Material {
									diffuseColor 1.0 1.0 0.0
								}
							}
							geometry Box {
								size 1.5 1.5 1.5
							}
						}
					]
				}
			]
		}
	]
}

2. matlab打开.wrl文件

%********************打开.wrl文件*************
vrsetpref('DefaultViewer', 'internalv5')
w = vrworld('scene.wrl');
open(w);
view(w);

在这里插入图片描述

3. matlab绘制wrl内的三维坐标点

txt格式打开.wrl文件,直接复制点集坐标到matlab代码中,并在每行坐标后边添加...

%绘制三维点
%%base_link
P=[ -0.075000 -0.100000 0.000000, -0.075000 -0.100000 0.570000, -0.075000 -0.075000 0.436008, -0.075000 -0.073559 0.421376, -0.075000 -0.073559 0.450639,...
 -0.075000 -0.069291 0.407306, -0.075000 -0.069291 0.464709, -0.075000 -0.062360 0.394340, -0.075000 -0.062360 0.477675, -0.075000 -0.053033 0.382975,...
 -0.075000 -0.053033 0.489041, -0.075000 -0.041668 0.373647, -0.075000 -0.041668 0.498368, -0.075000 -0.028701 0.366717, -0.075000 -0.028701 0.505299,...
 -0.075000 -0.014632 0.362449, -0.075000 -0.014632 0.509567, -0.075000 0.000000 0.361008, -0.075000 0.000000 0.511008, -0.075000 0.014632 0.362449,...
 -0.075000 0.014632 0.509567, -0.075000 0.028701 0.366717, -0.075000 0.028701 0.505299, -0.075000 0.041668 0.373647, -0.075000 0.041668 0.498368,...
 -0.075000 0.053033 0.382975, -0.075000 0.053033 0.489041, -0.075000 0.062360 0.394340, -0.075000 0.062360 0.477675, -0.075000 0.069291 0.407306,...
 -0.075000 0.069291 0.464709, -0.075000 0.073559 0.421376, -0.075000 0.073559 0.450639, -0.075000 0.075000 0.436008, -0.075000 0.100000 0.000000,...
 -0.075000 0.100000 0.570000, -0.025000 -0.075000 0.436008, -0.025000 -0.073559 0.421376, -0.025000 -0.073559 0.450639, -0.025000 -0.069291 0.407306,...
 -0.025000 -0.069291 0.464709, -0.025000 -0.062360 0.394340, -0.025000 -0.062360 0.477675, -0.025000 -0.053033 0.382975, -0.025000 -0.053033 0.489041,...
 -0.025000 -0.041668 0.373647, -0.025000 -0.041668 0.498368, -0.025000 -0.028701 0.366717, -0.025000 -0.028701 0.505299, -0.025000 -0.014632 0.362449,...
 -0.025000 -0.014632 0.509567, -0.025000 0.000000 0.361008, -0.025000 0.000000 0.436008, -0.025000 -0.000000 0.511008, -0.025000 0.014632 0.362449,...
 -0.025000 0.014632 0.509567, -0.025000 0.028701 0.366717, -0.025000 0.028701 0.505299, -0.025000 0.041668 0.373647, -0.025000 0.041668 0.498368,...
 -0.025000 0.053033 0.382975, -0.025000 0.053033 0.489041, -0.025000 0.062360 0.394340, -0.025000 0.062360 0.477675, -0.025000 0.069291 0.407306,...
 -0.025000 0.069291 0.464709, -0.025000 0.073559 0.421376, -0.025000 0.073559 0.450639, -0.025000 0.075000 0.436008, 0.075000 -0.100000 0.000000,...
 0.075000 -0.100000 0.570000, 0.075000 0.100000 0.000000, 0.075000 0.100000 0.570000 ]

i=1
while i<length(P)
    scatter3(P(i),P(i+1),P(i+2));
    hold on
    i=i+3;
end

在这里插入图片描述

4. 对三维点进行空间旋转变换

%旋转矩阵:
%绕x\y\z轴旋转,输入弧度
function [R]=getR(a,flg)
    R=[]
    if(flg==0)
        R=[ 1  0  0;
        0  cos(a) -sin(a);
        0  sin(a)  cos(a)];
    elseif(flg==1)
        R=[ cos(a)  0  sin(a);
        0  1  0 ;
        -sin(a)  0  cos(a)];
    elseif(flg==2)
        R=[ cos(a) -sin(a)  0;
        sin(a)  cos(a)  0  ;
        0  0  1];
    end
end

接标题3中的点集格式如下计算:

rx=getR(-pi/2,0);
points0=points*rx

ry=getR(-pi/2,1);
points1=points*ry

rz=getR(-pi/2,2)
points2=point*rz
这是一个wrl文件可以用来学习vrml #VRML V2.0 utf8 EXTERNPROTO BlaxxunZone [ eventIn MFNode addEvents eventIn MFNode removeEvents exposedField MFNode events ][ "shared.wrl#BlaxxunZone", "http://www.blaxxun.com/vrml/protos/shared.wrl#BlaxxunZone" ] EXTERNPROTO SharedEvent [ exposedField SFString name # for accessing a special event eventIn SFColor colorFromServer eventOut SFColor colorToServer eventIn SFColor set_color eventOut SFColor color_changed eventIn SFRotation rotationFromServer eventOut SFRotation rotationToServer eventIn SFRotation set_rotation eventOut SFRotation rotation_changed eventOut SFString string_changed eventIn SFString stringFromServer ] [ "shared.wrl#SharedEvent", "http://www.blaxxun.com/vrml/protos/shared.wrl#SharedEvent" ] DEF SharedZone BlaxxunZone { events [ DEF SharedColor SharedEvent { name "newColor" } DEF SharedRotation SharedEvent { name "newRotation" } ] } DEF BoxSwitch Switch { whichChoice 0 choice [ DEF BoxRotation Transform { children [ Shape { # 红色立方体 appearance Appearance { material DEF BoxColor Material { diffuseColor 1 0 0 } } geometry Box { } } ] } ] } # 此球体用来更换颜色 Transform { translation -4 0 0 children [ Shape { appearance Appearance { material Material { diffuseColor 0 0 1 } } geometry Sphere {} }, DEF ColorSensor TouchSensor {}, DEF ColorScript Script { eventIn SFTime clicked eventIn SFString changeColor eventOut SFColor color_changed eventOut SFColor color_changed_from_bot url "vrmlscript: function clicked (value, time) { color_changed = new SFColor(Math.random(),Math.random(),Math.random()); } function changeColor (value, time) { tempColor = new SFColor(1,0,0); temp = new SFString(value); pos1 = 7; for (i=0; i<3;i++){ temp2 = new SFString(temp.substring(pos1,pos1+1)); if (temp2 == '1') tempColor[i] = 1; pos2 = temp.indexOf(' ',pos1); pos1 = pos2+1; } color_changed_from_bot = tempColor; } " } ] } # 此锥体用来旋转更换 Transform { translation 4 0 0 children [ Shape { appearance Appearance { material Material { diffuseColor 0 0 1 } } geometry Cone {} }, DEF RotSensor TouchSensor {}, DEF RotationScript Script { field MFString newurl [ "OnEvent( par1, par2 )" ] # field MFString newurl [ "javascript:OnJSEvent( )" ] field MFString param [ "" "" ] eventIn SFTime clicked eventIn SFTime set_string eventOut SFRotation rotation_changed url "vrmlscript: function clicked (value, time) { angle = Math.random()*6.283; rotation_changed = new SFRotation(0,1,0,angle); } function set_string (value, time) { newurl[0] = 'OnEvent ( ' + 'your par1 ' + ',' + 'your par2' + ')'; Browser.loadURL (newurl,param); } " } ] } Transform { translation 0 -3 0 children [ DEF ChangeString Text { string "ChangeString" } ] } Script { url "vrmlscript: function initialize() { }" } #ROUTE RotSensor.touchTime TO RotationScript.clicked ROUTE RotSensor.touchTime TO RotationScript.set_string #ROUTE RotationScript.rotation_changed TO SharedRotation.set_rotation ROUTE RotationScript.rotation_changed TO BoxRotation.set_rotation ROUTE SharedRotation.rotation_changed TO BoxRotation.rotation ROUTE ColorSensor.touchTime TO ColorScript.clicked ROUTE ColorScript.color_changed TO SharedColor.set_color ROUTE SharedColor.color_changed TO BoxColor.diffuseColor ROUTE ColorScript.color_changed_from_bot TO BoxColor.diffuseColor ROUTE SharedColor.string_changed TO ColorScript.changeColor
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

明月醉窗台

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值