2011-04-13 154 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);