MATLAB: webcam video acquisition - matlab

The Logitech C910 webcam spec indicates image and video capture. Because image and video capture are listed separately, I am assuming that they are encoded and sent differently: effectively forming two different 'channels' to select from. If this understanding is incorrect, please respond with an explanation of the true nature.
This reference indicates a maximum frame-rate of 15 because of windows.
My search returned webcam video acquisition that comprises a series of time lapsed images 'stitched' together
% Connect to the webcam.
cam = webcam
% Open Video File
vidWriter = VideoWriter('frames.avi');
open(vidWriter);
% Write images file
for index = 1:20
% Acquire frame for processing
img = snapshot(cam);
% Write frame to video
writeVideo(vidWriter,img);
end
%Close file and cam
close(vidWriter);
clear cam
MATLAB has captured successfully images with the C910.
Question
If possible within MATLAB, how does one configure the webcam's **video* frame rate and save the video stream to .avi or the like? (not writing still images to video file as depicted above).
Perhaps someone with experience or sharper Google skills can provide an example of bridging the webcam's video (vs the image) stream into MATLAB. Any example that can be tested is greatly appreciated.

A good way to 'jumpstart' or accelerate newbie familiarity is with the image acquisition tool:
imaqtool
The tool seems to be GUI wrapper that reduces the command-line syntax to a GUI. Note that the bottom right panel shows the command line equivalent of a GUI interaction.
Configurable video capture paramaters comprise:
Resolution & ROI (Region of Interest)
Frame Rate
Video type (.avi .mp4) etc.

Related

Record screen in real time using MATLAB?

I am using an optical microscope and camera to record some videos which are post-processed in MATLAB.
Real time acquisition and pixel statistics would be extremely helpful, because some of what I am looking at absorbs very little light (I am using transmission mode). An example is that a blank (background) sample would give me an an average pixel value across a 512x512 ccd array of something like 144 (grayscale). An actual sample might have an average value of 140 or so. This subtle shift in pixel intensity would be useful in helping me focus the microscope.
Unfortunately, my camera setup is not supported by MATLAB, so I cannot use the image acquisition toolbox for real time. So I was wondering, is there a way that I could 'fake' real time image acquisition by selecting say a rectangle of my current desktop (the rectangle that is the video output of the microscopes camera), for matlab to record in real time?
Thanks

Display live processed webcam stream using matlab

I am trying to employ a chroma key algorithm on a live video. I need to take a live webcam input, process it in real time and display it. I already have the chroma key algorithm working on images.
How do I process the webcam input and display it immediately. I have tried using snapshot() and passing the image to the chroma key algorithm but it is too slow even if I increase the rate of snapshots. I want a smooth output.
[ Also, if there is a better alternative than Matlab please let me know. ]
Instead of using getsnapshot() which connects to the camera and disconnects on EVERY single frame (thus slow framerates), try to use videoinput and then preview the connection: http://www.mathworks.de/de/help/imaq/preview.html
This example is made for you:
http://www.mathworks.de/products/imaq/code-examples.html?file=/products/demos/shipping/imaq/demoimaq_LiveHistogram.html
As shown you can even define a callback-handler-function which is called on every newly received frame.
You must set TriggerType to manual, or else getsnapshot() will create (and destroy) a conection to the camera everytime you need a frame. By setting it to manual, you can start the camera once, get the frames and stop the camera when you're done.
Here is an example:
vidobj = videoinput('winvideo', 1, 'RGB24_640x480');
triggerconfig(vidobj, 'manual');
start(vidobj);
while true % Or any stop condition
img = getsnapshot(vidobj);
% Process the frame.
...
imshow(img);
drawnow;
end

Matlab: accessing both image sequences from a 3D video file

I have recorded a 3D video using a Fujifilm Finepix Real 3d w3 camera. The resulting video file is a single AVI, so the frames from both lenses must somehow be incorporated within the single file.
I now wish to read in the video into Matlab such that I have two image sequences, each corresponding to either the left lens or the right lens.
So far I have played back the AVI file using the computer vision toolbox functions (vision.VideoFileReader etc), however it ignores one of the lenses and plays back only a single lens' image sequence.
How do I access both image sequences within Matlab?

How to speed up frame rates in MATLAB? [duplicate]

the getsnapshot function takes a lot of time executing since (I guess) initializes the webcam every time is called. This is a problem if you want to acquire images with a high framerate.
I trick I casually discovered is to call the preview function, which keeps the webcam handler open making getsnapshot almost instantaneous, but it keeps a small preview window open:
% dummy example
cam = videoinput(...);
preview(cam);
while(1)
img = getsnapshot(cam);
% do stuff
end
Is there a "cleaner" way to speedup getsnapshot? (without preview window opened)
You could use the new "machine vision" toolbox which is specially built for vision applications. See code below:
vid = videoinput('winvideo', 1, 'RGB24_320x240'); %select input device
hvpc = vision.VideoPlayer; %create video player object
src = getselectedsource(vid);
vid.FramesPerTrigger =1;
vid.TriggerRepeat = Inf;
vid.ReturnedColorspace = 'rgb';
src.FrameRate = '30';
start(vid)
%start main loop for image acquisition
for t=1:500
imgO=getdata(vid,1,'uint8'); %get image from camera
hvpc.step(imgO); %see current image in player
end
As you can see, you can acquire the image with getdata. The bottleneck in video applications in Matlab was the preview window, which delayed to code substantially. The new vision.VideoPlayer is a lot faster (i have used this code in real time vision applications in Matlab. When i had written the first version without the vision toolbox, achieving frame rates at about 18 fps and using the new toolbox got to around 70!).
Note: I you need speed in image apps using Matlab, you should really consider using OpenCV libs through mex to get a decent performance in image manipulation.

how to generation animation as mpg or speed up gif in matlab

I am plotting a randomly walking points on the figure and trying to capture the motion at each step by getframe. After I collect all the frames, I output the result as avi with movie2avi, but the output file was so big to fit into my presentation. I am looking for a way to export the movie to mp4, anyone have any idea? I also try to use the 3rd party movie2gif, it save the size to huge extent but when I play the gif, it looks so unsmooth
In later versions of Matlab (e.g. 2012) it is done by creating and writing a video object. For example, the following code generates movie of a randomly moving circle. You can adjust the speed of the movie with the FrameRate and the size with the Quality properties. For more details see the Matlab documentation.
vobj=VideoWriter('MyMovieFile', 'Motion JPEG AVI');
vobj.FrameRate=4;
vobj.Quality=75
open(vobj);
for i=1:100
plot(rand,rand,'o')
F=getframe(gcf);
writeVideo(vobj, F);
cla(gca)
end
close(vobj)