How To Create Animated GIF Images in MATLAB?


GIF stands for Graphics Interchange Format. It is a type of image file which is a raster graphics file format, specially used in web and networking applications. The important characteristic of the GIF image format is that it can support both static and animated objects within the image. In this tutorial, I will explain how you can create an animated GIF image using MATLAB.

As we know, MATLAB is an efficient tool to perform various scientific and engineering tasks such as image processing, simulation, etc. We can utilize MATLAB to create animated GIF images.

What is a GIF Image?

A GIF image is a type of image file that can support both static and animated content and uses raster graphics file format. It is mainly used to create simple graphics or animated images because it uses a limited color space of up to 256 colors.

However, this image format utilizes lossless compression technique, hence the GIF images do not loss their quality during compression. There are several other important properties like looping, transparency, and more that the GIF image files support.

The GIF image files have the "*.gif" file extension. However, GIF files are not suitable for applications that require a wide range of colors and gradients like photography.

Now, let us see how we can create animated GIF images using MATLAB.

How to Create Animated GIF Images using MATLAB?

MATLAB is an efficient simulator tool that can be used to create animated GIF image files. In MATLAB, to create an animated GIF image, we have to use a loop to create a sequence of multiple frames.

In MATLAB, the following steps can be followed to create a GIF image:

Step (1) − Define the data that you want to animate.

Step (2) − Define important parameters like file name, number of frames in the image, etc. For example,

file_name = 'image_animation.gif';
frame_num = 30;

Step (3) − Create an empty figure that will be used to display the animation. Do this using the "figure" option.

Step (4) − Create an animation loop.

Step (5) − Once all the frames are created, close the figure to clean up computing resources.

Step (6) − Open and view the GIF image saved in the memory.

This is all about steps involved in creating an animated GIF image using MATLAB. Let us now consider some examples to understand the process practically.

Example (1) − Create an Animated GIF from a Series of Plots

% MATLAB code to create an animated GIF image from a series of plots
% Define the data to animate
x = 0:0.1:5;

% Specify a filename for the animated GIF image
file_name = 'myanimate.gif';

% Define the number of frames for the animation
frame_num = 30;

% Create an empty figure to capture and display the animation
figure;

% Loop through different values of frame number ‘f’ for the animation
for f = 1:frame_num
   % Generate the data for y-axis
   y = x.^f;
    
   % Plot the curve
   plot(x, y);
    
   % Update the figure with the plot
   drawnow;
    
   % Capture the current frame as an image
   frame = getframe(gcf);
   img = frame2im(frame);
    
   % Convert the frame to an indexed image if required
   [imind, cm] = rgb2ind(img, 256);
    
   % Write the frame to the animated GIF image
   if f == 1
      imwrite(imind, cm, file_name, 'gif', 'Loopcount', inf, 'DelayTime', 0.1);
   else
      imwrite(imind, cm, file_name, 'gif', 'WriteMode', 'append', 'DelayTime', 0.1);
   end
end

% Close the figure
close(gcf);

% Display the conformation message
disp(['The GIF image is saved as ' file_name]);

Output

The GIF image is saved as myanimate.gif

Code Explanation

In this MATLAB code, we have a create an animated 2D plot which shown in the above output GIF image. Here, we start by defining the data that we want to animate. Then, we specify the file name and file parameters such as number of frames in the image.

After that, we create an empty figure to capture and display the gif image. Next, we an animation loop to produce the GIF image. Then, we close the file to clean up the resources and display a conformation message that our animated gif is saved.

Let us take another example to create a GIF image having an animated text string.

Example (2) − Create an Animated GIF showing Text String

% MATLAB code to create an animated text GIF image (changing color of text)
% Specify the text string to be animated
text_string = 'Tutorials Point';

% Specify the file name
file_name = 'tp_animation.gif';

% Create an empty figure
figure;

% Initialize color index
color_index = 1;

while true
   % Determine text color based on the color index
   if color_index == 1
      text_color = 'g'; % Green
   elseif color_index == 2
      text_color = 'r'; % Red
   else
      text_color = 'b'; % Blue
   end

   % Plot the text on the figure with the selected color
   text(0.5, 0.5, text_string, 'FontSize', 16, 'HorizontalAlignment', 'center','Color', text_color);

   % Specify that figure is centered and axis limits are fixed
   axis([0 1 0 1]);
   axis off;

   % Capture the current frame as an image
   frame = getframe(gcf);
   img = frame2im(frame);

   % Convert the frame to an indexed image
   [imind, cm] = rgb2ind(img, 256);

   % Set the appropriate 'WriteMode'
   if color_index == 1
   % Overwrite for the first frame
      writemode = 'overwrite';
   else
   % Append for subsequent frames
      writemode = 'append';
   end

   % Write the frame to the animated GIF
   imwrite(imind, cm, file_name, 'gif', 'WriteMode', writemode, 'DelayTime', 0.1);

   % Clear the figure for the next frame
   clf;

   % Update color index for the next frame
   color_index = mod(color_index, 3) + 1;
end

Output

Code Explanation

The implementation of this code is same as that of the previous one. Here, we have specified the animated data instead of plot values. Also, some code lines are added which are described by their corresponding comment lines.

Conclusion

In conclusion, a GIF image is a type of image file that can support both static and animated content. In this tutorial, I have step-by-step explained that how to create animated GIF image using MATLAB. Also, I have added two example programs to understand the steps practically.

Updated on: 10-Oct-2023

152 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements