0% found this document useful (0 votes)
811 views7 pages

Matlab - Preview and Capture Webcam Into An Axes

The document discusses creating a GUI in Matlab that allows the user to preview and capture images from a connected webcam. It provides code snippets to open the webcam, display the preview in an axes, and capture images when the user presses buttons. The code initializes the webcam, sets properties like resolution and color space, and previews the stream in one callback while another callback captures a still image and saves it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
811 views7 pages

Matlab - Preview and Capture Webcam Into An Axes

The document discusses creating a GUI in Matlab that allows the user to preview and capture images from a connected webcam. It provides code snippets to open the webcam, display the preview in an axes, and capture images when the user presses buttons. The code initializes the webcam, sets properties like resolution and color space, and previews the stream in one callback while another callback captures a still image and saves it.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

12/10/13

matlab: Preview and capture webcam into an axes

Home About Contact Sitemap PrivacyPolicy

android delphi internet Lazarus/FPC linux matlab php project review windows

Previewandcapturewebcamintoanaxes
PostedbyadminonJul7,2012inmatlab|16comments MyfirstMatlabtips.WewillcreateaGUIformthatwillpreviewandcapturepicturefromwebcamconnected toyourcomputer.Webcampreviewandcapturedimagewillbeshownonanaxes. CreateanewblankGUIusingMatlabsGUIDE(File>New>GUI>BlankGUI).Putone panel,twopushbuttonandoneaxes.Youcanarrangethemlikethis:

Arrangethecomponentlikethis(justfor example) TagtheaxesasaxPreview,firstpushbuttonaspbPreviewandsecondpushbuttonaspbCapture. WhenuserclickonpbPreviewimageformwebcamwillbeshownonaxPreview.pbCapturewillcapturetheimage,showitonaxesand saveitasanimagefile. youneedtoknowhowsMatlabdetectedyourwebcam.OpenImageAcquisitionToolbox.FromMatlabsCommandWindow,typethis: imaqtoolORyoucanfollowthisscreenshottoaccessitviaMatlabsstartmenu

www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes

1/7

12/10/13

matlab: Preview and capture webcam into an axes

accessimaqtoolviastartmenu MakesureyourwebcamisconnectedinyourcomputerbeforeMatlabrun!Ifnot,Matlabmightnotdetectyourwebcam imaqtoolwindowwillbeshown.Seethescreenshot(apartofimaqtoolform):

Matlabdetectedmywebcam Fromthescreenshot,mywebcamisdetectedaswinvideo1andMatlabalsolistsallsupportedmodethatmywebcamhas.Illuse YUY2_176x144mode(the176144numberisobviouslytheimagesize) PutthesecodesonyourpbPreview_Callback(itsnotquiteprettycolored,GeSHidoesntsupportMatlab.mfilelanguageyet).Readthe codescommentfortheexplanationlinebyline


