logo logo

Preview and capture webcam into an axes

My first Matlab tips. We will create a GUI form that will preview and capture picture from webcam connected to your computer. Webcam preview and captured image will be shown on an axes.

  • Create a new blank GUI using Matlab’s GUIDE (File –> New –> GUI –> Blank GUI). Put one panel, two pushbutton and one axes. You can arrange them like this:
New form from Matlab's GUIDE

Arrange the component like this (just for example)

  • Tag the axes as axPreview, first pushbutton as pbPreview and second pushbutton as pbCapture.
  • When user click on pbPreview image form webcam will be shown on axPreview. pbCapture will capture the image, show it on axes and save it as an image file.
  • you need to know how’s Matlab detected your webcam. Open Image Acquisition Toolbox. From Matlab’s Command Window, type this: imaqtool  OR you can follow this screenshot to access it via Matlab’s start menu
matlab access imaqtool via menu

access imaqtool via start-menu

  • Make sure your webcam is connected in your computer before Matlab run! If not, Matlab might not detect your webcam
  • imaqtool window will be shown. See the screenshot (a part of imaqtool form):
matlab supported webcam mode

Matlab detected my webcam

  • From the screenshot, my webcam is detected as winvideo-1 and Matlab also lists all supported mode that my webcam has. I’ll use YUY2_176x144 mode (the 176×144 number is obviously the image size)
  • Put these codes on your pbPreview_Callback (it’s not quite pretty colored, GeSHi doesn’t support Matlab .m file language yet). Read the code’s comment for the explanation line by line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
% choose which webcam (winvideo-1) and which  mode (YUY2_176x144)
vid = videoinput('winvideo', 1, 'YUY2_176x144');
% only capture one frame per trigger, we are not recording a video
vid.FramesPerTrigger = 1;
% output would image in RGB color space
vid.ReturnedColorspace = 'rgb';
% tell matlab to start the webcam on user request, not automatically
triggerconfig(vid, 'manual');
% we need this to know the image height and width
vidRes = get(vid, 'VideoResolution');
% image width
imWidth = vidRes(1);
% image height
imHeight = vidRes(2);
% number of bands of our image (should be 3 because it's RGB)
nBands = get(vid, 'NumberOfBands');
% create an empty image container and show it on axPreview
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview);
% begin the webcam preview
preview(vid, hImage);
  • as for the pbCapture_Callback. Most of codes explanation are the same as pbPreview_Callback
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
vid = videoinput('winvideo', 1, 'YUY2_176x144');
vid.FramesPerTrigger = 1;
vid.ReturnedColorspace = 'rgb';
triggerconfig(vid, 'manual');
vidRes = get(vid, 'VideoResolution');
imWidth = vidRes(1);
imHeight = vidRes(2);
nBands = get(vid, 'NumberOfBands');
hImage = image(zeros(imHeight, imWidth, nBands), 'parent', handles.axPreview)
preview(vid, hImage);
 
% prepare for capturing the image preview
start(vid); 
% pause for 3 seconds to give our webcam a "warm-up" time
pause(3); 
% do capture!
trigger(vid);
% stop the preview
stoppreview(vid);
% get the captured image data and save it on capt1 variable
capt1 = getdata(vid);
% now write capt1 into file named captured.png
imwrite(capt1, 'captured.png');
% just dialog that we are done capturing
warndlg('Done!');
  • Example result:
matlab preview and capture webcam

Example form running our Matlab codes

Things to remember:

  • connect your webcam before running Matlab
  • put a pause a few seconds before capturing image, without this you’ll capture black/dark image!
  • choose rgb as ReturnedColorspace before capturing image

UPDATE: since there are people that having difficulties implementing the code, you can download this .zip file so you can quickly try it on your own Matlab. (Please remember that this code is created using Matlab 2011a GUIDE, I don’t guarantee it will work on earlier version)

Download MATLAB demo codes to capture image from webcam
bottom

15 Responses to “Preview and capture webcam into an axes”

  1. budi says:

    thank you very much, really help a lot.

  2. frado says:

    thanks a lot, the code is very easy and focus….

  3. JuCh says:

    Thank you, just I needed

  4. lalit says:

    Thank you very much.. I am just begginner with matlab and camera interfacing.. and this helps me lot…

  5. Sevcan says:

    I’m grateful to you! This was my homework and I have searching about three hours on the internet.
    Thanks a lot.

  6. bosbubalus says:

    i used it in my code, but suddenly it stopped working. I don’t know why

    • admin says:

      I’ve edited my post, please download my code (.zip) and try it. Let me know if there’s still error.

  7. chidi says:

    i keep getting an error regarding undefined variable or class “handles” and “handles.axPreview”. How do i solve this?

    Thanks

  8. Alex Ricketts says:

    Where are the zip files?

    • admin says:

      Read the whole post. At the end of the post, there is a big download button (“Download MATLAB demo codes to capture image from webcam“)

  9. amila says:

    thanks dear..
    it works.

  10. jems says:

    thank you very much..it’s works..
    but, how to use two types of axes
    1 axes for preview
    another axes for result of capturing
    thanks a lot

  11. Sami Ogün says:

    That helped me really very much thanks a lot for your efforts you are good :) ))

  12. XDanke says:

    Gracias!!! es lo que andaba buscando y lo explicas muy bien.

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

bottom