Tag Archives: capture

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
% 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
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
% pause for 3 seconds to give our webcam a "warm-up" time
% do capture!
% stop the preview
% 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
  • 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:

guidata(hObject, handles);

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

if ~isfield(handles, 'vid')
    warndlg('Please do the preview first!');

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