How to record a 60 fps constant frame rate video?
13 次查看(过去 30 天)
显示 更早的评论
I bought a camera that records at 60 and 120 fps and it has set default recording with variable frame rate. I need record at 60 fps constant. If I record setting the following properties the frame rate is variable and lower than 60 fps:
video = videoinput('winvideo', 1, 'MJPG_800x600');
% Get the frame rates
source = getselectedsource(video);
frameRates = set(source, 'FrameRate')
% Set the frame rate to 60 fps
fps = frameRates{1};
source.FrameRate = fps;
video.TimeOut = Inf;
video.TriggerRepeat = Inf;
video.FrameGrabInterval = 1;
video.FramesPerTrigger = 1;
video.LoggingMode = 'disk';
%%Create the VideoWriter object and set the DiskLogger Property
timenow = datestr(now,'hhMMss_ddmmyy');
v = VideoWriter(['video_', timenow,'.avi']);
v.Quality = 100;
v.FrameRate = str2double(fps);
video.DiskLogger = v;
I create a figure with a preview, so I stop recording when the figure is closed.
start(video)
uiwait(fig)
stop(video)
I have tried another way based on the example "Creating Time-Lapse Video Using Timer Events" (edit demoimaq_timelapse2):
function ejemplo_timer_aaaaaaaaaaa()
%%Creating Time-Lapse Video Using Timer Events
%
% This example shows how to create a time-lapse video using timer events to prequalify frames.
%
% The Image Acquisition Toolbox(TM) makes it easy to produce time-lapse
% video. In this example, we will use timer events to acquire
% frames to an AVI file. This technique of time decimation has the
% advantage that it allows you to make a decision about each frame before
% storing it. An application of this would be to only store frames that
% meet certain illumination levels, have motion relative to the previous
% frame, etc.
%
% Copyright 2005-2014 The MathWorks, Inc.
%%Create a Video Input Object
% Before acquiring images using the Image Acquisition Toolbox,
% create a video input object.
% When executing the following code, you may need to
% modify it to match your acquisition hardware.
info = imaqhwinfo('winvideo');
id = 1;
formats = info.DeviceInfo(id).SupportedFormats;
% For windows
vid = videoinput('winvideo', id, formats{7});
%%Configure the Timer
% To generate timer events, we specify two things: what happens when it
% occurs, and how often it should occur. The |TimerFcn| property
% specifies the callback function to execute when a timer event
% occurs. A timer event occurs when the time period specified by the
% |TimerPeriod| property expires.
%
% The callback function is responsible for triggering the acquisition and
% storing the frame into the AVI file. More details on how to use this
% callback can be found in the documentation.
% The configuration we will use is
%
% * timelapse_timer will execute each time the timer elapses
% * The timer function will execute every one second
vid.TimerPeriod = 1/60;
vid.TimerFcn = @timelapse_timer_v2;
%%Store the VideoWriter Object
% Store the VideoWriter object in the |UserData| property of the
% video input object so that it will be available inside the callback.
vwObj = VideoWriter('timelapsevideo', 'Uncompressed AVI');
vwObj.FrameRate = 60;
open(vwObj);
vid.UserData = vwObj;
%%Configure the Video Input Object to Use Manual Triggering
% Each time the timer event occurs
%
% * Manually trigger the acquisition using the |triggerconfig| command
% * Acquire one frame
% * Acquire 9 additional triggers worth of data, for a total of 10 frames
triggerconfig(vid, 'manual');
vid.FramesPerTrigger = 1;
vid.TriggerRepeat = 240;
vid
%%Perform the Time-Lapse Acquisition
% Now, start the time lapse acquisition, and wait for up to 20 seconds
% for the acquisition to complete.
start(vid);
% Wait 4 seconds
wait(vid,4);
%%Close the AVI File
% Once the capture is completed, retrieve the VideoWriter object stored in
% the UserData property, and use the |close| function to release the
% resources associated with it.
% vwObj = vid.UserData;
close(vwObj);
%%Play Back the Time-Lapse AVI Sequence
% To play back the time-lapse AVI sequence, right-click on
% the filename in the MATLAB(R) Current Folder browser, and choose
% Open Outside MATLAB from the context menu.
%%Clean Up
% When you are done with the video input object, you should
% use the |delete| function to free the hardware resources associated with
% it, and remove it from the workspace using the |clear| function. Also
% delete and clear the VideoWriter object.
delete(vid);
delete(vwObj);
clear vid;
clear vwObj;
%%The Timer Callback Function
% The following is a description of the callback function executed for each
% timer event
%
% * Trigger the toolbox to acquire a single frame
% * Retrieve the frame
% * Use the |writeVideo| function to add the frame to the AVI file
%
% The VideoWriter object is stored in the |UserData| property of the
% object.
%
function timelapse_timer_v2(vid,~)
% This callback function triggers the acquisition and saves frames to an AVI file.
tic
% trigger the acquisition and get the frame
trigger(vid);
frame = getdata(vid,1);
%frame = getsnapshot(vid);
% Retrieve the VideoWriter object stored in the UserData
% property.
v = vid.UserData;
% Add the frame to the AVI
writeVideo(v, frame);
time = toc
end
end
The timer function will be called every 1/60 = 0.016667 seconds, but the comands lines in the function takes to run 0.04 seconds, so it does not works. Any solutions?
0 个评论
回答(1 个)
Augusto Sanches
2019-3-5
Hello, Oscar!
Would you try that?
clear all
clc
vid1 = videoinput('winvideo', 1);
set(vid1, 'FramesPerTrigger', Inf);
set(vid1, 'ReturnedColorspace', 'rgb')
vid1.FrameGrabInterval = 1;
src1 = getselectedsource(vid1);
frameRates = set(src1, 'FrameRate')
fps = frameRates{1};
src1.FrameRate = fps;
myVideo1 = VideoWriter('cam1.avi');
myVideo1.FrameRate = str2double(fps);
open(myVideo1);
start(vid1);
totalFrames = 150;
while(vid1.FramesAcquired<=totalFrames || vid2.FramesAcquired<=totalFrames)
img1 = getsnapshot(vid1);
writeVideo(myVideo1, img1);
end
close(myVideo1);
stop(vid1);
flushdata(vid1);
delete(vid1);
1 个评论
Mariana Alegria Palacios
2024-5-15
Hi Augusto, I have tried your solution but it generates a video with half the real time, i.e. if you record 60 frames it generates a 1 sec video, but in reality it corresponds to a 2 sec video.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Acquisition Using Any Hardware 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!