Saturday 15 January 2011

image - Animate text in MATLAB -



image - Animate text in MATLAB -

my simplified problem animate text on 3d plot.

i have cube,

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 possible this?

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

thanks,

the thought utilize texture mapping @hoki showed. tried implement on end, here came with.

first we'll need 6 images apply on cube faces. these can random images of size. example:

% bunch of demo images 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

better yet, let's utilize online service returns placeholder images containing digits 1 6:

% online api placeholder images function imgs = get_images() imgs = cell(6,1); clr = round(255*brighten(lines(6),0.75)); 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

here resulting images:

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

next let's create function renders unit cube images texture-mapped faces:

function h = get_unit_cube(imgs) % need cell array of 6 images, 1 each face (they can size) assert(iscell(imgs) && numel(imgs)==6); % coordinates 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

here looks like:

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

now can have fun creating interesting animations. consider following:

% create 2 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 parents of cubes t1 = hgtransform('parent',gca); t2 = hgtransform('parent',gca); set(h1, 'parent',t1) set(h2, 'parent',t2) % transform sec cube (scaled, rotated, 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 rotating cubes 5 times % (1st rotated around z-axis, 2nd around own z-axis in opposite % direction orbiting 1st) r = linspace(0,10*pi,90) r = makehgtform('zrotate', r); set(t1, 'matrix',r) set(t2, 'matrix',r\(m/r)) pause(0.1) end

i'm using hgtransform function manage transformations, much more efficient continuously changing x/y/z info points of graphics objects.

btw i've used different images in animation above.

edit:

let's replace rotating cubes images of planet earth mapped onto spheres. first here 2 functions render spheres (i'm borrowing code these examples in matlab documentation):

get_earth_sphere1.m function h = get_earth_sphere1() % read images of planet earth earth = imread('landocean.jpg'); clouds = imread('cloudcombined.jpg'); % unit sphere 35x35 faces [x,y,z] = sphere(35); z = flipud(z); = 1.02; % render first sphere earth mapped onto surface, % sec transparent surface 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 info s = load('topo.mat'); c = s.topo; cmap = s.topomap1; n = size(cmap,1); % convert height info , colormap rgb image c = (c - min(c(:))) ./ range(c(:)); % scale [0,1] c = ind2rgb(round(c*(n-1)+1), cmap); % convert indexed rgb % unit sphere 50x50 faces [x,y,z] = sphere(50); % render sphere earth mapped onto surface h = surface(x, y, z, c, ... 'facecolor','texturemap', 'edgecolor','none'); end

the animation script similar before (with minor changes), i'm not gonna repeat it. here result:

(this time i'm using new graphics scheme in r2014b)

image matlab animation text 3d

No comments:

Post a Comment