{"id":932,"date":"2014-06-17T07:30:36","date_gmt":"2014-06-17T12:30:36","guid":{"rendered":"https:\/\/blogs.mathworks.com\/loren\/?p=932"},"modified":"2017-08-28T20:21:35","modified_gmt":"2017-08-29T01:21:35","slug":"webcam-support-new-in-r2014a","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/loren\/2014\/06\/17\/webcam-support-new-in-r2014a\/","title":{"rendered":"Webcam Support \u2013 New in R2014a"},"content":{"rendered":"<div class=\"content\"><!--introduction--><p>Today I&#8217;d like to introduce a fairly frequent guest blogger <a href=\"mailto:sarah.zaranek@mathworks.com\">Sarah Wait Zaranek<\/a> 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.<\/p><!--\/introduction--><h3>Contents<\/h3><div><ul><li><a href=\"#43894050-3cd4-424f-8cb1-aa0536a4100e\">Webcam is Available<\/a><\/li><li><a href=\"#801894d3-7bac-423e-90c2-487911b7f817\">Installing the Support Package<\/a><\/li><li><a href=\"#2737eb70-c285-430d-85da-69b74266c342\">Listing Webcams and Previewing<\/a><\/li><li><a href=\"#a9f831ed-ff4b-4378-80d1-1724a54709ff\">Taking a Single Image<\/a><\/li><li><a href=\"#4fa8da2d-7bbc-4636-9e8d-a0ff271442b1\">Taking Images Within a Loop<\/a><\/li><li><a href=\"#17f6a8d4-3d66-4f5d-8be7-daf2c6018591\">Taking Images Within a Loop and Saving to an AVI-file<\/a><\/li><li><a href=\"#cd77fc3c-83b9-441e-857a-81df2d6af5ef\">Set up video writer<\/a><\/li><li><a href=\"#0652cfae-18b1-4032-b4c6-bcaabcbd6a50\">Grab and process frames<\/a><\/li><li><a href=\"#4eb700f2-1814-4d3b-a3a6-15bf76c302bf\">Taking Images Within a Loop and Creating an Animated GIF<\/a><\/li><li><a href=\"#e6badf56-5dd9-4649-9525-b7bb1ef5c1bc\">Here's the movie we just made<\/a><\/li><li><a href=\"#0f64dce9-da3c-4c32-9bbd-dae387a103c7\">Tidy up<\/a><\/li><li><a href=\"#3f978f03-06d7-4657-be3a-2398602f1ad1\">Additional Camera Support<\/a><\/li><li><a href=\"#08b47b2f-e995-4f5c-a1db-df4fd5f72dd8\">Do You Have a Project that Uses a Webcam?<\/a><\/li><\/ul><\/div><h4>Webcam is Available<a name=\"43894050-3cd4-424f-8cb1-aa0536a4100e\"><\/a><\/h4><p>In R2014a, you can bring live images from webcams into MATLAB.<\/p><h4>Installing the Support Package<a name=\"801894d3-7bac-423e-90c2-487911b7f817\"><\/a><\/h4><p>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.<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/HardwareSupport-Figure1.png\" alt=\"\"> <\/p><p>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<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/HardwareSupport-Figure2.png\" alt=\"\"> <\/p><p>And install your webcam support package.<\/p><h4>Listing Webcams and Previewing<a name=\"2737eb70-c285-430d-85da-69b74266c342\"><\/a><\/h4><p>Now, that the webcam support is installed &#8211; let&#8217;s get started using our webcam.<\/p><p>You can see a list of available webcams<\/p><pre class=\"codeinput\">webcamlist\r\n<\/pre><pre class=\"codeoutput\">ans = \r\n    'Microsoft LifeCam Cinema'\r\n    'Integrated Camera'\r\n<\/pre><p>You can see that Loren has two different webcams. We use the function <tt>webcam<\/tt>, to select which camera to use, by using either the camera name or the index in the <tt>webcamlist<\/tt> corresponding to the camera.<\/p><pre class=\"codeinput\">mycam = webcam(<span class=\"string\">'Microsoft LifeCam Cinema'<\/span>)\r\n<\/pre><pre class=\"codeoutput\">mycam = \r\n  webcam with properties:\r\n\r\n                     Name: 'Microsoft LifeCam Cinema'\r\n               Resolution: '640x480'\r\n     AvailableResolutions: {1x12 cell}\r\n               Brightness: 133\r\n    BacklightCompensation: 0\r\n         WhiteBalanceMode: 'auto'\r\n               Saturation: 83\r\n                     Zoom: 0\r\n                      Pan: 0\r\n                FocusMode: 'auto'\r\n                Sharpness: 25\r\n             WhiteBalance: 4500\r\n             ExposureMode: 'auto'\r\n                     Tilt: 0\r\n                    Focus: 0\r\n                 Contrast: 5\r\n                 Exposure: -6\r\n<\/pre><p>If you only have a single webcam available, that webcam will be used by default.  You can use <tt>preview<\/tt> to check on the webcam view.<\/p><pre class=\"codeinput\">preview(mycam)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_01.png\" alt=\"\"> <pre class=\"codeinput\">pause(10)\r\nsnapnow\r\nclosePreview(mycam)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_02.png\" alt=\"\"> <p>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.<\/p><pre class=\"codeinput\">mycam.Brightness =  200;\r\n<\/pre><h4>Taking a Single Image<a name=\"a9f831ed-ff4b-4378-80d1-1724a54709ff\"><\/a><\/h4><p>You can acquire a single live image from your webcam.<\/p><pre class=\"codeinput\">img = snapshot(mycam);\r\nimagesc(img)\r\n\r\n<span class=\"comment\">% You can see me and Loren hanging out in her office!<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_03.png\" alt=\"\"> <h4>Taking Images Within a Loop<a name=\"4fa8da2d-7bbc-4636-9e8d-a0ff271442b1\"><\/a><\/h4><p>You can set up a loop to acquire many images &#8211; and can process each frame within the loop. For example, we can reduce the number of distinct colors used in the image.<\/p><p>Grab and process frames<\/p><pre class=\"codeinput\">frames = 50;\r\n\r\n<span class=\"keyword\">for<\/span> i = 1:frames\r\n    <span class=\"comment\">% Acquire frame for processing<\/span>\r\n    img = snapshot(mycam);\r\n\r\n    <span class=\"comment\">% Quantize image by thresholding<\/span>\r\n    idx = img &gt; 60 &amp; img &lt; 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n\r\n    <span class=\"comment\">% Display frame<\/span>\r\n    imagesc(img);\r\n    axis <span class=\"string\">image<\/span>;\r\n    axis <span class=\"string\">off<\/span>;\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_04.png\" alt=\"\"> <h4>Taking Images Within a Loop and Saving to an AVI-file<a name=\"17f6a8d4-3d66-4f5d-8be7-daf2c6018591\"><\/a><\/h4><p>Additionally, you can create a video file of the processed frames by using <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/videowriter.html\"><tt>VideoWriter<\/tt><\/a>.<\/p><h4>Set up video writer<a name=\"cd77fc3c-83b9-441e-857a-81df2d6af5ef\"><\/a><\/h4><pre class=\"codeinput\">mywriter = VideoWriter(<span class=\"string\">'mymovie.avi'<\/span>);\r\nopen(mywriter);\r\n<\/pre><h4>Grab and process frames<a name=\"0652cfae-18b1-4032-b4c6-bcaabcbd6a50\"><\/a><\/h4><pre class=\"codeinput\">frames = 50;\r\n\r\n<span class=\"keyword\">for<\/span> ii = 1:frames\r\n    <span class=\"comment\">% Acquire frame for processing<\/span>\r\n    img = snapshot(mycam);\r\n\r\n    <span class=\"comment\">% Quantize image by thresholding<\/span>\r\n    idx = img &gt; 60 &amp; img &lt; 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n\r\n    <span class=\"comment\">% Display frame<\/span>\r\n    imagesc(img);\r\n    axis <span class=\"string\">image<\/span>;\r\n    axis <span class=\"string\">off<\/span>;\r\n\r\n    <span class=\"comment\">% Write frame to video<\/span>\r\n    writeVideo(mywriter,img);\r\n<span class=\"keyword\">end<\/span>\r\nclose(mywriter)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_05.png\" alt=\"\"> <h4>Taking Images Within a Loop and Creating an Animated GIF<a name=\"4eb700f2-1814-4d3b-a3a6-15bf76c302bf\"><\/a><\/h4><p>We're going to do ALMOST the same thing we've just done, but output an animated GIF file instead.<\/p><p>Grab and process frames<\/p><pre class=\"codeinput\">frames = 50;\r\nfilename = <span class=\"string\">'mymovie.gif'<\/span>;\r\n\r\n<span class=\"keyword\">for<\/span> i = 1:frames\r\n    <span class=\"comment\">% Acquire frame for processing<\/span>\r\n    img = snapshot(mycam);\r\n\r\n    <span class=\"comment\">% Quantize image by thresholding<\/span>\r\n    idx = img &gt; 60 &amp; img &lt; 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n\r\n    <span class=\"comment\">% Display frame<\/span>\r\n    imagesc(img)\r\n    axis <span class=\"string\">image<\/span>;\r\n    axis <span class=\"string\">off<\/span>;\r\n\r\n    [img, cm] = rgb2ind(img, 256);\r\n    <span class=\"keyword\">if<\/span> i == 1;\r\n        imwrite(img,cm,filename,<span class=\"string\">'gif'<\/span>,<span class=\"string\">'Loopcount'<\/span>,inf);\r\n    <span class=\"keyword\">else<\/span>\r\n        imwrite(img,cm,filename,<span class=\"string\">'gif'<\/span>,<span class=\"string\">'WriteMode'<\/span>,<span class=\"string\">'append'<\/span>);\r\n    <span class=\"keyword\">end<\/span>\r\n<span class=\"keyword\">end<\/span>\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_06.png\" alt=\"\"> <h4>Here's the movie we just made<a name=\"e6badf56-5dd9-4649-9525-b7bb1ef5c1bc\"><\/a><\/h4><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/mymovie.gif\" alt=\"\"> <\/p><h4>Tidy up<a name=\"0f64dce9-da3c-4c32-9bbd-dae387a103c7\"><\/a><\/h4><pre class=\"codeinput\">delete(mycam)\r\n<\/pre><h4>Additional Camera Support<a name=\"3f978f03-06d7-4657-be3a-2398602f1ad1\"><\/a><\/h4><p>Support for high-end scientific and industrial cameras and more advanced features such as triggering, data logging can be found in the <a href=\"https:\/\/www.mathworks.com\/products\/imaq\/\">Image Acquisition Toolbox<\/a>.<\/p><h4>Do You Have a Project that Uses a Webcam?<a name=\"08b47b2f-e995-4f5c-a1db-df4fd5f72dd8\"><\/a><\/h4><p>Do you have a project, for school, work, or fun, where you want to use a webcam with MATLAB?  Tell us about it <a href=\"https:\/\/blogs.mathworks.com\/loren\/?p=932#respond\">here<\/a>.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_b1be64109d8b4e27aa380d3dab44573c() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='b1be64109d8b4e27aa380d3dab44573c ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' b1be64109d8b4e27aa380d3dab44573c';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        copyright = 'Copyright 2014 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n\r\n        d.title = title + ' (MATLAB code)';\r\n        d.close();\r\n    }   \r\n     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_b1be64109d8b4e27aa380d3dab44573c()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2014a<br><\/p><\/div><!--\r\nb1be64109d8b4e27aa380d3dab44573c ##### SOURCE BEGIN #####\r\n%% Webcam Support \u00e2\u20ac\u201c New in R2014a\r\n% Today I\u00e2\u20ac\u2122d like to introduce a fairly frequent guest blogger\r\n% <mailto:sarah.zaranek@mathworks.com Sarah Wait Zaranek> who works for the\r\n% MATLAB Marketing team here at The MathWorks. She and I will be writing\r\n% about the new capabilities for the webcam in R2014a.\r\n%% Webcam is Available\r\n% In R2014a, you can bring live images from webcams into MATLAB.\r\n\r\n%% Installing the Support Package\r\n% Webcam support is available through a hardware support package. Hardware\r\n% support pacakges have existed in previous release for Simulink but now\r\n% they are available for MATLAB, too.\r\n%\r\n% <<HardwareSupport-Figure1.png>>\r\n%\r\n% You can find the support package installer in the resources section of\r\n% the home tab of the Toolstrip.  From there, choose install from\r\n% Internet, select USB Webcams\r\n%\r\n% <<HardwareSupport-Figure2.png>>\r\n%\r\n% And install your webcam support package.\r\n%\r\n%% Listing Webcams and Previewing\r\n% Now, that the webcam support is installed \u00e2\u20ac\u201c let\u00e2\u20ac\u2122s get started using our\r\n% webcam.\r\n%\r\n% You can see a list of available webcams\r\n\r\nwebcamlist\r\n\r\n%%\r\n% You can see that Loren has two different webcams. We use the function <\r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/webcam.html |webcam|>, to\r\n% select which camera to use, by using either the camera name or the index\r\n% in the |webcamlist| corresponding to the camera.\r\nmycam = webcam('Microsoft LifeCam Cinema')\r\n\r\n%%\r\n% If you only have a single webcam available, that webcam will be used by\r\n% default.  You can use |preview| to check on the webcam view.\r\n\r\npreview(mycam)\r\n%%\r\npause(10)\r\nsnapnow\r\nclosePreview(mycam)\r\n\r\n\r\n%%\r\n% You can experiment and set any properties that you may want to change.\r\n% For example, you might want to change the resolution or the brightness.\r\n\r\nmycam.Brightness =  200;\r\n\r\n\r\n%% Taking a Single Image\r\n% You can acquire a single live image from your webcam.\r\n\r\nimg = snapshot(mycam);\r\nimagesc(img)\r\n\r\n% You can see me and Loren hanging out in her office!\r\n\r\n%% Taking Images Within a Loop\r\n% You can set up a loop to acquire many images \u00e2\u20ac\u201c and can process each frame\r\n% within the loop. For example, we can reduce the number of distinct colors\r\n% used in the image.\r\n%\r\n% Grab and process frames\r\nframes = 50;\r\n\r\nfor i = 1:frames\r\n    % Acquire frame for processing\r\n    img = snapshot(mycam);\r\n    \r\n    % Quantize image by thresholding\r\n    idx = img > 60 & img < 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n    \r\n    % Display frame\r\n    imagesc(img);\r\n    axis image;\r\n    axis off;\r\nend\r\n\r\n%% Taking Images Within a Loop and Saving to an AVI-file\r\n% Additionally, you can create a video file of the processed frames by\r\n% using <https:\/\/www.mathworks.com\/help\/matlab\/ref\/videowriter-class.html\r\n% |VideoWriter|>.\r\n%% Set up video writer\r\nmywriter = VideoWriter('mymovie.avi');\r\nopen(mywriter);\r\n\r\n%% Grab and process frames\r\nframes = 50;\r\n\r\nfor ii = 1:frames\r\n    % Acquire frame for processing\r\n    img = snapshot(mycam);\r\n    \r\n    % Quantize image by thresholding\r\n    idx = img > 60 & img < 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n    \r\n    % Display frame\r\n    imagesc(img);\r\n    axis image;\r\n    axis off;\r\n    \r\n    % Write frame to video\r\n    writeVideo(mywriter,img);\r\nend\r\nclose(mywriter)\r\n\r\n%% Taking Images Within a Loop and Creating an Animated GIF\r\n% We're going to do ALMOST the same thing we've just done, but output an\r\n% animated GIF file instead.\r\n%\r\n% Grab and process frames\r\nframes = 50;\r\nfilename = 'mymovie.gif';\r\n\r\nfor i = 1:frames\r\n    % Acquire frame for processing\r\n    img = snapshot(mycam);\r\n    \r\n    % Quantize image by thresholding\r\n    idx = img > 60 & img < 170 ;\r\n    img(idx)= 255;\r\n    img(~idx)= 0;\r\n    \r\n    % Display frame\r\n    imagesc(img)\r\n    axis image;\r\n    axis off;\r\n    \r\n    [img, cm] = rgb2ind(img, 256);\r\n    if i == 1;\r\n        imwrite(img,cm,filename,'gif','Loopcount',inf);\r\n    else\r\n        imwrite(img,cm,filename,'gif','WriteMode','append');\r\n    end\r\nend\r\n\r\n%% Here's the movie we just made\r\n%\r\n% <<mymovie.gif>>\r\n%\r\n\r\n\r\n%% Tidy up\r\ndelete(mycam)\r\n\r\n\r\n\r\n%% Additional Camera Support\r\n% Support for high-end scientific and industrial cameras and more advanced\r\n% features such as triggering, data logging can be found in the\r\n% <https:\/\/www.mathworks.com\/products\/imaq\/ Image Acquisition Toolbox>.\r\n\r\n%% Do You Have a Project that Uses a Webcam?\r\n% Do you have a project, for school, work, or fun, where you want to use a\r\n% webcam with MATLAB?  Tell us about it\r\n% <https:\/\/blogs.mathworks.com\/loren\/?p=932#respond here>.\r\n##### SOURCE END ##### b1be64109d8b4e27aa380d3dab44573c\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/images\/loren\/2014\/WebcamSupportBlog_06.png\" onError=\"this.style.display ='none';\" \/><\/div><!--introduction--><p>Today I&#8217;d like to introduce a fairly frequent guest blogger <a href=\"mailto:sarah.zaranek@mathworks.com\">Sarah Wait Zaranek<\/a> 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.... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/loren\/2014\/06\/17\/webcam-support-new-in-r2014a\/\">read more >><\/a><\/p>","protected":false},"author":39,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[33,49,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/932"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/users\/39"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/comments?post=932"}],"version-history":[{"count":9,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/932\/revisions"}],"predecessor-version":[{"id":2409,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/posts\/932\/revisions\/2409"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/media?parent=932"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/categories?post=932"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/loren\/wp-json\/wp\/v2\/tags?post=932"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}