Acquiring An Image From Webcam, Using MATLAB Code

Acquiring An Image From Webcam, Using MATLAB Code is rather simple if you know how to access your webcam or other imaging device using MATLAB.
Click Here to know more about accessing Webcam using MATLAB code.

MATLAB Code For Acquiring An Image From Webcam:
 
CODE1: For fast camera in which no warm up delay is required
%creating a "videoinput" abject 'v'
v=videoinput('winvideo', 1, 'YUY2_640x480');
%storing the image in 'im' variable
im=getsnapshot(v);
%displaying the captured image
imshow(im);
%writing/storing the captured image in the hard-disk
imwrite(im,'c:\image.jpg');
%deleting the "videoinput" object to avoid filling up of memory
delete(v);
 
or
 
CODE2: For slow camera in which some warm up delay is required(1-5 seconds)
 
%creating a "videoinput" abject 'v'
v=videoinput('winvideo', 1, 'YUY2_640x480');
%Setting the "FramesPerTrigger" value to '1'
v.FramesPerTrigger= 1;
%for previewing window of the "videoinput" object 'v'
preview(v);
%start capturing
start(v);
%for warming up of webcam
pause(2.0);% Get image data from webcam
im=getdata(v);
%displaying the captured image
imshow(im);
%writing/storing the captured image in the hard-disk
imwrite(im,'c:\image.jpg');
%deleting the "videoinput" object to avoid filling up of memory
delete(v);
 
 
NOTE: 
If your acquired image is completely black, it may be that your camera needs to warm up. To allow for this, try using manual triggering & GETSNAPSHOT.
The exposure time may show up as a device specific property on the source:
More more help regarding this problem do read the below link:

3 comments: