问题
I would like to know how I can record a video in MATLAB with my webcam.
回答1:
NOTE: This is now updated for use with newer versions of MATLAB, since some of the older functionality has been superseded and removed.
If you already know how to capture a single image from a webcam, then it should just be a matter of stitching the images together into a movie. You can use a VideoWriter object to open a movie file and then add sequential images using the writeVideo method. For example:
aviObject = VideoWriter('myVideo.avi'); % Create a new AVI file
for iImage = 1:100 % Capture 100 frames
% ...
% You would capture a single image I from your webcam here
% ...
writeVideo(aviObject, I); % Add the image to the AVI file
end
close(aviObject); % Close the AVI file
I just used a for loop as a simple example, but you may want to use a timer if you instead want to capture images and add them to the AVI file at regular time intervals.
回答2:
First construct a video input interface
vid = videoinput('winvideo',1,'RGB24_400x300');
You'll need to adjust the last bit for your webcam. To find a list of webcam devices (and other things besides) use:
imaqhwinfo
The following makes the first webcam into an object
a=imaqhwinfo('winvideo',1)
Find the list of supported video formats with
a.SupportedFormats
You'll then want to determine your frame rate (more on this here):
set(vid,'FramesPerTrigger',100);
start(vid);
wait(vid,Inf);
% Retrieve the frames and timestamps for each frame.
[frames,time] = getdata(vid, get(vid,'FramesAvailable'));
% Calculate frame rate by averaging difference between each frame's timestamp
framerate = mean(1./diff(time))
The FrameGrabInterval property specifies how often frames are stored from the video stream. For instance, if we set it to 5, then only 1 in 5 frames is kept -- the other 4 frames will be discarded. Using the framerate, determine how often you want to get frames
set(vid,'FrameGrabInterval',10);
To determine how many frames to acquire in total, calculate the total number of frames that would be acquired at the device's frame rate, and then divide by the FrameGrabInterval.
capturetime = 30;
interval = get(vid,'FrameGrabInterval');
numframes = floor(capturetime * framerate / interval)
You are now ready to record and play with video using the getdata
command (peekdata
is also helpful), however...
If a large number of frames will be acquired, it is more practical to log the images to disk rather than to memory. Using the Image Acquisition Toolbox, you can log images directly to an AVI file. We configure this using the LoggingMode property.
set(vid,'LoggingMode','disk');
Create an AVI file object to log to, using the avifile command. We must specify the filename to use, and the frame rate that the AVI file should be played back at. Then, set the DiskLogger property of the video input object to the AVI file.
avi = avifile('timelapsevideo','fps',framerate);
set(vid,'DiskLogger',avi);
Start the time-lapse acquisition, and wait for the acquisition to complete. Note that the Image Acquisition Toolbox does not tie up MATLAB® while it is acquiring. You can start the acquisition and keep working in MATLAB.
start(vid);
wait(vid,Inf); % Wait for the capture to complete before continuing.
Once the capture is completed, retrieve the AVI file object, and use the close function to release the resources associated with it.
avi = get(vid,'DiskLogger');
avi = close(avi);
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.
delete(vid);
clear vid;
A large portion, but not all, of the above was drawn from here.
When you hit start(vid)
you may notice that there's a bit of a delay before frames begin to be acquired. This is bad if you're trying to synchronize the video with something. In this case, you'll want to try working with the trigger:
triggerconfig(vid,'manual');
start(vid); %There'll be a delay here, but nothing is being captured
trigger(vid); %Use this line when you want the capture to start. There should be very little delay.
More info on triggers and synchronization is here.
回答3:
Google revealed these:
- Create Video file from image or device
- Image Capture using webcam in MATLAB
回答4:
Here you can see great videos how to capture and process the images from webcam, so recording shouldn't be hard:
http://blogs.mathworks.com/videos/2008/01/18/cool-feature-video-processing-demos/
回答5:
In my college life i have made this project named as
Audio/video Recorder & player application based on MATLAB
In that i have did audio recording and video recording and playback in matlab programming. you can download Source code from here
回答6:
I recently wrote a blog post about how to record synchronized audio and video in MATLAB by sending command lines to the ffmpeg program. You can find out more here, but below is a sample using some settings that made sense on my computer.
dos('ffmpeg -list_devices true -f dshow -i dummy')
dos('ffmpeg -f dshow -list_options true -i video="USB2.0 HD UVC WebCam')
dos('ffmpeg -f dshow -video_size 1280x720 -framerate 30 -audio_buffer_size 80 -i video="USB2.0 HD UVC WebCam":audio="Microphone (Realtek High Definition Audio)" -t 10 -c:v libx264 -preset veryfast -crf 25 test_vid2.mp4');
来源:https://stackoverflow.com/questions/1637589/how-do-i-record-video-from-a-webcam-in-matlab