Texture map for a 2D grid

前端 未结 4 641
名媛妹妹
名媛妹妹 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:47

    EDIT:

    Here is an example of texture mapping using the surface function:

    %# image and 2D grid of points of the same size as the image
    img = load('clown');    %# indexed color image
    [m,n] = size(img.X);
    [a,b] = meshgrid(1:n,1:m);
    
    %# initial grid (here 1/5-th the size, but could be anything)
    [X,Y] = meshgrid(linspace(1,n,n/5),linspace(1,m,m/5));
    
    %# resize image to fit this grid
    [C,map] = imresize(img.X, img.map, size(X), 'bicubic');
    
    %# deformed 2D points (we dont need to know f, just load U/V here)
    fx = @(x,y) sqrt(x);
    fy = @(x,y) y.^2;
    U = fx(X,Y);
    V = fy(X,Y);
    
    %# Z-coordinates: I'm using Z=0 for all points, but could be anything
    Z = zeros(size(U));
    %Z = peaks(max(size(U))); Z = Z(1:size(U,1),1:size(U,2)); view(3)
    
    %# show image as texture-mapped surface
    surface(U, V, Z, C, 'CDataMapping','direct', ...
        'FaceColor','texturemap', 'EdgeColor','none')
    colormap(map)
    axis ij tight off
    view(2)
    

    pic

    An alternative to using imresize is explicit interpolation (idea borrowed from @shoelzer):

    CC = ind2rgb(img.X, img.map);    %# convert to full truecolor
    C = zeros(size(X));
    for i=1:size(CC,3)
        C(:,:,i) = griddata(a,b, CC(:,:,i), X,Y, 'linear');
    end
    

    of course with this change, there is no need for the colormap/CDataMapping anymore...

    (Note: I think interp2 would be much faster here)


    Note that in the example above, I'm using an indexed color image with an associated colormap. The code can easily be adapted to work on grayscale or truecolor full RGB images. See this page for an explanation on the different image types.

提交回复
热议问题