Webcam Support – New in R2014a
Today I’d like to introduce a fairly frequent guest blogger Sarah Wait Zaranek who works for the MATLAB Marketing team here at The MathWorks. She and I will be writing about the new capabilities for the webcam in R2014a.
Contents
- Webcam is Available
- Installing the Support Package
- Listing Webcams and Previewing
- Taking a Single Image
- Taking Images Within a Loop
- Taking Images Within a Loop and Saving to an AVI-file
- Set up video writer
- Grab and process frames
- Taking Images Within a Loop and Creating an Animated GIF
- Here's the movie we just made
- Tidy up
- Additional Camera Support
- Do You Have a Project that Uses a Webcam?
Webcam is Available
In R2014a, you can bring live images from webcams into MATLAB.
Installing the Support Package
Webcam support is available through a hardware support package. Hardware support pacakges have existed in previous release for Simulink but now they are available for MATLAB, too.
You can find the support package installer in the resources section of the home tab of the Toolstrip. From there, choose install from Internet, select USB Webcams
And install your webcam support package.
Listing Webcams and Previewing
Now, that the webcam support is installed – let’s get started using our webcam.
You can see a list of available webcams
webcamlist
ans = 'Microsoft LifeCam Cinema' 'Integrated Camera'
You can see that Loren has two different webcams. We use the function webcam, to select which camera to use, by using either the camera name or the index in the webcamlist corresponding to the camera.
mycam = webcam('Microsoft LifeCam Cinema')
mycam = webcam with properties: Name: 'Microsoft LifeCam Cinema' Resolution: '640x480' AvailableResolutions: {1x12 cell} Brightness: 133 BacklightCompensation: 0 WhiteBalanceMode: 'auto' Saturation: 83 Zoom: 0 Pan: 0 FocusMode: 'auto' Sharpness: 25 WhiteBalance: 4500 ExposureMode: 'auto' Tilt: 0 Focus: 0 Contrast: 5 Exposure: -6
If you only have a single webcam available, that webcam will be used by default. You can use preview to check on the webcam view.
preview(mycam)
pause(10) snapnow closePreview(mycam)
You can experiment and set any properties that you may want to change. For example, you might want to change the resolution or the brightness.
mycam.Brightness = 200;
Taking a Single Image
You can acquire a single live image from your webcam.
img = snapshot(mycam);
imagesc(img)
% You can see me and Loren hanging out in her office!
Taking Images Within a Loop
You can set up a loop to acquire many images – and can process each frame within the loop. For example, we can reduce the number of distinct colors used in the image.
Grab and process frames
frames = 50; for i = 1:frames % Acquire frame for processing img = snapshot(mycam); % Quantize image by thresholding idx = img > 60 & img < 170 ; img(idx)= 255; img(~idx)= 0; % Display frame imagesc(img); axis image; axis off; end
Taking Images Within a Loop and Saving to an AVI-file
Additionally, you can create a video file of the processed frames by using VideoWriter.
Set up video writer
mywriter = VideoWriter('mymovie.avi');
open(mywriter);
Grab and process frames
frames = 50; for ii = 1:frames % Acquire frame for processing img = snapshot(mycam); % Quantize image by thresholding idx = img > 60 & img < 170 ; img(idx)= 255; img(~idx)= 0; % Display frame imagesc(img); axis image; axis off; % Write frame to video writeVideo(mywriter,img); end close(mywriter)
Taking Images Within a Loop and Creating an Animated GIF
We're going to do ALMOST the same thing we've just done, but output an animated GIF file instead.
Grab and process frames
frames = 50; filename = 'mymovie.gif'; for i = 1:frames % Acquire frame for processing img = snapshot(mycam); % Quantize image by thresholding idx = img > 60 & img < 170 ; img(idx)= 255; img(~idx)= 0; % Display frame imagesc(img) axis image; axis off; [img, cm] = rgb2ind(img, 256); if i == 1; imwrite(img,cm,filename,'gif','Loopcount',inf); else imwrite(img,cm,filename,'gif','WriteMode','append'); end end
Here's the movie we just made
Tidy up
delete(mycam)
Additional Camera Support
Support for high-end scientific and industrial cameras and more advanced features such as triggering, data logging can be found in the Image Acquisition Toolbox.
Do You Have a Project that Uses a Webcam?
Do you have a project, for school, work, or fun, where you want to use a webcam with MATLAB? Tell us about it here.
- 类别:
- Fun,
- Hardware,
- New Feature