| by Arround The Web | No comments

How to Create Video from an Image Using MATLAB

MATLAB is a programming as well as numeric computing framework used by engineers for the analysis of data, algorithm development, and model creation. Images can be manipulated in MATLAB using image processing techniques. Images are numeric arrays that can be used in performing analysis.

Most images are represented as two-dimensional arrays or matrices which have each of its elements corresponding to a pixel.

Images such as RGB require three-dimensional representation as it has three channels Red, Green, and Blue. Different formats are used to support images and their graphical files. Once the format image is displayed, it becomes the image object. The graphical file formats are:

    • BMP(Bitmap)
    • GIF(Graphics Interchange Files)
    • HDF (Hierarchical Data Format)
    • JPEG (Joint Photographic Experts Group)
    • PCX (Paintbrush)
    • PNG (Portable Network Graphics)
    • TIFF (Tagged Image File Format)
    • XWD (X Window Dump)

The interface of MATLAB is as follows:


The workspace consists of variables that are created during programming or are imported from data files or other programs. We can edit it in the Workspace browser or the Command Window.

The Editor is used to write codes. By pressing the RUN button, the currently written code inside the script file will run. It is a live editor which means that you can make changes on run time, and it makes it easier for you or anyone else to understand code. We can create as many notebooks as possible, and we can use them by importing them into other files. To run a file first save it inside the MATLAB directory.

The Command Window makes use of prompt (>>) to enter individual statements and execute them right away. If your editor is showing an error on a specific line, then you can write that line in the command window to show where the error is and correct it. If you write a=1 in the command window, then it will create a new variable in Workspace.

The current folder is a location-finding folder or in other words the folder for finding the reference location. It is used to find files. It mentions highlighting the current folder where we are working and creates a hierarchy of folders by branching them. To access any folder just simply click on it.

Making Video from Images in MATLAB

To make a video from images, I have selected multiple MATLAB logo images. Following are the images that I have selected:


These 5 images of MATLAB logos are of different sizes and shapes. I used imread() to read each of them and stored each of the 5 in individual variables such as image1, image2, etc. I have used (.png) to save them. PNG formatting helps in a high-quality display of digital images, and they have lossless compression as well as a very broad color palette. They are easily manipulatable when it comes to image processing and most of the work is done upon them.

Image Shapes

Video making requires all images to be of the same size i.e., width and length for which I used imresize() function to resize them to 628 by 428 as it is a standard size of images required for videos. Resizing is followed by saving where using saveas() function each of the images is saved individually and replaces the images in the folder. Images are saved as numbers e.g., 1.png, 2.png, etc. as they are easily accessible in such a way.

Video Writer

VideoWriter() function is used to make a video. It constructs an object in which we write data to a file that uses Motion JPEG Compression. The first argument is the video_name.mp4 by which the video is saved in the folder.

MPEG-4 is both Windows and iOS compatible files that can be used in both software. We can change its framerate which determines the rate at which the frames will move i.e., the playback speed at which frames will move per second. These frames are individual images.

We will open this object which is writerObj in our case and use a For loop to the number of images. Opening it makes it accessible to write videodata. Upon running the loop, we will read every image stored in the folder in every For-loop iteration using imread(). The num2str() will convert all numbers to string and the PNG extension is used as images are in the same extension. The strcat() will concatenate the image name to the .png extension.

We will then convert every image to a video frame using im2frame() function. This is our current frame. It is then used in the writeVideo() function which takes the object of VideoWriter and writes each image in every loop iteration as a video frame, and this will continue till the end of the loop and in this way, a video is formed.

The code is as follows:

% load the images
%  images    = cell(4,1);
 image1 = imread('1.png');
 image2 = imread('2.png');
 image3 = imread('3.png');
 image4 = imread('4.png');
 image5 = imread('5.png');
 imshow(image3)
%   % create the video writer with 1 fps
 image1 = imresize(image1 , [468 628]);
 image2 = imresize(image2 , [468 628]);
 image3 = imresize(image3 , [468 628]);
 image4 = imresize(image4 , [468 628]);
 image5 = imresize(image5 , [468 628]);
 %  image3 = imresize(image3,size(image2));
tt=imshow(image1);
saveas(tt,'C:\Users\Kashif Javed\Documents\MATLAB\1.png');
tt=imshow(image2);
saveas(tt,'C:\Users\Kashif Javed\Documents\MATLAB\2.png');
tt=imshow(image3);
saveas(tt,'C:\Users\Kashif Javed\Documents\MATLAB\3.png');
tt=imshow(image4);
saveas(tt,'C:\Users\Kashif Javed\Documents\MATLAB\4.png');
tt=imshow(image5);
saveas(tt,'C:\Users\Kashif Javed\Documents\MATLAB\5.png');
   
 writerObj = VideoWriter('myVideo.mp4','MPEG-4');
 writerObj.FrameRate = 0.5;
 % open the video writer
 open(writerObj);
 % write the frames to the video
 for u=1:5
     % convert the image to a frame
     a = imread(strcat(num2str(u),'.png'));
     currframe = im2frame(a);
     writeVideo(writerObj,currframe);

 end
 % close the writer object
 close(writerObj);

 
The video’s name is myvideo.mp4. We must close the object of VideoWriter at the end to enclose the video writing process so that MATLAB could know that we have ended our writing process.


The following video will be created inside the same directory as the current MATLAB file.

Conclusion

MATLAB is a tool where we can manipulate images and it is used for video making using images. Every image is read using the imread() function which is then resized and stored in place of the original files. The images are then converted into frames and written in the VideoWriter object (after opening it) where the frame rate is set as 0.5 which is the speed at which frames move per second. All this writing and framing of images is done in a For loop. The VideoWriter object is then closed, and the video is saved as a .mp4 file.

Share Button

Source: linuxhint.com

Leave a Reply