title: Get iplImage or Mat from directshow to opencv
body: <p>I had to change to directshow for my eyetracking software due to the difficulties to change resolution of the camera when using c++ and opencv. </p>
  
  <p>Directshow is new to me and it is kind of hard to understand everything. But I found this nice example that works perfectly for capturing &amp; viewing the web cam. </p>
  
  <p><a href="http://www.codeproject.com/Articles/12869/Real-time-video-image-processing-frame-grabber-usi" rel="nofollow">http://www.codeproject.com/Articles/12869/Real-time-video-image-processing-frame-grabber-usi</a></p>
  
  <p>I am using the version that not requires directShow SDK. (But it is still directshow that is used in the example, right??)</p>
  
  <pre><code>#include &lt;windows.h&gt;
  #include &lt;dshow.h&gt;
  
  #pragma comment(lib,"Strmiids.lib")
  
  #define DsHook(a,b,c) if (!c##_) { INT_PTR* p=b+*(INT_PTR**)a;   VirtualProtect(&amp;c##_,4,PAGE_EXECUTE_READWRITE,&amp;no);\
                                            *(INT_PTR*)&amp;c##_=*p;   VirtualProtect(p,    4,PAGE_EXECUTE_READWRITE,&amp;no);   *p=(INT_PTR)c; }
  
  
  // Here you get image video data in buf / len. Process it before calling Receive_ because renderer dealocates it.
  HRESULT ( __stdcall * Receive_ ) ( void* inst, IMediaSample *smp ) ; 
  HRESULT   __stdcall   Receive    ( void* inst, IMediaSample *smp ) {     
      BYTE*     buf;    smp-&gt;GetPointer(&amp;buf); DWORD len = smp-&gt;GetActualDataLength();
      HRESULT   ret  =  Receive_   ( inst, smp );   
      return    ret; 
  }
  
  int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show){
      HRESULT hr = CoInitialize(0); MSG msg={0}; DWORD no;
  
      IGraphBuilder*  graph= 0;  hr = CoCreateInstance( CLSID_FilterGraph, 0, CLSCTX_INPROC,IID_IGraphBuilder, (void **)&amp;graph );
      IMediaControl*  ctrl = 0;  hr = graph-&gt;QueryInterface( IID_IMediaControl, (void **)&amp;ctrl );
  
      ICreateDevEnum* devs = 0;  hr = CoCreateInstance (CLSID_SystemDeviceEnum, 0, CLSCTX_INPROC, IID_ICreateDevEnum, (void **) &amp;devs);
      IEnumMoniker*   cams = 0;  hr = devs?devs-&gt;CreateClassEnumerator (CLSID_VideoInputDeviceCategory, &amp;cams, 0):0;  
      IMoniker*       mon  = 0;  hr = cams-&gt;Next (1,&amp;mon,0);  // get first found capture device (webcam?)    
      IBaseFilter*    cam  = 0;  hr = mon-&gt;BindToObject(0,0,IID_IBaseFilter, (void**)&amp;cam);
                                 hr = graph-&gt;AddFilter(cam, L"Capture Source"); // add web cam to graph as source
      IEnumPins*      pins = 0;  hr = cam?cam-&gt;EnumPins(&amp;pins):0;   // we need output pin to autogenerate rest of the graph
      IPin*           pin  = 0;  hr = pins?pins-&gt;Next(1,&amp;pin, 0):0; // via graph-&gt;Render
                                 hr = graph-&gt;Render(pin); // graph builder now builds whole filter chain including MJPG decompression on some webcams
      IEnumFilters*   fil  = 0;  hr = graph-&gt;EnumFilters(&amp;fil); // from all newly added filters
      IBaseFilter*    rnd  = 0;  hr = fil-&gt;Next(1,&amp;rnd,0); // we find last one (renderer)
                                 hr = rnd-&gt;EnumPins(&amp;pins);  // because data we are intersted in are pumped to renderers input pin 
                                 hr = pins-&gt;Next(1,&amp;pin, 0); // via Receive member of IMemInputPin interface
      IMemInputPin*   mem  = 0;  hr = pin-&gt;QueryInterface(IID_IMemInputPin,(void**)&amp;mem);
  
      DsHook(mem,6,Receive); // so we redirect it to our own proc to grab image data
  
      hr = ctrl-&gt;Run();   
  
      while ( GetMessage(   &amp;msg, 0, 0, 0 ) ) {  
          TranslateMessage( &amp;msg );   
          DispatchMessage(  &amp;msg ); 
      }
  };
  </code></pre>
  
  <p>The method HRESULT Receive is called for every new frame from the cam. the the comments says that buf contains the data. But I have 3 problems/questions.</p>
  
  <ol>
  <li><p>I cant include the opencv lib. I create a new project in visual studio, and add the same property sheets as I always include.  the only difference from earlier projects is that I Now create a totaly empty project, earlier I created a win32 application.
  How to add opencv into the directshow project?</p></li>
  <li><p>The example above. from buf. which is a pointer to the data. How do I get that into iplImage/Mat for the opencv calc?</p></li>
  <li><p>Is there a way to not show the images from the webcam (I only need to perform some algorithms on the frames, I guess removing the window with the results might give me more power for the analyse algorithms?!)</p></li>
  </ol>
  
  <p>Thanks!</p>