1 %c h o o s ew h i c hw e b c a m( w i n v i d e o 1 )a n dw h i c h m o d e( Y U Y 2 _ 1 7 6 x 1 4 4 ) 2v i d=v i d e o i n p u t ( ' w i n v i d e o ' ,1 ,' Y U Y 2 _ 1 7 6 x 1 4 4 ' ) ; 3 %o n l yc a p t u r eo n ef r a m ep e rt r i g g e r ,w ea r en o tr e c o r d i n gav i d e o 4v i d . F r a m e s P e r T r i g g e r=1 ; 5 %o u t p u tw o u l di m a g ei nR G Bc o l o rs p a c e 6v i d . R e t u r n e d C o l o r s p a c e=' r g b ' ; 7 %t e l lm a t l a bt os t a r tt h ew e b c a mo nu s e rr e q u e s t ,n o ta u t o m a t i c a l l y 8t r i g g e r c o n f i g ( v i d ,' m a n u a l ' ) ; 9 %w en e e dt h i st ok n o wt h ei m a g eh e i g h ta n dw i d t h 1 0v i d R e s=g e t ( v i d ,' V i d e o R e s o l u t i o n ' ) ; 1 1 %i m a g ew i d t h 1 2i m W i d t h=v i d R e s ( 1 ) ; 1 3 %i m a g eh e i g h t 1 4i m H e i g h t=v i d R e s ( 2 ) ; 1 5 %n u m b e ro fb a n d so fo u ri m a g e( s h o u l db e3b e c a u s ei t ' sR G B ) 1 6n B a n d s=g e t ( v i d ,' N u m b e r O f B a n d s ' ) ; 1 7 %c r e a t ea ne m p t yi m a g ec o n t a i n e ra n ds h o wi to na x P r e v i e w 1 8h I m a g e=i m a g e ( z e r o s ( i m H e i g h t ,i m W i d t h ,n B a n d s ) ,' p a r e n t ' ,h a n d l e s . a x P r e v i e w ) ; 1 9 %b e g i nt h ew e b c a mp r e v i e w 2 0p r e v i e w ( v i d ,h I m a g e ) ;

asforthepbCapture_Callback.MostofcodesexplanationarethesameaspbPreview_Callback
1v i d=v i d e o i n p u t ( ' w i n v i d e o ' ,1 ,' Y U Y 2 _ 1 7 6 x 1 4 4 ' ) ; 2v i d . F r a m e s P e r T r i g g e r=1 ; 3v i d . R e t u r n e d C o l o r s p a c e=' r g b ' ; 4t r i g g e r c o n f i g ( v i d ,' m a n u a l ' ) ; 5v i d R e s=g e t ( v i d ,' V i d e o R e s o l u t i o n ' ) ; 6i m W i d t h=v i d R e s ( 1 ) ; 7i m H e i g h t=v i d R e s ( 2 ) ; 8n B a n d s=g e t ( v i d ,' N u m b e r O f B a n d s ' ) ; 9h I m a g e=i m a g e ( z e r o s ( i m H e i g h t ,i m W i d t h ,n B a n d s ) ,' p a r e n t ' ,h a n d l e s . a x P r e v i e w ) 1 0p r e v i e w ( v i d ,h I m a g e ) ; 1 1

www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes

2/7

12/10/13

matlab: Preview and capture webcam into an axes

1 2 %p r e p a r ef o rc a p t u r i n gt h ei m a g ep r e v i e w 1 3s t a r t ( v i d ) ; 1 4 %p a u s ef o r3s e c o n d st og i v eo u rw e b c a ma" w a r m u p "t i m e 1 5p a u s e ( 3 ) ; 1 6 %d oc a p t u r e ! 1 7t r i g g e r ( v i d ) ; 1 8 %s t o pt h ep r e v i e w 1 9s t o p p r e v i e w ( v i d ) ; 2 0 %g e tt h ec a p t u r e di m a g ed a t aa n ds a v ei to nc a p t 1v a r i a b l e 2 1c a p t 1=g e t d a t a ( v i d ) ; 2 2 %n o ww r i t ec a p t 1i n t of i l en a m e dc a p t u r e d . p n g 2 3i m w r i t e ( c a p t 1 ,' c a p t u r e d . p n g ' ) ; 2 4 %j u s td i a l o gt h a tw ea r ed o n ec a p t u r i n g 2 5w a r n d l g ( ' D o n e ! ' ) ;

Exampleresult:

ExampleformrunningourMatlabcodes Thingstoremember: connectyourwebcambeforerunningMatlab putapauseafewsecondsbeforecapturingimage,withoutthisyoullcaptureblack/darkimage! choosergbasReturnedColorspacebeforecapturingimage UPDATE:sincetherearepeoplethathavingdifficultiesimplementingthecode,youcandownloadthis.zipfilesoyoucanquicklytryitonyour ownMatlab.(PleaserememberthatthiscodeiscreatedusingMatlab2011aGUIDE,Idontguaranteeitwillworkonearlierversion)


DownloadMATLABdemocodestocaptureimagefromwebcam

RelatedPostsviaTaxonomies
Detectinginstalledwebcamviacommandline/mscriptfile(5) [matlab]gettingandsettingvalueinotherform/figure/gui(4) Matlabvariablesharing(globalvariableandsuchandsuch)(4) ConnectandaccessMySQLdatabase(3) Savebandwidth,blockimagehotlinking(1)

16ResponsestoPreviewandcapture webcamintoanaxes

1.

budisays: 2012/08/22at01:55 thankyouverymuch,reallyhelpalot. Reply

2.

fradosays: 2012/09/18at09:47 thanksalot,thecodeisveryeasyandfocus.

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.

JuChsays: 2012/09/24at11:51 Thankyou,justIneeded Reply

4.

lalitsays: 2012/10/08at12:37 Thankyouverymuch..Iamjustbegginnerwithmatlabandcamerainterfacing..andthishelpsmelot Reply

5.

Sevcansays: 2012/10/19at02:49 Imgratefultoyou!ThiswasmyhomeworkandIhavesearchingaboutthreehoursontheinternet. Thanksalot. Reply

6.

bosbubalussays: 2012/11/02at19:38 iuseditinmycode,butsuddenlyitstoppedworking.Idontknowwhy Reply

adminsays: 2012/11/05at09:11 Iveeditedmypost,pleasedownloadmycode(.zip)andtryit.Letmeknowiftheresstillerror. Reply

7.

chidisays: 2012/11/04at01:15 ikeepgettinganerrorregardingundefinedvariableorclasshandlesandhandles.axPreview.Howdoisolvethis? Thanks Reply

adminsays: 2012/11/05at09:12 postupdated,pleasedownloadmycodes(.zip)file.youcantryit. Reply

8.

AlexRickettssays: 2012/11/16at22:16 Wherearethezipfiles? 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

adminsays: 2012/11/17at01:24 Readthewholepost.Attheendofthepost,thereisabigdownloadbutton(DownloadMATLABdemocodestocaptureimage fromwebcam) Reply

9.

amilasays: 2012/12/04at00:27 thanksdear.. itworks. Reply

10.

jemssays: 2013/01/07at10:38 thankyouverymuch..itsworks.. but,howtousetwotypesofaxes 1axesforpreview anotheraxesforresultofcapturing thanksalot Reply

11.

SamiOgnsays: 2013/04/22at19:10 Thathelpedmereallyverymuchthanksalotforyoureffortsyouaregood )) Reply

