Skip to content Skip to sidebar Skip to footer

No More Frames Available to Read From File. Matlab

               This tutorial will come up in handy if you are interested in learning about video processing using MATLAB. Techniques such as Reading and writing a video file, displaying the frames and writing the frames as images in a folder are discussed below.

To read a video file:

Vptr = VideoReader( 'VIDE01.mp4' )

'VideoReader' command in MATLAB creates reader object for the video file. This object contains the metadata or the parameters related to the video such as Frame rate, Elevation and Width of the frames,duration of the video etc.

To read all the frames in the video, we can use 2 methods. The first method is to observe the number of frames in the video and read it. The second method is to read the frames until no more video frames are available. Here in the tutorial, the frame charge per unit is assumed to be constant throughout the duration of the video. At constant frame charge per unit, the number of frames in the video is obtained past straight multiplication of frame charge per unit and video duration. In our example, the video is xiii.29 seconds long and the frame charge per unit is 24.0. Multiplying thirteen.29 and 24 gives 318.96 that is 319 frames bachelor in the video.

1. To Read the frames in the video , brandish , save every bit image file and store as mat file

%MATLAB Lawmaking:

Vptr = VideoReader( 'VIDE01.mp4' );

%Num_Frames = Vptr.NumberOfFrames;

NFrames = circular(Vptr.FrameRate*Vptr.Elapsing);

%Notice the height and weight of the frame

Nrows = Vptr.height;

Ncols = Vptr.width;

%Preallocate the matrix

Img_s = zeros([Nrows,Ncols,NFrames]);

for i = 1:NFrames

%Read each frame

Img = readFrame(Vptr);

%To display all the frames

effigy,imshow(Img);

%To save the images

Img_name=[ 'Image' ,num2str(i), '.jpg' ];

imwrite(Img,Img_name);

%To shop in MAT file

Img_s(:,:,i)=Img;

end

%Save the matrix as .mat file

Salvage Video_Images.mat Img_s ;

Explanation:

The above MATLAB lawmaking can

a. Display all the frames in the video

b. Save all the frames as images in the current working directory

c. Store all the frames every bit images in a multidimensional matrix and salve it as '.mat' file

After creating the video reader object, the number of frames is computed using the frame rate and elapsing of the video. The top and width of the frame can be obtained from the metadata.

'readFrame' extracts each frame sequentially in the paradigm format. The image can be farther displayed using 'imshow' or written to an image file using 'imwrite' command or stored in a multidimensional matrix as stack of images.

The proper name format of the images saved in the electric current working directory will be 'Image1.jpg','Image2.jpg'…'Image319.jpg'

two. To read all the frames in the video and brandish few frames

Instance ii:

%MATLAB Code

Vptr = VideoReader( 'VIDE01.mp4' );

NFrames = round(Vptr.FrameRate*Vptr.Elapsing);

Jump_ptr = 27;

N = ane;

%To display the Images

for i=1:NFrames

Img = readFrame(Vptr);

if (mod(i-1,Jump_ptr)==0)

figure(2),subplot(3,4,N),imshow(Img);

              N=North+1;

end

end

EXPLANATION:

The in a higher place MATLAB code reads all the frames in the video just displays only few frames. This example typically highlights the use of MATLAB command 'subplot'.

Instead of displaying all the frames, frames with specific interval are displayed. In this instance,frame 1 will be displayed get-go, frame 28 the next then followed by 55 and so on and so forth. 'mod' control is used to find the remainder afterwards division, then whenever 'i' assumes the value equal to multiples of the variable 'Jump_ptr' then the image will be displayed. To displayall the images in the same figure, 'subplot' tin can be used.

'subplot(3,4,N)' refers that the 'figure(2)' can be divided into 3 rows and 4 columns and each image can be placed in each position.  In the given instance, number of frames =319 and the interval altitude (Jump_ptr) is 27, so 319/27 gives 12. And so the subplot is divided as 3 rows and 4 columns to classify spacefor 12 images.

3. To read from a video file and write the frames to another video file

%To write frames to the video

Vptr = VideoReader( 'VIDE01.mp4' );

Wptr = VideoWriter( 'VIDE02.mp4' ,'MPEG-four' );

Wptr.FrameRate=10;

open(Wptr);

for i=1:120

Img = readFrame(Vptr);

writeVideo(Wptr,Img);

end

shut(Wptr);

Caption:

Create the video reader object using 'VideoReader' for 'VIDEO1.mp4'

Create the video writer object using 'VideoWriter' for 'VIDEO2.mp4'

Set the frame rate for the video to be written to a file.

Here, the frame rate 10 indicates,x frames will be displayed per 2nd in a video.

'open' command volition open the video file to starting time the writing process. Instead of 319 frames from the original video('VIDEO1.MP4'), only 120 frames are written to the video file.So information technology is unnecessary to become through all the frames in the video. Outset read the frame from the input video file and write it to the output video file. After 120 frames are read from the input file and written to the output file, the output file is closed.

four. To read a video file and process the frames and write it to some other video file

%To write frames to the video

%Create video Reader object

Vptr = VideoReader( 'VIDE01.mp4' );

%Detect number of frames

NFrames = round(Vptr.FrameRate*Vptr.Elapsing);

%Create Video Writer Object

Wptr = VideoWriter( 'VIDEO_NOISY.mp4' ,'MPEG-iv' );

%Open the output video file

open up(Wptr);

for i=one:NFrames

%Read from video file

Img = readFrame(Vptr);

%Add noise to the prototype

Img = imnoise(Img, 'salt & pepper' );

%write to video file

writeVideo(Wptr,Img);

terminate

%Close the output video file

close(Wptr);

EXPLANATION:

All the frames in the input video is processed and then written to an output file. Here, noise is added to each frame in the intermediate step and and so written to the output video file. Still, instead of addition of noise, the image can be enhanced or processed in the intermediate step.

EXAMPLE:

%Case - VIDEO PROCESSING

%Set the frame rate

%Arrange the Image intensity

%Crop the Image

%Read 250 Frames

Vptr = VideoReader( 'VIDE01.mp4' );

%Find number of frames

NFrames = round(Vptr.FrameRate*Vptr.Elapsing);

%Create Video Author Object

Wptr = VideoWriter( 'VIDEO_Enhance.mp4' ,'MPEG-4' );

Wptr.FrameRate = 10;

%Open the output video file

open(Wptr);

for i=1:230

%Read from video file

    Img = readFrame(Vptr);

%Adapt the paradigm intensity

    Img = imadjust(Img,[0 0 0; 0.7 0.7 0.5],[]);

%Ingather undesired portion

    Img = Img(1:end,251:cease,:);

%write to video file

    writeVideo(Wptr,Img);

finish

%Close the output video file

close(Wptr);

Explanation:

In this example, the frame rate is set to 10 and Instead of reading all the frames(319), 230 frames are read starting from the first frame. Each frame is enhanced and a portion of it is cropped every bit well.

changreaver.blogspot.com

Source: https://www.imageeprocessing.com/2016/06/read-process-and-save-video-in-matlab.html

Postar um comentário for "No More Frames Available to Read From File. Matlab"