12/ 10/ 13
m atlab: Preview and capture w ebcam into an ax es
Home About Contact Sitemap Privacy Policy
android delphi internet Lazarus/FPC linux matlab php project review windows
Preview and capture webcam into an axes Posted by ad admin min on on Jul 7, 2012 in matlab matlab | | 16 comments My first Ma first Matlab tlab 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 GUI using Matl Matlab’s ab’s GUIDE ( GUIDE (File File –> New –> GUI –> Blank GUI). GUI ). Put one panel, two pushbutton and one axes. You can arrange them like this:
Arrange the component like this (just for example) Tag the axes as axPreview, axPreview, first pushbutton as pbPreview pbPreview and second pushbutton as pbCapture. When user click on pbPreview on pbPreview image image form webcam will be shown on axPreview. axPreview . pbCapture will 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 Open Image Acquisitio Acquisition n Toolbox T oolbox.. From Matl From Matlab’s ab’s Command Window, Window, type this: imaqtool OR you can follow this screenshot to access it via Matlab’s start menu
w w w . ahow to. net/ matlab/preview -and-capture-w ebcam-into-an-ax es
1/ 7
12/10/13
matlab: Preview and capture webcam into an axes
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 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
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);
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
2/7
12/10/13 12 13 14 15 16 17 18 19 20 21 22 23 24 25
matlab: Preview and capture webcam into an axes
% 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:
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
Related Posts via Taxonomies Detecting installed webcam via command line/m script file (5) [matlab] getting and setting value in other form/figure/gui (4) Matlab variable sharing (global variable and such and such…) (4) Connect and access MySQL database (3) Save bandwidth, block image hot linking (1)
16 Responses to “Preview and capture webcam into an axes”
1.
budi says: 2012/08/22 at 01:55 thank you very much, really help a lot. Reply
2.
frado says: 2012/09/18 at 09:47 thanks a lot, the code is very easy and focus….
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
3/7
12/10/13
matlab: Preview and capture webcam into an axes
Reply
3.
JuCh says: 2012/09/24 at 11:51 Thank you, just I needed Reply
4.
lalit says: 2012/10/08 at 12:37 Thank you very much.. I am just begginner with matlab and camera interfacing.. and this helps me lot… Reply
5.
Sevcan says: 2012/10/19 at 02:49 I’m grateful to you! This was my homework and I have searching about three hours on the internet. Thanks a lot. Reply
6.
bosbubalus says: 2012/11/02 at 19:38 i used it in my code, but suddenly it stopped working. I don’t know why Reply
admin says: 2012/11/05 at 09:11 I’ve edited my post, please download my code (.zip) and try it. Let me know if there’s still error. Reply
7.
chidi says: 2012/11/04 at 01:15 i keep getting an error regarding undefined variable or class “handles” and “handles.axPreview”. How do i solve this? Thanks Reply
admin says: 2012/11/05 at 09:12 post updated, please download my codes (.zip) file. you can try it. Reply
8.
Alex Ricketts says: 2012/11/16 at 22:16 Where are the zip files? Reply
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
4/7
12/10/13
matlab: Preview and capture webcam into an axes
admin says: 2012/11/17 at 01:24 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“) Reply
9.
amila says: 2012/12/04 at 00:27 thanks dear.. it works. Reply
10.
jems says: 2013/01/07 at 10:38 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 Reply
11.
Sami Ogün says: 2013/04/22 at 19:10 That helped me really very much thanks a lot for your efforts you are good
))
Reply
12.
XDanke says: 2013/05/22 at 04:41 Gracias!!! es lo que andaba buscando y lo explicas muy bien. Reply
13.
pooja says: 2013/07/03 at 16:03 thank u so much,,it’s realy helpfull for me… Reply
Leave a Reply Your email address will not be published. Required fields are marked * Name * Email * Website
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
5/7
12/10/13
matlab: Preview and capture webcam into an axes
Comment You may use these HTML tags and attributes:
Submit Comment
Notify me of follow-up comments by email. Notify me of new posts by email. search our blog
subscribe to rss Your email address submit
popular tags android apt
asus azure
bing.com bios codeigniter
datamarket
debian delphi fpc freepascal freeware gadget gis google GUIDE htaccess image
internet lazarus library linux matlab m script mysql open source pascal payment proof php phpexcel phpoffice project review script server tablet treq utility video webcam web service windows wordpress ytconv Sponsored Links
Subscribe to Blog via Email Enter your email address to subscribe to this blog and receive notifications of new posts by email. Join 21 other subscribers Email Address
Subscribe
Badge
Archives
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
6/7
12/10/13
matlab: Preview and capture webcam into an axes
December 2012 October 2012 August 2012 July 2012 June 2012 February 2012 January 2012
Featured Posts NuSphere PhpED code completion for CodeIgniter Framework Creating Ms Word Document using CodeIgniter and PHPWord YtConv.net has been reported as attack site by Google Safebrowsing Detecting installed webcam via command line/m script file
RSS Feeds RSS 2.0 Feed URL Atom Feed URL Comments RSS 2.0 Feed RSS/RDF 1.0 Feed URL Designed by Elegant Themes | Powered by Wordpress
www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes
7/7