在MATLAB动画文本 [英] Animate text in MATLAB

查看:346
本文介绍了在MATLAB动画文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的简单的问题是要设置动画的3D绘图一些文本。

My simplified problem is to animate some text on a 3D plot.

我有一个立方体,

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

是否有可能得到这样的事情?

Is it possible to get something like this?

使用文本并不能帮助(它看起来假的!),

Using text doesn't help (it looks fake!),

谢谢

推荐答案

我们的想法是使用纹理贴图作为 @Hoki显示。我试图实现这个我的结束,这里是我想出了。

The idea is to use texture mapping as @Hoki showed. I tried to implement this on my end, here is what I came up with.

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

First we'll need 6 images to apply on the cube faces. These can be any random images of any size. For example:

% 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

更重要的是,让我们使用它返回占位符图像包含数字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{:}))

接下来让我们创建一个函数,使一个单位立方体的图像纹理贴图的面孔:

Next let's create a function that renders a unit cube with images texture-mapped as faces:

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

现在,我们可以有这个创造有趣的动画一番情趣。考虑以下几点:

Now we can have some fun with this creating interesting animations. Consider the following:

% 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

我使用的是 hgtransform 功能,可管理的转换,这是更有效比连续改变X / Y的图形对象的/ Z数据点

I'm using the hgtransform function to manage transformations, this is much more efficient than continuously changing the x/y/z data points of the graphics objects.

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

BTW I've used slightly different images in the animation above.

让我们与地球的映射到球图片更换旋转立方体。首先这里有两个功能,使球体(我借用code从<一个href="http://www.mathworks.com/help/matlab/examples/changing-transparency-of-images-patches-or-surfaces.html#zmw57dd0e6118">these <一href="http://www.mathworks.com/help/matlab/examples/displaying-topographic-data.html#zmw57dd0e5031">examples在MATLAB文件):

Let's replace the rotating cubes with images of planet earth mapped onto spheres. First here are two functions to render the spheres (I'm borrowing code from these examples in the MATLAB documentation):

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

动画脚本类似于前(轻微改变),所以我不会重复。下面是结果:

The animation script is similar to before (with minor changes), so I'm not gonna repeat it. Here is the result:

(这次我使用的是 href="http://www.mathworks.com/products/matlab/matlab-graphics/">新的图形系统)

(This time I'm using the new graphics system in R2014b)

这篇关于在MATLAB动画文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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