MATLAB中的纹理映射 [英] Texture mapping in MATLAB
问题描述
我在3D空间中有一些点及其对应的2D图像点.如何在3D点之外制作网格,然后对由网格形成的三角形面进行纹理处理?
I have points in 3D space and their corresponding 2D image points. How can I make a mesh out of the 3D points, then texture the triangle faces formed by the mesh?
推荐答案
请注意,函数 'FaceColor'
属性,您可以看到没有'texturemap'
选项.该选项仅对 'FaceColor'
属性有效>表面对象.因此,您将必须找到一种方法将三角形表面绘制为 surface 对象而不是 patch 对象.这有两种解决方法:
Note that the function trisurf
that you were originally trying to use returns a handle to a patch object. If you look at the 'FaceColor'
property for patch objects, you can see that there is no 'texturemap'
option. That option is only valid for the 'FaceColor'
property of surface objects. You will therefore have to find a way to plot your triangular surface as a surface object instead of a patch object. Here are two ways to approach this:
如果表面数据的坐标表示一个均匀的网格,使得z
是矩形点集,则在x轴上跨度从xmin
到xmax
,在x轴上跨度从ymin
到ymax
y轴,您可以使用 surf
进行绘制 trisurf
:
If the coordinates of your surface data represent a uniform grid such that z
is a rectangular set of points that span from xmin
to xmax
in the x-axis and ymin
to ymax
in the y-axis, you can plot it using surf
instead of trisurf
:
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.这样产生了以下纹理贴图表面:
In order to illustrate the results of the above code, I initialized the data as Z = peaks;
, used the built-in sample image 'peppers.png'
, and set the x
and y
values to span from 1 to 16. This resulted in the following texture-mapped surface:
如果您的数据不是规则间隔的,则可以创建一组规则间隔的X
和Y
坐标(就像我在上面使用 griddata
或我对另一个SO问题的回答中讨论了如何使用这两个功能.这是您使用TriScatteredInterp
发布的代码的精炼版本(注意:从R2013a开始,
If your data are not regularly spaced, you can create a set of regularly-spaced X
and Y
coordinates (as I did above using meshgrid
) and then use one of the functions griddata
or TriScatteredInterp
to interpolate a regular grid of Z
values from your irregular set of z
values. I discuss how to use these two functions in my answer to another SO question. Here's a refined version of the code you posted using TriScatteredInterp
(Note: as of R2013a scatteredInterpolant
is the recommended alternative):
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
在这种情况下,必须先 为矩阵Z
的大小选择N
和M
的值.为了说明上述代码的结果,我按如下所示初始化了x
,y
和z
的数据,并使用了内置示例图像'peppers.png'
:
In this case, you have to first choose the values of N
and M
for the size of your matrix Z
. In order to illustrate the results of the above code, I initialized the data for x
, y
, and z
as follows and used the built-in sample image '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
这将导致以下纹理贴图表面:
This resulted in the following texture-mapped surface:
请注意,表面拐角附近存在锯齿状边缘.在这些地方,TriScatteredInterp
的点太少,不足以完全适合插值曲面.因此,这些位置的Z
值为 nan
,导致未绘制表面点.
Notice that there are jagged edges near the corners of the surface. These are places where there were too few points for TriScatteredInterp
to adequately fit an interpolated surface. The Z
values at these points are therefore nan
, resulting in the surface point not being plotted.
这篇关于MATLAB中的纹理映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!