<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:dc="http://purl.org/dc/elements/1.1/" >

<channel><title><![CDATA[Pulkit Budhiraja - Research Blog]]></title><link><![CDATA[http://www.pulkitbudhiraja.com/research-blog]]></link><description><![CDATA[Research Blog]]></description><pubDate>Mon, 17 May 2021 16:45:43 -0500</pubDate><generator>Weebly</generator><item><title><![CDATA[Fast way to use webcam stream as a texture using EmguCV]]></title><link><![CDATA[http://www.pulkitbudhiraja.com/research-blog/fast-way-to-use-webcam-stream-as-a-texture-using-emgucv]]></link><comments><![CDATA[http://www.pulkitbudhiraja.com/research-blog/fast-way-to-use-webcam-stream-as-a-texture-using-emgucv#comments]]></comments><pubDate>Wed, 08 Apr 2015 05:59:29 GMT</pubDate><category><![CDATA[Code]]></category><category><![CDATA[EmguCV]]></category><category><![CDATA[Unity3D]]></category><guid isPermaLink="false">http://www.pulkitbudhiraja.com/research-blog/fast-way-to-use-webcam-stream-as-a-texture-using-emgucv</guid><description><![CDATA[Unity3D allows the ability to render webcam stream as a texture using the class WebCamTexture. While this was useful to render raw webcam data, my application required doing some OpenCV processing on the webcam data before rendering it in the scene.&nbsp;The default data type to store image data with EmguCV is Image&lt;TColor, TDepth&gt;. In the Unity land however, textures do not have any parsing capability &nbsp;for Image&lt;TColor, TDepth&gt; but only allow Color32[] data to be set as texture [...] ]]></description><content:encoded><![CDATA[<div class="paragraph" style="text-align:left;">Unity3D allows the ability to render webcam stream as a texture using the class <a href="http://docs.unity3d.com/ScriptReference/WebCamTexture.html" target="_blank" title="">WebCamTexture</a>. While this was useful to render raw webcam data, my application required doing some OpenCV processing on the webcam data before rendering it in the scene.&nbsp;<br><br>The default data type to store image data with EmguCV is Image&lt;TColor, TDepth&gt;. In the Unity land however, textures do not have any parsing capability &nbsp;for Image&lt;TColor, TDepth&gt; but only allow Color32[] data to be set as textures. A quick google search did not help much to find ways of converting Image&lt;TColor, TDepth&gt; to Color32[]. I found some <a href="http://stackoverflow.com/questions/24464378/emgucv-image-to-texture2d-on-unity" target="_blank" title="">code</a>&nbsp;that uses Bitmap and MemoryStream &nbsp;but couldn't get it to work.&nbsp;<br><br>Eventually, I found <a href="http://docs.unity3d.com/ScriptReference/Texture2D.LoadRawTextureData.html" target="_blank" title="">Texture2D.LoadRawTextureData()</a>&nbsp;but couldn't find any good reference to use it with Image&lt;TColor, TDepth&gt;. After much playing around and some coffees later, I got it to work. Here is my code:<br></div><div><div id="379073743755009252" align="left" style="width: 100%; overflow-y: hidden;" class="wcustomhtml"><!-- code formatted by http://manoli.net/csharpformat/ --><style type="text/css">.csharpcode, .csharpcode pre{        font-size: small;        color: black;        font-family: Consolas, "Courier New", Courier, Monospace;        background-color: #ffffff;        /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #a31515; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt {        background-color: #f4f4f4;        width: 100%;        margin: 0em;}.csharpcode .lnum { color: #606060; }</style><pre class="csharpcode"><span class="kwrd">using</span> UnityEngine;<span class="kwrd">using</span> System.Collections;<span class="kwrd">using</span> Emgu.CV;<span class="kwrd">using</span> Emgu.Util;<span class="kwrd">using</span> Emgu.CV.Structure;<span class="kwrd">using</span> System;<span class="kwrd">public</span> <span class="kwrd">class</span> WebcamCaptureCV : MonoBehaviour {    <span class="kwrd">private</span> <span class="kwrd">int</span> frameWidth;    <span class="kwrd">private</span> <span class="kwrd">int</span> frameHeight;    <span class="kwrd">private</span> Capture cvCapture;    <span class="kwrd">private</span> Image&lt;Bgr, <span class="kwrd">byte</span>&gt; currentFrameBgr;    <span class="kwrd">private</span> Image&lt;Rgb, <span class="kwrd">byte</span>&gt; currentFrameRgb;    <span class="kwrd">private</span> Image&lt;Bgra, <span class="kwrd">byte</span>&gt; currentFrameBgra;    <span class="kwrd">private</span> Texture2D tex;    <span class="kwrd">void</span> Start () {            cvCapture = <span class="kwrd">new</span> Capture(0);                  cvCapture.FlipVertical = <span class="kwrd">true</span>;    <span class="rem">//The image I am getting from webcam is flipped </span>        cvCapture.ImageGrabbed += ProcessFrame;        frameWidth = (<span class="kwrd">int</span>) cvCapture.GetCaptureProperty (Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_WIDTH);        frameHeight = (<span class="kwrd">int</span>) cvCapture.GetCaptureProperty (Emgu.CV.CvEnum.CAP_PROP.CV_CAP_PROP_FRAME_HEIGHT);        tex = <span class="kwrd">new</span> Texture2D (frameWidth, frameHeight,  TextureFormat.BGRA32, <span class="kwrd">false</span>);        gameObject.renderer.material.mainTexture = tex;        currentFrameBgr = <span class="kwrd">new</span> Image&lt;Bgr, <span class="kwrd">byte</span>&gt; (frameWidth, frameHeight);        currentFrameRgb = <span class="kwrd">new</span> Image&lt;Rgb, <span class="kwrd">byte</span>&gt; (frameWidth, frameHeight);        currentFrameBgra = <span class="kwrd">new</span> Image&lt;Bgra, <span class="kwrd">byte</span>&gt; (frameWidth, frameHeight);        cvCapture.Start();    }    <span class="kwrd">private</span> <span class="kwrd">void</span> ProcessFrame(<span class="kwrd">object</span> sender, EventArgs arg)    {                currentFrameBgr = cvCapture.RetrieveBgrFrame ();                currentFrameRgb.Bytes = currentFrameBgr.Bytes;                currentFrameBgra = currentFrameRgb.Convert&lt;Bgra, <span class="kwrd">byte</span>&gt;();             <span class="rem">//    showImageDebug&lt;Rgb, Byte&gt; (currentFrameRgb);</span>    }    <span class="kwrd">void</span> Update () {                    tex.LoadRawTextureData(currentFrameBgra.Bytes);                    tex.Apply();    }    <span class="kwrd">void</span> OnDestroy(){        cvCapture.Stop();    }    <span class="kwrd">private</span> <span class="kwrd">void</span> showImageDebug&lt;TColor, TDepth&gt;(Image&lt;TColor, TDepth&gt; img, <span class="kwrd">string</span> windowName=<span class="str">"Debug Window"</span> ) where TColor : <span class="kwrd">struct</span>, IColor where TDepth : <span class="kwrd">struct</span>    {        CvInvoke.cvShowImage(windowName, img);         CvInvoke.cvWaitKey(0);          CvInvoke.cvDestroyWindow(windowName);     }}</pre></div></div>]]></content:encoded></item></channel></rss>