Accessing Webcam Through MATLAB Code

It is actually possible to access various hardware, with MATLAB codes. One of the hardware is your Webcam, in order to take a video or a Picture as an input.

The functions we are going to use for it is: "videoinput" & "preview"
This "videoinput" function creates a "video input object" which can be used for previewing the video through "preview" function.

So code will be like: 
%start of code
vid = videoinput('winvideo', 1, '

 %end of code

Preview of how output will be:

Accessing Webcam Through MATLAB Code: videoinput & preview functions
Accessing Webcam Through MATLAB Code

Click here to see How to Acquire An Image From Webcam, Using MATLAB Code.
Click here to see How to Record a Video From Webcam Using MATLAB Code.

Details of videoinput & preview function:

"videoinput" function:

v = videoinput(Adapter_name)    or
v = videoinput(Adapter_name, Device_ID)    or
v = videoinput(Adapter_name, Device_ID, Format)

# here 'v' is the video object created after executing the "videoinput" function.
# here "Adapter_name" is a string that specifies the name of the adapter used to communicate with that particular device.

NOTE: Use the "imaqhwinfo" function to determine the adaptors available on your system. Its output will be like: 
>> imaqhwinfo
ans = InstalledAdaptors: {'gentl'  'gige'  'matrox'  'ni'  'winvideo'}
         MATLABVersion: '8.0 (R2012b)'
         ToolboxName: 'Image Acquisition Toolbox'
         ToolboxVersion: '4.4 (R2012b)'

With these info., we can use 'gentl' or 'gige' or 'matrox' or 'ni' or 'winvideo' as Adapter_name.

# here Device_ID is a numeric scalar value that identifies a particular device available through the specified adaptor, Adapter_name.

NOTE: Use the "imaqhwinfo(Adapter_name)" syntax to determine the devices available through the specified adaptor. If Device_ID is not specified, the first available device ID is used. For convenience, a device's name can be used in place of the Device_ID. If multiple devices have the same name, the first available device is used.

Its output will be like:
>> imaqhwinfo('ni')
ans = AdaptorDllName: [1x68 char]
          AdaptorDllVersion: '4.4 (R2012b)'
          AdaptorName: 'ni'
          DeviceIDs: {1x0 cell}
          DeviceInfo: [1x0 struct]

# In this case the AdapterName is 'ni', but its device id is not clear, so we can use its name instead of Device_ID.

>> imaqhwinfo('winvideo')
ans = AdaptorDllName: [1x74 char]
          AdaptorDllVersion: '4.4 (R2012b)'
          AdaptorName: 'winvideo'
          DeviceIDs: {[1]}
          DeviceInfo: [1x1 struct]

# In this case the AdapterName is 'winvideo', & its device id is '1', so we can use this value as Device_ID.

# FORMAT is a text string that specifies a particular video format supported by the device
To get a list of the formats supported by a particular device, view the DeviceInfo structure for the device that is returned by the imaqhwinfo  function. Each DeviceInfo structure contains a SupportedFormats field. If FORMAT is not specified, the device's default format is used.

>> a=imaqhwinfo('winvideo');
>> a.DeviceInfo.SupportedFormats

ans =

  Columns 1 through 4

    'YUY2_1280x1024'    'YUY2_1280x800'    'YUY2_160x120'    'YUY2_176x144'

  Columns 5 through 7

    'YUY2_320x240'    'YUY2_352x288'    'YUY2_640x480'

Here 'YUY2_1280x1024' , 'YUY2_1280x800'  ,  'YUY2_160x120'  ,  'YUY2_176x144' ,
'YUY2_320x240'  ,  'YUY2_352x288'  &  'YUY2_640x480' are the supported video format of the device_name 'winvideo' use that.

"preview" function:

It Display preview of live video data. preview(v) creates a Video Preview window that displays live video data for video input object 'v'. The window also displays the timestamp & video resolution of each frame, and the current status of 'v'. The Video Preview window displays the video data at 100% magnification (1:1).


  1. hi thanks for sharing matlab code.
    how can i activate or set laptop camera for image processing and computer vision?i use this code but show error.

    1. Do you have relevant MATLAB toolbox installed? (IMAQ and computervision toolbox). If yes, you can use it.