2D网格的纹理贴图 [英] Texture map for a 2D grid

查看:121
本文介绍了2D网格的纹理贴图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在常规2D,N x M网格上定义了一组点[x,y]=meshgrid(1:N,1:M).我还有另一组点[u,v],它们是原始网格的某些变形,即[u,v]=f(x,y)'(但是我没有导致变形的实际f).如何将纹理映射到u,v定义的变形"网格?即,给定具有纵横比N/M的图像,如何将其映射到变形的网格?

I have a set of points [x,y]=meshgrid(1:N,1:M) defined on a regular 2D, N x M grid. I have another set of points [u,v] that are some deformation of the original grid, i.e [u,v]=f(x,y)' (however I do not have the actual f that caused the deformation). How can I map a texture to the "deformed" grid defined by u,v? i.e., given an image with aspect-ratio N/M how can I map it to the deformed grid?

推荐答案

我想您是想在[u,v]处获取原始纹理的样本.您可以使用 interp2 .

I think you are asking to get samples of the original texture at [u,v]. You can use interp2.

比方说,纹理样本位于z中,而您想要新的样本z2.要在[u,v]处插入原始纹理,请使用:

Let's say that the texture samples are in z and you want new samples z2. To interpolate the original texture at [u,v], use:

z2 = interp2(x,y,z,u,v);

另一方面,如果要将变形的"纹理映射回规则间隔的网格[x2,y2],请使用

On the other hand, if you want to map the "deformed" texture back to a regularly spaced grid [x2,y2], use griddata:

[x2,y2] = meshgrid(1:N2,1:M2);
z2 = griddata(u,v,z,x2,y2);

更新:

下面是一些示例代码,显示了如何对真实数据执行此操作.使用归一化坐标会更容易.

Here's some example code showing how to do this with real data. Using normalized coordinates makes it easier.

% get texture data
load penny
z = P;

% define original grid based on image size
[m,n] = size(z);
[a,b] = meshgrid(linspace(0,1,n), linspace(0,1,m));

% define new, differently sized grid
m2 = 256;
n2 = 256;
[x,y] = meshgrid(linspace(0,1,n2), linspace(0,1,m2));

% define deformed grid
u = sqrt(x);
v = y.^2;

% sample the texture on the deformed grid
z2 = interp2(a,b,z,u,v);

% plot original and deformed texture
figure
subplot(2,1,1)
surface(a,b,z,'EdgeColor','none')
axis ij image off
colormap gray
title('original')
subplot(2,1,2)
surface(x,y,z2,'EdgeColor','none')
axis ij image off
colormap gray
title('deformed')

这是结果:

这篇关于2D网格的纹理贴图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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