[英]reading and displaying video file frame by frame
我正在與Matlab合作。 我想讀取一個視頻文件並在每幀進行一些計算並顯示每一幀。 我寫了下面的代碼,但每次只顯示第一幀。 任何人都可以幫忙。
mov=VideoReader('c:\vid\Akiyo.mp4');
nFrames=mov.NumberOfFrames;
for i=1:nFrames
videoFrame=read(mov,i);
imshow(videoFrame);
end
注意:MATLAB已停止使用
mmreader
API,因此更喜歡使用VideoReader
。
請參閱@Vivek的評論。
我經常這樣做:
obj=mmreader('c:\vid\Akiyo.mp4');
nFrames=obj.NumberOfFrames;
for k=1:nFrames
img=read(obj,k);
figure(1),imshow(img,[]);
end
就你的代碼而言,我看到了MATLAB的文檔。 你應該按照以下順序做事:
mov=VideoReader('c:\vid\Akiyo.mp4');
vidFrames=read(mov);
nFrames=mov.NumberOfFrames;
for i=1:nFrames
imshow(vidFrames(:,:,i),[]); %frames are grayscale
end
函數read()和字段NumberOfFrames()現已棄用,Matlab建議使用
xyloObj = VideoReader(file);
vidHeight = xyloObj.Height;
vidWidth = xyloObj.Width;
mov = struct('cdata',zeros(vidHeight, vidWidth, 3,'uint8'), 'colormap',[]);
while hasFrame(xyloObj)
mov(k).cdata = readFrame(xyloObj,'native');
end
如果您想估算視頻中的幀數,請使用nFrames = floor(xyloObj.Duration) * floor(xyloObj.FrameRate);
以下建議的代碼僅顯示一幀
imshow(vidFrames(:,:,i),[]);
我正在做一些事情來存儲每一幀
obj = VideoReader('path/to/video/file');
for img = 1:obj.NumberOfFrames;
filename = strcat('frame',num2str(img),'.jpg');
b = read(obj,img);
imwrite(b,filename);
end
這將存儲您主目錄中的所有幀。是的,正如Vivek和Parag所建議的那樣
您需要使用VideoReader,因為MATLAB已停止使用mmreader。
* =我正在創建一個函數來播放任何.avi文件作為圖中的一組幀。 這是做了什么。 你所做的一些組合,除了我的NumberOfFrames沒有工作:(注意這也顯示它的顏色)
function play_video(filename)
% play_video Play a video file
% play_video(filename) plays the video file specified by filename in a MATLAB Figure window.
figure
set(figure, 'Visible', 'on')
mov=VideoReader(filename);
vidFrames=read(mov);
duration = mov.Duration;
frame_rate = mov.FrameRate;
total_frames = duration .* frame_rate
for i=1:1:total_frames
imshow(vidFrames(:, :, :, i), []);
drawnow
end
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.