MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   MATLAB中的纹理映射 (https://www.labfans.com/bbs/showthread.php?t=23231)

poster 2019-12-10 20:30

MATLAB中的纹理映射
 
我有3D空间中的点及其对应的2D图像点。如何在3D点之外制作网格,然后对由网格形成的三角形面进行纹理处理?



[B]回答:[/B]

请注意,您最初尝试使用的函数[URL="https://www.mathworks.com/help/matlab/ref/trisurf.html"]trisurf[/URL]返回了[I]补丁[/I]对象的句柄。如果查看[URL="https://www.mathworks.com/help/matlab/ref/patch-properties.html#property_d0e724248"]补丁对象[/URL]的[URL="https://www.mathworks.com/help/matlab/ref/patch-properties.html#property_d0e724248"]'FaceColor'属性[/URL] ,则可以看到没有'texturemap'选项。该选项仅对[URL="https://www.mathworks.com/help/matlab/ref/primitivesurface-properties.html#property_d0e891374"][I]表面[/I]对象[/URL]的[URL="https://www.mathworks.com/help/matlab/ref/primitivesurface-properties.html#property_d0e891374"]'FaceColor'属性[/URL]有效。因此,您将必须找到一种方法来绘制三角形表面作为[I]表面[/I]对象而不是[I]面[/I] [I]片[/I]对象。这有两种解决方法:

[B]如果您的数据在统一网格中... [/B]

如果表面数据的坐标表示均匀的网格,则z是矩形点集,在x轴上跨度为xmin至xmax ,在y轴上跨度为ymin至ymax ,则可以使用[URL="https://www.mathworks.com/help/matlab/ref/surf.html"]surf[/URL]而不是[URL="https://www.mathworks.com/help/matlab/ref/trisurf.html"]trisurf[/URL] :

Z = ... % N-by-M matrix of data x = linspace(xmin, xmax, size(Z, 2)); % x-coordinates for columns of Z y = linspace(ymin, ymax, size(Z, 1)); % y-coordinates for rows of Z [X, Y] = meshgrid(x, y); % Create meshes for x and y C = imread('image1.jpg'); % Load RGB image h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface (flips rows of C, if needed) 'FaceColor', 'texturemap', ... 'EdgeColor', 'none'); axis equal 为了说明以上代码的结果,我将数据初始化为Z = peaks; ,使用内置的样本图像'peppers.png' ,并将x和y值设置为从1到16。这将导致以下纹理贴图表面:

[URL="https://i.stack.imgur.com/gxZNt.jpg"][IMG]https://i.stack.imgur.com/gxZNt.jpg[/IMG][/URL]

[B]如果您的数据间隔不均匀... [/B]

如果您的数据不是规则间隔的,则可以创建一组规则间隔的X和Y坐标(就像我在上面使用[URL="https://www.mathworks.com/help/matlab/ref/meshgrid.html"]meshgrid[/URL]所做的[URL="https://www.mathworks.com/help/matlab/ref/meshgrid.html"]meshgrid[/URL] ),然后使用函数[URL="https://www.mathworks.com/help/matlab/ref/griddata.html"]griddata[/URL]或[URL="https://www.mathworks.com/help/matlab/ref/triscatteredinterp.html"]TriScatteredInterp[/URL]之一从不规则中插入Z值的规则网格z值集。我在[URL="https://stackoverflow.com/a/1674574/52738"]回答另一个SO问题时[/URL]讨论了如何使用这两个功能。这里是您发布使用代码的改良版本TriScatteredInterp (注:如R2013a的[URL="https://www.mathworks.com/help/matlab/ref/scatteredinterpolant-object.html"]scatteredInterpolant[/URL]是推荐的选择):

x = ... % Scattered x data y = ... % Scattered y data z = ... % Scattered z data xmin = min(x); xmax = max(x); ymin = min(y); ymax = max(y); F = TriScatteredInterp(x(:), y(:), z(:)); % Create interpolant N = 50; % Number of y values in uniform grid M = 50; % Number of x values in uniform grid xu = linspace(xmin, xmax, M); % Uniform x-coordinates yu = linspace(ymin, ymax, N); % Uniform y-coordinates [X, Y] = meshgrid(xu, yu); % Create meshes for xu and yu Z = F(X, Y); % Evaluate interpolant (N-by-M matrix) C = imread('image1.jpg'); % Load RGB image h = surf(X, Y, Z, flipdim(C, 1), ... % Plot surface 'FaceColor', 'texturemap', ... 'EdgeColor', 'none'); axis equal 在这种情况下,必须[I]首先[/I]为矩阵Z的大小选择N和M的值。为了说明上述代码的结果,我按如下所示初始化了x , y和z的数据,并使用了内置的示例图像'peppers.png' :

x = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5 y = rand(1, 100)-0.5; % 100 random values in the range -0.5 to 0.5 z = exp(-(x.^2+y.^2)./0.125); % Values from a 2-D Gaussian distribution 这导致了以下纹理贴图表面:

[URL="https://i.stack.imgur.com/dnKlJ.jpg"][IMG]https://i.stack.imgur.com/dnKlJ.jpg[/IMG][/URL]

请注意,曲面角附近有锯齿状的边缘。在这些地方, TriScatteredInterp点TriScatteredInterp无法完全适合插值曲面。因此,这些点的Z值为[URL="https://www.mathworks.com/help/matlab/ref/nan.html"]nan[/URL] ,导致未绘制表面点。



[url=https://stackoverflow.com/questions/2488086]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 03:00

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.