如何使用纹理贴图绘制曲面 [英] How to plot a surface with a texture map

查看:293
本文介绍了如何使用纹理贴图绘制曲面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想绘制一个带有纹理贴图的曲面,但条件不是理想曲面。

I want to plot a surface with a texture map on it, but the conditions are not the "ideal" ones.

首先让我们解释一下。

first lets explain what I have.

我有一组点(~7000),它们是网格中的图像坐标。 这一点不能定义完美的正方形。 它不是MESHGRID 。为了这个问题,让我们假设我们有9分。让我们用图像说明我们的内容:

I have a set of points (~7000) that are image coordinates, in a grid. This points do NOT define perfect squares. IT IS NOT A MESHGRID. For the sake of the question, lets assume that we have 9 points. Lets ilustrate what we have with an image:

 X=[310,270,330,430,410,400,480,500,520]
 Y=[300,400,500,300,400,500,300,400,500]


假设我们可以得到网格的结构,所以

Lets say we can get the "structure" of the grid, so

 size1=3;
 size2=3;
 points=zeros(size1,size2,2)
 X=[310,270,330;
    430,410,400;
    480,500,520]
 Y=[300,400,500;
    300,400,500;
    300,400,500]
 points(:,:,1)=X;
 points(:,:,2)=Y;

现在我们假设我们有第三维,Z。

And now lets say we have a 3rd dimension, Z.

编辑:忘记添加一条信息。我对图像中的点进行三角测量并获得3D对应关系,因此当在表面中显示时,它们没有图像的X和Y坐标,为了简化给定数据,可以说X = X / 2 Y = Y / 3

Forgot to add a piece if info. I triangulate the points in the image and get a 3D correspondence, so when displayed in a surface they don't have the X and Y coords of the image, for a simplification of the given data lets say X=X/2 Y=Y/3

我们有:

 points=zeros(size1,size2,3)
 Z=[300,330,340;
    300,310,330;
    290,300,300]

 surf(points(:,:,1)/2,points(:,:,2)/3,points(:,:,3))

我想要的是用图像纹理在3D中绘制表面。每个元素都应该具有第一张图像中的纹理片段。

What I want is to plot the surface in 3D with the image texture. Each element should have the texture piece that have in the first image.

这需要适用于大型数据表。我并不特别需要快速。

This needs to work for huge datasheets. I don't specially need it to be fast.

相关帖子(但我有一个网格网格作为初始点集): 2D网格的纹理贴图

related post (but I has a meshgrid as initial set of points) : Texture map for a 2D grid

PD 我可以根据需要发布原始图片+真实数据,只是张贴了这个,因为我认为使用小数据更容易。

PD: I can post original images + real data if needed, just posted this because i think it is easier with small data.

推荐答案

我认为你不能用Matlab的内置命令和功能做你想做的事。但是使用我的其他答案中的技术和高分辨率版本的网格可以为您完成。

I don't think you can do what you want with Matlab's built in commands and features. But using the technique from my other answer with a high-res version of the grid can do it for you.

high-res是指具有更密集数据点的非均匀网格的插值版本。这用于在更密集的数据点处对纹理进行采样,因此可以使用 surf texturemap 功能进行绘制。但是,您不能使用普通的2D插值,因为您需要保留不均匀的网格形状。这就是我提出的:

By "high-res", I mean an interpolated version of the non-uniform grid with denser data points. That is used to sample the texture at denser data points so it can be drawn using the texturemap feature of surf. You can't use a normal 2D interpolation, however, because you need to preserve the non-uniform grid shape. This is what I came up with:

function g = nonUniformGridInterp2(g, sx, sy)

[a,b] = size(g);
g = interp1(linspace(0,1,a), g, linspace(0,1,sy)); % interp columns
g = interp1(linspace(0,1,b), g', linspace(0,1,sx))'; % interp rows

请注意,您必须调用此方法两次才能独立插入X和Y点。这是原始网格和每个方向上有10个点的插值版本的示例。

Note that you have to call this twice to interpolate the X and Y points independently. Here's an example of the original grid and an interpolated version with 10 points in each direction.

以下是如何使用 interp2的高分辨率网格 texturemap

function nonUniformTextureMap

% define the non-uniform surface grid
X = [310,270,330; 430,410,400; 480,500,520];
Y = [300,400,500; 300,400,500; 300,400,500];
Z = [300,330,340; 300,310,330; 290,300,300];

% get texture data
load penny % loads data in variable P

% define texture grid based on image size
% note: using 250-550 so that a,b covers the range used by X,Y
[m,n] = size(P);
[a,b] = meshgrid(linspace(250,550,n), linspace(250,550,m));

% get a high-res version of the non-uniform grid
s = 200; % number of samples in each direction
X2 = nonUniformGridInterp2(X, s, s);
Y2 = nonUniformGridInterp2(Y, s, s);

% sample (map) the texture on the non-uniform grid
C = interp2(a, b, P, X2, Y2);

% plot the original and high-res grid
figure
plot(X(:),Y(:),'o',X2(:),Y2(:),'.')
legend('original','high-res')

% plot the surface using sampled points for color
figure
surf(X, Y, Z, C, 'edgecolor', 'none', 'FaceColor','texturemap')
colormap gray

这篇关于如何使用纹理贴图绘制曲面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