How to take pictures from the webcam with Matlab

In Image Processing, taking pictures as well as recording videos is an important task since it is the first step before processing the images. Thus, I consider interesting to write an entry about configuring the webcam in Matlab. I have to say that I was extremely astonished by how easy this was. I was expecting installing external drivers or add-ons in Matlab (actually that was my first and wrong step) but nothing like that is needed. You have to, at least, have installed the camera in your PC, which seems to be quite obvious if you want to use it. The only requirement is having the Image Acquisition Toolbox installed, which I had installed by default in my R2013a Matlab version.

At first, we will use imaqhwinfo to see the adaptors recognized by Matlab:

1
2
3
4
5
6
7
8
>> imaqhwinfo

ans =

    InstalledAdaptors: {'gentl'  'gige'  'matrox'  'winvideo'}
        MATLABVersion: '8.1 (R2013a)'
          ToolboxName: 'Image Acquisition Toolbox'
       ToolboxVersion: '4.5 (R2013a)'

“winvideo” seems to be the adaptor I want to use, so I will try to get more information about it:

1
2
3
4
5
6
7
8
9
>> devices = imaqhwinfo('winvideo')

devices =

       AdaptorDllName: [1x81 char]
    AdaptorDllVersion: '4.5 (R2013a)'
          AdaptorName: 'winvideo'
            DeviceIDs: {[1]  [2]}
           DeviceInfo: [1x2 struct]

We can see in “DeviceIDs” that I have two cameras connected: my laptop camera and a USB camera. If I want to get more information about each camera, I will print out DeviceInfo.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
>> devices.DeviceInfo(1)

ans =

             DefaultFormat: 'YUY2_160x120'
       DeviceFileSupported: 0
                DeviceName: 'USB2.0 Camera'
                  DeviceID: 1
     VideoInputConstructor: 'videoinput('winvideo', 1)'
    VideoDeviceConstructor: 'imaq.VideoDevice('winvideo', 1)'
          SupportedFormats: {1x5 cell}

>> devices.DeviceInfo(2)

ans =

             DefaultFormat: 'MJPG_1280x1024'
       DeviceFileSupported: 0
                DeviceName: '1.3M HD WebCam'
                  DeviceID: 2
     VideoInputConstructor: 'videoinput('winvideo', 2)'
    VideoDeviceConstructor: 'imaq.VideoDevice('winvideo', 2)'
          SupportedFormats: {1x18 cell}

When we are using a camera in Matlab, we need to determine which format will be used. I decided that I want to use the in-built camera, so let’s check what formats does it support.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
>> device2 = devices.DeviceInfo(2);
>> device2.SupportedFormats

ans =

  Columns 1 through 5

    'MJPG_1280x1024'    'MJPG_1280x720'    'MJPG_1280x800'    'MJPG_1280x960'    'MJPG_160x120'

  Columns 6 through 10

    'MJPG_176x144'    'MJPG_320x240'    'MJPG_352x288'    'MJPG_640x480'    'YUY2_1280x1024'

  Columns 11 through 15

    'YUY2_1280x720'    'YUY2_1280x800'    'YUY2_1280x960'    'YUY2_160x120'    'YUY2_176x144'

  Columns 16 through 18
    'YUY2_320x240'    'YUY2_352x288'    'YUY2_640x480'

My recommendation is to use MJPG rather than YUY2, and to use a small size like 640×480 or lower because the lower, the faster to process. In addition, if you want to test your camera in Matlab, you can also type imaqtool and the Simulator will pop up. Now we have to configure Matlab writing which camera and format will be used, frames per second captured, and the color space:

1
2
3
vid = videoinput('winvideo', 2, 'MJPG_640x480');
vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';

When we want to capture a picture with the camera, we simple have to trigger:

1
start(vid);

To retrieve the capture picture(s):

1
picture = getdata(vid);

When using RGB, we would expect that the variable picture have 3 dimensions (height, width, color channel), but if FramesPerTrigger is more than 1, this variable will have 4 dimensions. The 4th dimension will be each frame, so if we want to save the first and last frame taken, we will simply do:

1
2
3
4
5
6
7
vid = videoinput('winvideo', 2, 'MJPG_640x480');
vid.FramesPerTrigger = 30;
vid.ReturnedColorspace = 'rgb';
start(vid)
frames = getdata(vid);
imwrite(frames(:,:,:,1),'firstframe.jpg');
imwrite(frames(:,:,:,end),'lastframe.jpg');

lipman

"The only way to proof that you understand something is by programming it"

2 thoughts on “How to take pictures from the webcam with Matlab

Leave a Reply

Your email address will not be published. Required fields are marked *