1. 程式人生 > >MATLAB 視訊轉影象演算法

MATLAB 視訊轉影象演算法

%function [filename,pathname,fileindex]  by vincent 
[filename,pathname,fileindex]= uigetfile('B*.avi','C:\Users\sunboy\Desktop\file\B','video.avi','Multiselect','on');
%'video.avi'
if ischar(filename) %只有選擇了檔案才進行以下計算
video=mmreader([pathname filename]);
%%General Settings:
%Duration 時間
%Name 視訊名稱
%Path 路徑
%Tag =
%Type = mmreader
%UserData = []
%%Video Settings:
%BitsPerPixel=24
%FrameRate 視訊採集速率
%Height 高度
%NumberOfFrames 總幀數
%VideoFormat 影象模式
%Width = 寬度
LEN=video.NumberOfFrames; %獲得視訊長度
dir=strcat(pathname,strrep(filename,'.avi',''),'\pic');
mkdir(dir);
fn=strrep(filename,'.avi','');
for k=1:LEN-1 %若read 到len,常會報錯如下???MATLAB:read:readTimedOut,read 到len-1 就好了
frame=rgb2gray(read(video,k));
if k<10
imwrite(frame,strcat(dir,'\',fn,'-avi-000',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=10 && k<100
imwrite(frame,strcat(dir,'\',fn,'-avi-00',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=100 && k<1000
imwrite(frame,strcat(dir,'\',fn,'-avi-0',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=1000 && k<10000
imwrite(frame,strcat(dir,'\',fn,'-avi-',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
end
end
elseif iscell(filename)
navi=length(filename);
for n=1:navi
video=mmreader([pathname filename{n}]);
%%General Settings:
%Duration 時間
%Name 視訊名稱
%Path 路徑
%Tag =
%Type = mmreader
%UserData = []
%%Video Settings:
%BitsPerPixel=24
%FrameRate 視訊採集速率
%Height 高度
%NumberOfFrames 總幀數
%VideoFormat 影象模式
%Width = 寬度
LEN=video.NumberOfFrames; %獲得視訊長度
dir=strcat(pathname,strrep(filename{n},'.avi',''),'\pic');
mkdir(dir);
fn=strrep(filename{n},'.avi','');
for k=1:LEN-1 %若read 到len,常會報錯如下???MATLAB:read:readTimedOut,獨到len-1 就好了
frame=rgb2gray(read(video,k));
if k<10
imwrite(frame,strcat(dir,'\',fn,'-avi-000',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=10 && k<100
imwrite(frame,strcat(dir,'\',fn,'-avi-00',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=100 && k<1000
imwrite(frame,strcat(dir,'\',fn,'-avi-0',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
elseif k>=1000 && k<10000
imwrite(frame,strcat(dir,'\',fn,'-avi-',int2str(k),'.bmp'),'bmp');%把每幀影象存入硬碟
end
end
disp(strcat(num2str(n),'/',num2str(navi),' :"',filename{n},'" Finished!',datestr(now,13)));
end
else
return
end
msgbox('所有幀提取完畢,已寫入磁碟!','提示');