
mmreader method to open a file and further use the read method to read frames: % open file
video = mmreader ('d: \ test \ 1.avi');
% like this you can learn about some properties of the open video
width = video.Width;
height = video.Height;
frameRate = video.FrameRate;
numOfFrames = video.NumberOfFrames;
% read frame
% frames numbered from 1
frameNo = 50;
frame = read (video, frameNo);
% frame can be read and like this
frame = video.read (frameNo);
% read frame range
% 50 - the number of the first frame read
% 100 - last read frame number
framesRange = [50 100];
frames = read (video, framesRange);
% read all videos
% operation will not be performed if the video is large and does not fit into memory
entireVideo = read (video);
% There is no special function to close the video file.
% Correspondingly, when you finish working with video
% no additional action is needed.
>> video.Width
ans =
704
>> video.Height
ans =
576
>> frame = read (video, 5);
>> size (frame)
ans =
576,704 3
>> frames = read (video, [5 10]);
>> size (frames)
ans =
576 704 3 6 % create an object to read the video
reader = video.MultimediaFileReader ('Filename', 'd: \ test \ 1.avi');
% read all videos frame by frame
% step function will return a matrix with the size of Height x Width x 3
% true the elements of this matrix will be of the single type - 4 byte floating point numbers
while ~ isDone (reader)
frame = step (reader);
end
% here as in the previous case, you do not need to perform any closing operations
% at the end of reading the video
mmreader and read . %% reading video
filename = 'd: \ test \ 1.avi';
video = mmreader (filename);
nFrames = 200;
% prepare the structure in which the information will be read
mov (1: nFrames) = struct ('cdata', [], 'colormap', []);
% Read frame by frame
for k = 1: nFrames
mov (k) .cdata = read (video, k);
end
%% play video
hf = figure;
set (hf, 'position', [150 150 video.Width video.Height])
movie (hf, mov, 1, video.FrameRate);
mov variable. This variable stores an array of structures. Each structure has two fields: cdata and colormap . The cdata field stores the bitmap of the frame, and the colormap field needs to store a color palette for indexed video. In this case, this is a full-fledged RGB video and the colormap empty. % create an object to read the video
reader = video.MultimediaFileReader ('Filename', 'd: \ test \ 1.avi');
% create an object to play the video
player = video.VideoPlayer;
% read frame by frame and play the video
while ~ isDone (reader)
frame = step (reader);
step (player, frame);
end %% prepare variables
filename = 'd: \ test \ 1.avi';
result_file = 'd: \ test \ result.avi';
nFrames = 200;
video = mmreader (filename);
% open file for writing
% compressor set - XVID
writer = avifile (result_file, 'compression', 'XVID');
%% in cycle read-modify-write
% structuring element for dilatation
se = strel ('rectangle', [3 3]);
for k = 1: nFrames
% read frame
frame = read (video, k);
% do frame dilatation
frame = imdilate (frame, se);
% write frame
writer = addframe (writer, frame);
end
% sure to close the writer
writer = close (writer); avifile function are avifile . The first argument is the name of the file, then the arguments go in pairs — first the name of the parameter, then the value. In the above example, the avifile function avifile in addition to the file name, also passes the compression parameter with the value XVID.movie function to play videos. More to the point: the array should consist of structures, one of the members of the structure is the cdata field that stores the bitmap of the image, and the second is the colormap field that stores the palette for the indexed color image: %% reading video
filename = 'd: \ test \ 1.avi';
video = mmreader (filename);
nFrames = 200;
% prepare the structure in which the information will be read
mov (1: nFrames) = struct ('cdata', [], 'colormap', []);
% Read and modify the first 200 frames of video
se = strel ('rectangle', [3 3]);
for k = 1: nFrames
frame = read (video, k)
mov (k) .cdata = imdilate (frame, se);
end
%% video recording
movie2avi (mov, 'd: \ test \ result.avi', 'compression', 'XVID', 'fps', 25);
movie2avi function is the same as in the previous section. reader = video.MultimediaFileReader ('Filename', 'd: \ test \ 1.avi');
% open the video to record
% file name = d: \ test \ result.avi
% audio - no, there is video
% video codec = MJPEG
writer = video.MultimediaFileWriter ('Filename', 'd: \ test \ result.avi', ...
'AudioInputPort', false, 'VideoInputPort', true, ...
'VideoCompressor', 'MJPEG Compressor');
% structuring element for dilatation
se = strel ('rectangle', [3 3]);
while ~ isDone (reader)
% read frame
frame = step (reader);
% do frame dilatation
frame = imdilate (frame, se);
% write frame
step (writer, frame);
end
% sure to close the writer
close (writer);
video.MultimediaFileWriter function are the pairs 'parameter name', the value of the parameter. Even the file name is transferred in this way (i.e., the pair 'Filename', 'file name' is indicated).avifile and movie2avi . “None (uncompressed)” can be specified as VideoCompressor, “MJPEG Compressor” has also worked for me. If you omit the VideoCompressor parameter in the argument list, it will be equivalent to specifying 'None (uncompressed)'.sudo octave . And then in Octave run the command pkg install /path/to/package.tar.gzfilename = '/home/alexey/octave/1.avi'; result_file = '/home/alexey/octave/result.avi'; % uncomment the next line to access the video properties % info = aviinfo (filename); % width = info.Width % height = info.Height % create a video recording object writer = avifile (result_file); se = ones ([3 3 3]); for i = 1: 25 % read frame i from video frame = aviread (filename, i); % do frame dilatation frame = imdilate (frame, se); % write frame addframe (writer, frame); end
avifile(filename, frameNo) function reads the frame with the frameNo number from the video file with the name filename . No need to create any objects in advance and open the file. This function is also present in the trademark, but it is deprecated.aviinfo(filename) function. As a result, the structure will be returned storing information about the size and format of the video, the number of frames, etc. In the lab, this function is deprecated.avifile and addframe , but the syntax of these functions is slightly different in the hardware and Octave. In addition, in Octave, it is not necessary to close the object recording the video (moreover, there is not even a method that would close it). %% reading video
filename = 'd: \ test \ result.avi';
result_file = 'd: \ test \ result2.avi';
% info = aviinfo (filename);
% width = info.Width;
% height = info.Height;
writer = avifile (result_file, 'compression', 'xvid');
se = strel ('rectangle', [3 3]);
for i = 1: 25
frame = aviread (filename, i);
frame.cdata = imdilate (frame.cdata, se);
writer = addframe (writer, frame.cdata);
end
writer = close (writer);
Source: https://habr.com/ru/post/97145/
All Articles