2011-04-13 150 views
0

我想要读取电影的第一帧,裁剪(丢弃)帧的上半部分,然后将此新(想要)帧保存到新电影中,然后第二帧等等..亲切地帮助我,我该如何做到这一点。 到目前为止,我已经试过这样:从电影中读取和裁剪帧

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 

请帮我this..where我是做错误。

回答

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);