image - 使用 MATLAB 制作文本动画

标签 image matlab animation text 3d

我的简化问题是在 3D 绘图上为一些文本设置动画。

我有一个立方体,

vert = [1 1 0;0 1 0;0 1 1;1 1 1;0 0 1;1 0 1;1 0 0;0 0 0];
fac =  [1 2 3 4; 4 3 5 6; 6 7 8 5; 1 2 8 7; 6 7 1 4; 2 3 5 8];
patch('Faces',fac,'Vertices',vert,'FaceColor',[.8 .5 .2]); 
axis([0, 1, 0, 1, 0, 1]);
axis equal
axis off

enter image description here

有没有可能得到这样的东西?

enter image description here

使用 text 没有帮助(它看起来是假的!),

谢谢,

最佳答案

想法是使用纹理映射作为 @Hoki showed .我试着在我这边实现这个,这是我想出的。

首先,我们需要将 6 张图像应用于立方体的表面。这些可以是任何大小的任何随机图像。例如:

% just a bunch of demo images from IPT toolbox
function imgs = get_images()
    imgs = {
        imread('autumn.tif');
        imread('coloredChips.png');
        imread('toysflash.png');
        imread('football.jpg');
        imread('pears.png');
        imread('peppers.png');
    };
end

更好的是,让我们使用 online service返回包含数字 1 到 6 的占位符图像:

% online API for placeholder images
function imgs = get_images()
    imgs = cell(6,1);
    clr = round(255*brighten(lines(6),0.75));
    for i=1:6
        %bg = randsample(['0':'9' 'a':'f'], 6, true);
        %fg = randsample(['0':'9' 'a':'f'], 6, true);
        bg = strjoin(cellstr(dec2hex(clr(i,:))).', '');
        fg = strjoin(cellstr(dec2hex(clr(7-i,:))).', '');
        [img,map] = imread(sprintf(...
            'http://placehold.it/100x100/%s/%s&text=%d', bg, fg, i));
        imgs{i} = im2uint8(ind2rgb(img,map));
    end
end

这是生成的图像:

>> imgs = get_images();
>> montage(cat(4,imgs{:}))

digit_images

接下来让我们创建一个函数来渲染一个单位立方体,其中图像纹理映射为面:

function h = get_unit_cube(imgs)
    % we need a cell array of 6 images, one for each face (they can be any size)
    assert(iscell(imgs) && numel(imgs)==6);

    % coordinates for unit cube
    [D1,D2,D3] = meshgrid([-0.5 0.5], [-0.5 0.5], 0.5);

    % texture mapped surfaces
    opts = {'FaceColor','texturemap', 'EdgeColor','none'};
    h = zeros(6,1);
    h(6) = surface(D1, flipud(D2), D3, imgs{6}, opts{:});           % Z = +0.5 (top)
    h(5) = surface(D1, D2, -D3, imgs{5}, opts{:});                  % Z = -0.5 (bottom)
    h(4) = surface(fliplr(D1), D3, flipud(D2), imgs{4}, opts{:});   % Y = +0.5 (right)
    h(3) = surface(D1, -D3, flipud(D2), imgs{3}, opts{:});          % Y = -0.5 (left)
    h(2) = surface(D3, D1, flipud(D2), imgs{2}, opts{:});           % X = +0.5 (front)
    h(1) = surface(-D3, fliplr(D1), flipud(D2), imgs{1}, opts{:});  % X = -0.5 (back)
end

这是它的样子:

imgs = get_images();
h = get_unit_cube(imgs);
view(3), axis vis3d, rotate3d on

unit_cube


现在我们可以通过创建有趣的动画来获得一些乐趣。请考虑以下事项:

% create two separate unit cubes
figure('Renderer','OpenGL')
h1 = get_unit_cube(get_images());
h2 = get_unit_cube(get_images());
set([h1;h2], 'FaceAlpha',0.8)         % semi-transparent
view(3), axis vis3d off, rotate3d on

% create transformation objects, used as parents of cubes
t1 = hgtransform('Parent',gca);
t2 = hgtransform('Parent',gca);
set(h1, 'Parent',t1)
set(h2, 'Parent',t2)

% transform the second cube (scaled, rotated, then shifted)
M = makehgtform('translate', [-0.7 1.2 0.5]) * ...
    makehgtform('yrotate', 15*(pi/180)) * ...
    makehgtform('scale', 0.5);
set(t2, 'Matrix',M)

drawnow
axis on, axis([-2 2 -2 2 -0.7 1]), box on
xlabel x, ylabel y, zlabel z

% create animation by rotating cubes 5 times
% (1st rotated around z-axis, 2nd around its own z-axis in opposite
%  direction as well as orbiting the 1st)
for r = linspace(0,10*pi,90)
    R = makehgtform('zrotate', r);
    set(t1, 'Matrix',R)
    set(t2, 'Matrix',R\(M/R))
    pause(0.1)
end

orbiting_animation

我正在使用 hgtransform管理转换的功能,这是much more efficient而不是不断改变图形对象的 x/y/z 数据点。

顺便说一句,我在上面的动画中使用了稍微不同的图像。


编辑:

让我们用映射到球体上的地球图像替换旋转的立方体。首先,这里有两个函数来渲染球体(我从 MATLAB 文档中的 these examples 借用代码):

get_earth_sphere1.m

function h = get_earth_sphere1()
    % read images of planet earth
    earth = imread('landOcean.jpg');
    clouds = imread('cloudCombined.jpg');

    % unit sphere with 35x35 faces
    [X,Y,Z] = sphere(35);
    Z = flipud(Z);
    a = 1.02;

    % render first sphere with earth mapped onto the surface,
    % then a second transparent surface with clouds layer
    if verLessThan('matlab','8.4.0')
        h = zeros(2,1);
    else
        h = gobjects(2,1);
    end
    h(1) = surface(X, Y, Z, earth, ...
        'FaceColor','texturemap', 'EdgeColor','none');
    h(2) = surface(X*a, Y*a, Z*a, clouds, ...
        'FaceColor','texturemap', 'EdgeColor','none', ...
        'FaceAlpha','texturemap', 'AlphaData',max(clouds,[],3));
end

get_earth_sphere2.m

function h = get_earth_sphere2()
    % load topographic data
    S = load('topo.mat');
    C = S.topo;
    cmap = S.topomap1;
    n = size(cmap,1);

    % convert altitude data and colormap to RGB image
    C = (C - min(C(:))) ./ range(C(:));   % scale to [0,1]
    C = ind2rgb(round(C*(n-1)+1), cmap);  % convert indexed to RGB

    % unit sphere with 50x50 faces
    [X,Y,Z] = sphere(50);

    % render sphere with earth mapped onto the surface
    h = surface(X, Y, Z, C, ...
        'FaceColor','texturemap', 'EdgeColor','none');
end

动画脚本与之前类似(略有改动),不再赘述。这是结果:

planets

(这次我使用的是 new graphics system in R2014b )

关于image - 使用 MATLAB 制作文本动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26717775/

相关文章:

filter - 我如何自己编写 Matlab "filter"函数?

android - Activity 退出时的 RecyclerView 动画

javascript - 如何为 SVG 多边形点设置动画?

ios - iOS中如何实现 "clock wipe"/径向删除效果?

java - 加载图像到 JPanel

python - Python OpenCV 绑定(bind)中的 copyTo 是否等效?

Android:由于某些布局属性,ImageView 高度限制

algorithm - 如何避免在我的 while 循环中不必要地检查相同的 if 语句?

java - 为什么我的面板/窗口中没有图片?

matlab - 解析 '[' : usage might be invalid MATLAB syntax时出错