How to Write Animated PNG (APNG) Files from MATLAB?


I first wrote about APNG files. An APNG file is a valid PNG image file that uses file metadata in a clever way to compress and store the frames of a full animation.

Just recently, I created a MATLAB utility for creating APNG files. This utility makes use of the program APNG Assember, which is available under separate license terms. The utility will automatically download this program for you the first time you use it.

To demonstrate its use, I will use it to create an animation of some pursuit curves that. Here is the code for one version of the pursuit curves:

theta = 0:60:330;
P = cosd(theta) + 1i*sind(theta);
P = reshape(P,1,[]);
N = length(P);
d = 0.01;
for k = 1:1000
    V = P(end,[(2:N) 1]) - P(end,:);
    P(end+1,:) = P(end,:) + d*V;
end
hold on
for k = 1:N
    plot(real(P(:,k)),imag(P(:,k)))
end
hold off
axis equal

This relatively simplistic way to compute the curves doesn't work so well for animating them, however, because the spacing of the computed points gets smaller and smaller as the points get closer to the center. The animation would appear to slow down.

I wrote a little code to resample the generated curve in approximately equal arc-length segments. Here's what that looks like:

step_distance = abs(diff(P(:,1),1));
cumulative_step_distance = [0; cumsum(step_distance,1)];
max_travel_distance = cumulative_step_distance(end);
num_resampled_steps = 10000;
x = linspace(0,max_travel_distance,num_resampled_steps);
Pr = interp1(cumulative_step_distance,P,x);

And here's the code to generate one frame of the animation at a time.

num_frames = 100;
q = round(linspace(1,size(Pr,1),num_frames));

fig = gcf;
fig.Position(3:4) = 300;

frames = zeros(0,0,'uint8');
for k = 1:length(q)
    clf
    hold on
    for v = 1:N
        plot(real(Pr(1:q(k),v)),imag(Pr(1:q(k),v)))
    end
    hold off
    axis(limits)
    axis equal
    axis off
    frame = print(fig,'-r150','-RGBImage');
    frames = cat(4,frames,frame);
end

Here's an extra wrinkle. Although APNG is widely supported by browsers today, there are plenty of programs around that display image files and that don't know about the extra animation metadata in APNG files. I want these viewers to show the last frame of the animation because that's the frame that includes the complete curves. So I will prepend that frame to the beginning, and then I'll tell the APNG utility class to skip the first frame for animations.

frames = cat(4,frame,frames);

% Create the APNG file.
w = animatedPNGWriter('animated-pursuit-curves.png');
w.FramesPerSecond = 20;
w.SkipFirstFrame = true;
for k = 1:size(frames,4)
    addframe(w,frames(:,:,:,k));
end
finish(w);

Matlabsolutions.com provides guaranteed satisfaction with a commitment to complete the work within time. Combined with our meticulous work ethics and extensive domain experience, We are the ideal partner for all your homework/assignment needs. We pledge to provide 24*7 support to dissolve all your academic doubts. We are composed of 300+ esteemed Matlab and other experts who have been empanelled after extensive research and quality check.

Matlabsolutions.com provides undivided attention to each Matlab assignment order with a methodical approach to solution. Our network span is not restricted to US, UK and Australia rather extends to countries like Singapore, Canada and UAE. Our Matlab assignment help services include Image Processing Assignments, Electrical Engineering Assignments, Matlab homework help, Matlab Research Paper help, Matlab Simulink help. Get your work done at the best price in industry.