我想要阅读电影的第一帧,裁剪(丢弃)帧的上半部分,然后将这个新的(想要的)帧保存到新电影中,然后将第二帧保存等等......我怎么能做这个? 到目前为止,我已经尝试过这个:
clc;
clear all;
obj=mmreader('2.avi'); % input movie file
mov=read(obj);
frames=get(obj,'numberOfFrames'); %get the number of frames
cmap=zeros(256,3);
for i=1:10
cmap(i,1)=(i-1)/255;
cmap(i,2)=(i-1)/255;
cmap(i,3)=(i-1)/255;
end;
aviobj=avifile('c:\new_movie_file.avi','compression','none','colormap',cmap);
for k = 1 : 10
I(k).cdata = mov(:,:,:,k); %store frame information in an array vector
I(k).colormap = [];
end
for j = 1 : 10
%get the first frame from the movie
%frame1=mov(1,k);
%extract the colour data AND crop
frame2=I(j).cdata(:,100:end); % I am confused how to write this statement properly to crop the image frame from desired row number
%add to avi file
aviobj=addframe(aviobj,frame2);
end;
%close file!
aviobj=close(aviobj);
implay(aviobj); % It displays a movie which contains three separate overlaped frames(windows) of the original movie in distorted form
答案 0 :(得分:0)
如果你想一次性完成所有操作而不在循环中进行...
%read the entire video
obj=VideoReader('2.avi');
mov=read(obj);
size(mov) %mov is in 4D matrix: [Height (Y), Width (X), RGB (color), frame]
%determine the height of the video
vidHeight = obj.Height;
%only use the top half of the image:
mov_cropped=mov(1:vidHeight/2,:,:,:);
aviObj = VideoWriter('cropped_video.avi','Uncompressed AVI');
%save the cropped video
open(aviObj);
writeVideo(aviObj,mov_cropped);
close(aviObj);
请注意,对于大型avi文件存在问题,通过尝试一次性读取它,可能会耗尽内存。在这种情况下,最好逐帧读取文件,并逐帧写出文件。
for k=1:obj.NumberOfFrames
mov(k).cdata = read(xyloObj, k);
end
在转换数据后保存数据(注意矩阵中的顺序会发生变化)
vidObj = VideoWriter('cropped_video.avi');
open(vidObj);
for k=1:obj.NumberOfFrames
imshow(mov(k));
writeVideo(vidObj,currFrame);
end
close(vidObj);