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

  • as for the pbCapture_Callback. Most of codes explanation are the same as pbPreview_Callback

  • 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

UPDATE 2 : In order to make the codes work on latest MATLAB you should make “vid” become a global variable. Here’s how:
on pbPreview_Callback, after preview(vid, hImage);, add these:

on pbCapture_Callback, replace vid = videoinput(‘winvideo’, 1, ‘YUY2_176x144’); with these:

Now it should work fine on latest MATLAB, tested on MATLAB 2015b.

Leave a comment ?

37 Comments.

  1. thank you very much, really help a lot.

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

  3. Thank you, just I needed

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

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

  6. bosbubalus

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

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

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

    Thanks

  8. Alex Ricketts

    Where are the zip files?

    • 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. thanks dear..
    it works.

  10. 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

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

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

  13. thank u so much,,it’s realy helpfull for me…

  14. this post really helps but i would like to know how can i do the video preview takes the size of the axes

  15. I can’t download this zip files..

    I have used your code, but first time it sets preview. 2nd time to capture image, it first stops camera, then again starts, wait for 3 seconds , captures image and then store and stop…

    Is it possible, without stopping 1st time, it capture image by clicking at 2nd time..?
    Bacause for second time 3seconds are not enough for clearity of image because it takes initialization time, and image is little black..

  16. Hi there , i tried your code and it only saves the Photo as ‘PNG’
    why cant i save it in JPEG?? i get an error if i replace PNG with JPEG

    Thank you

  17. Nice and easy code.. Thanks a lot..

  18. i keep getting this error and I do not understand why

    Error in mygui>pbCapture_Callback (line 107)
    vid = videoinput(‘winvideo’, 1, ‘YUY2_176x144’);

    Error in gui_mainfcn (line 96)
    feval(varargin{:});

    Error in mygui (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)mygui(‘pbCapture_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

    • make sure your webcam is detected by MATLAB.
      try using “imaqtool” before and see if your webcam is listed.

      • It is being picked up when i check both the preview and the capture button work but only one will work then ill have to rerun the program to get capture to work. Is there anyway to be able to press preview then capture n then preview again n capture numerous times without having to rerun the program every time?

  19. first time it worked!
    then I keep getting this error!

  20. i keep getting this error and I do not understand why
    Reference to non-existent field ‘axPreview’.

    Error in camex1>pbpreview_Callback (line 106)
    hImage = image(zeros(imHeight, imWidth, nBands), ‘parent’, handles.axPreview);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in camex1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)camex1(‘pbpreview_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

    Reference to non-existent field ‘axPreview’.

    Error in camex1>pbpreview_Callback (line 106)
    hImage = image(zeros(imHeight, imWidth, nBands), ‘parent’, handles.axPreview);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in camex1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)camex1(‘pbpreview_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

    Error using input
    Too many input arguments.

    Error in camex1>pbcapture_Callback (line 115)
    input(‘winvideo’, 1, ‘YUY2_176x144’);

    Error in gui_mainfcn (line 95)
    feval(varargin{:});

    Error in camex1 (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in @(hObject,eventdata)camex1(‘pbcapture_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

  21. Thanks a lot for ur efforts and code.

    I am using both Preview and Capture in my GUI,but unfortunately when i push the Preview button and then capture, I face this error:
    Error using videoinput (line 380)
    winvideo: The device associated with device ID 1 is already in use. A new videoinput
    object cannot be created for this device while it is in use.

    what should i do?How can i save the image by using capture button?

    I tried to use getframe and frame2im functions to save images from the frame provided in the GUI but the quality is not comparable with getting images directly from camera.
    Please help me

  22. Thank you,it works just fine.
    another problem is the brightness of the image i capture is totally different with the image that is saved. why?

  23. Tasnim Sadikot

    Hi there, i continuously get error like
    Error using videoinput (line 380)
    winvideo: The device associated with device ID 1 is already in use. A new videoinput object
    cannot be created for this device while it is in use.

    Error in camerapreview>pbCapture_Callback (line 118)
    vid = videoinput(‘winvideo’, 1, ‘YUY2_320x240’);

    Error in gui_mainfcn (line 96)
    feval(varargin{:});

    Error in camerapreview (line 42)
    gui_mainfcn(gui_State, varargin{:});

    Error in
    @(hObject,eventdata)camerapreview(‘pbCapture_Callback’,hObject,eventdata,guidata(hObject))

    Error while evaluating uicontrol Callback

    >>> How to declare videoinput globally to use it by both callbacks? or i need to include two axes for solution?

    • same answer as previous commenter, my codes doesn’t work properly in latest MATLAB, you should make “vid” as global variable. So, “vid” will be initialized once and can be use on other function.
      It should work.

  24. MOHD ROZAIMEI BIN AHMAD

    for pbPreview is OK..its works.. but for pbCapture appears error : Error using videoinput
    winvideo: The device associated with device ID 2 is already in use. A
    new videoinput object cannot be created for this device while it is in
    use.

  25. MOHD ROZAIMEI BIN AHMAD

    can you give some example how to use global variable to fix this problem? i have try use that but cant works.

Leave a Reply

Trackbacks and Pingbacks:

%d bloggers like this: