Sunday, 3 July 2016

matlab - Texture map for a 2D grid



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?


Answer



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




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);


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);



Update:



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')


And this is the result:




Plot of original and deformed texture


No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...