Texture map for a 2D grid

前端 未结 4 582
名媛妹妹
名媛妹妹 2021-02-06 15:56

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 defor

4条回答
  •  星月不相逢
    2021-02-06 16:48

    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

提交回复
热议问题