12.

XDankesays: 2013/05/22at04:41 Gracias!!!esloqueandababuscandoyloexplicasmuybien. Reply

13.

poojasays: 2013/07/03at16:03 thankusomuch,,itsrealyhelpfullforme Reply

LeaveaReply
Youremailaddresswillnotbepublished.Requiredfieldsaremarked* 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 YoumayusetheseHTMLtagsandattributes:< ah r e f = " "t i t l e = " " >< a b b rt i t l e = " " >< a c r o n y mt i t l e = " " >< b >< b l o c k q u o t e
c i t e = " " >< c i t e >< c o d e >< d e ld a t e t i m e = " " >< e m >< i >< qc i t e = " " >< s t r i k e >< s t r o n g > SubmitComment

Notifymeoffollowupcommentsbyemail. Notifymeofnewpostsbyemail. searchourblog subscribetorss


Youremailaddress submit

populartags
androidaptasusazurebing.combioscodeigniterdatamarket
lazarus

debiandelphifpcfreepascalfreewaregadgetgisgoogleGUIDEhtaccessimage
phpexcelphpoffice

internet librarylinuxmatlabmscriptmysqlopensourcepascalpaymentproofphp scriptserver utilityvideo webservicewindowswordpressytconv


tablet treq webcam

projectreview

SponsoredLinks

SubscribetoBlogviaEmail
Enteryouremailaddresstosubscribetothisblogandreceivenotificationsofnewpostsbyemail. Join21othersubscribers
EmailAddress 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

December2012 October2012 August2012 July2012 June2012 February2012 January2012

FeaturedPosts
NuSpherePhpEDcodecompletionforCodeIgniterFramework CreatingMsWordDocumentusingCodeIgniterandPHPWord YtConv.nethasbeenreportedasattacksitebyGoogleSafebrowsing Detectinginstalledwebcamviacommandline/mscriptfile

RSSFeeds
RSS2.0FeedURL AtomFeedURL CommentsRSS2.0Feed RSS/RDF1.0FeedURL DesignedbyElegantThemes|PoweredbyWordpress

www.ahowto.net/matlab/preview-and-capture-webcam-into-an-axes

7/7

You might also like