Camera access and video input in Matlab

Posted by SUYOG PATIL on 10:24 AM in , , ,
Getting Hardware information
Till now we have been working on images already saved on our computer. But in actual practice, we need to work in real time, i.e., we need to take images continuously from the current environment using a webcam and then process them. Hence, the Image Acquisition toolbox of MATLAB provides support in this regard. To start with working in real time, you must have a functional USB webcam connected to your PC and its driver installed. MATLAB has built-in adaptors for accessing these devices. An adaptor is a software that MATLAB uses to communicate with an image acquisition device. You can check if the support is available for your camera in MATLAB by typing the following:
>> imaqhwinfo % stands for image acquisition hardware info
>> cam=imaqhwinfo;
>> cam.InstalledAdaptors
I tried this on my computer and I got the following information

To get more information about the device, type
>>dev_info = imaqhwinfo('winvideo',1)

Most probably you would have 'winvideo' installed and use that. If it is not available, use whichever adapter is shown by ‘imaqhwinfo’. If you are using a laptop, you may also have a webcam in it. So note down the DeviceName shown as above. If it is not the USB webcam, then probably DeviceID = 2 should work. Hence, type
>>dev_info = imaqhwinfo('winvideo',2)
*Note: From now onwards, I will refer Adapter by ‘winvideo’ and DeviceID by ‘1’. You must check yourself what is available on your system and change the commands described further accordingly. Note down the supported formats by your camera as
>>dev_info = imaqhwinfo('winvideo',1);

As you can see, there are 5 supported formats in my camera. The numbers (160x120, 176x144….) denote the size of the image to be captured by the camera.

Previewing video
You can preview the video captured by the camera by defining an object (say by the name ‘vid’) and associate it with the device.
>>vid=videoinput('winvideo',1, 'YUY2_160x120')
>>vid=videoinput('winvideo',1, 'RGB24_160x120') % depends on availability
It should give information somewhat like this

‘vid’ is has been declared as the video input object, and now the camera can be referenced by this name. To see a preview of the video through your camera, use preview command
>> preview(vid)

You can check out the preview with different formats. You will see that the size of preview window changes. Use a format that suits your size requirement. A larger size gives greater clarity and is easier to work with, but consumes more memory and therefore is slow. But in a smaller image, it is difficult to differentiate between two objects.

Capturing Images
Now you have a video stream available and you need to capture still images from it. For that, use getsnapshot() command.
>>im=getsnapshot(vid); % where vid is video input object
Here ‘im’ is the 3D matrix and you can see the image by the usual imshow() command
If you get an unexpected image (with shade of violet/green/pink and low clarity), there is nothing to worry. You must be using a format starting with ‘YUY2_...’ which means that your image is in YCbCr format and not RGB format. Therefore, you must convert it in RGB format by using
If a format somewhat like ‘RGB24_160x120’ (anything starting with RGB24_....) is used, then you directly get the image in RGB format. If you want to store an image captured, so that you can view it later (like .jpg, .png, .bmp), you can use imwrite()
The file myimage.jpg is saved in the current working directory.
Courtesy-Ankur Agrawal,Indian Institute of Technology, Kanpur

Thanks for reading ...
we will study some functions for Image processing next time....



thank you very much for your supporting..
i tried to do the same steps on my external stereo camera but error result even with the dev_info = imaqhwinfo('winvideo',2)

Thank you. That was very helpful

This comment has been removed by the author.

I have a ttl serial camera where can i get the adapters?

could you please give me the code for calculation of distance from object in image (live video) using single camera.

Connect with your world day or night using our Webcam with Night Vision. 6 LEDs will illuminate the darkness, and a high-resolution color sensor provides 24-bit true color. Twist the lens to focus it for an incredibly clear picture. Great for Skype, ICQ, AIM, MSN, or net meetings! Pleas 8mp Hikvision

Post a Comment

Copyright © 2009 ALL ABOUT ROBOTICS!! All rights reserved. Theme by Laptop Geek. | Bloggerized by FalconHive.