웹캠 업데이트 로직
 if( !m_cap.isOpened() )
 {
  return;
 }
 cv::Mat frame;
 m_cap >> frame;
 D3DLOCKED_RECT lr;
 ZeroMemory( &lr, sizeof(D3DLOCKED_RECT));
 LPDIRECT3DSURFACE9 suf = NULL;
 g_pTexture->GetSurfaceLevel( 0, &suf);
 D3DSURFACE_DESC sr;
 ZeroMemory( &sr, sizeof(D3DSURFACE_DESC));
 suf->GetDesc( &sr );
 suf->LockRect( &lr, 0, 0 );
 BYTE *surfPtr = (BYTE*)(lr.pBits);
 cv::Mat     m_image;
 frame.copyTo(m_image);
 unsigned char *pSurfPtr = static_cast<unsigned char*>(lr.pBits);
 if( frame.empty() )
  return;
 IplImage p_image = m_image;
 BYTE* pBufferData;
 for(UINT h = 0; h < sr.Height; h++)
 {
  for(UINT w = 0; w < sr.Width; w++) 
  {
   pBufferData = (BYTE*)lr.pBits + (h * (lr.Pitch)) + w * 4;
   pBufferData[0] = (BYTE)(p_image.imageData[h * p_image.widthStep + w * 3 + 0]);
   pBufferData[1] = (BYTE)(p_image.imageData[h * p_image.widthStep + w * 3 + 1]);
   pBufferData[2] = (BYTE)(p_image.imageData[h * p_image.widthStep + w * 3 + 2]);
   pBufferData[3] = (BYTE)255;
  }
 }
 suf->UnlockRect( );

코드에서 외부변수는 OpenCV에서 디바이스에서 영상을 지속적으로 입력받는cv::VideoCapture 의 m_cap 과 화면상에 렌더링해줄 g_pTexture밖에 없으니 기존 D3D를 하실줄 아는 분이 OpenCV의 Video관련 샘플을 한번 본다면 쉽게 자신이 원하는 대로 수정해서 사용할수있을거라고 생각합니다.



Posted by 랜스.

Leave your greetings here.