3/15/2014

OpenCV Study, IplImage * display to PictureBox on Dialog (MFC), example source code

void DisplayIplImageToPictureBox(IplImage* pImgIpl, CDC* pDC, CRect rect)
{
 BITMAPINFO bitmapInfo;
 bitmapInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
 bitmapInfo.bmiHeader.biPlanes=1;
 bitmapInfo.bmiHeader.biCompression=BI_RGB;
 bitmapInfo.bmiHeader.biXPelsPerMeter=100;
 bitmapInfo.bmiHeader.biYPelsPerMeter=100;
 bitmapInfo.bmiHeader.biClrUsed=0;
 bitmapInfo.bmiHeader.biClrImportant=0;
 bitmapInfo.bmiHeader.biSizeImage=0;
 bitmapInfo.bmiHeader.biWidth=pImgIpl->width;
 bitmapInfo.bmiHeader.biHeight=-pImgIpl->height;

 IplImage* tempImage=NULL;

 if(pImgIpl->nChannels == 3)
 {
  tempImage = (IplImage*)cvClone(pImgIpl);
  bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;

 }
 else if(pImgIpl->nChannels == 1)
 {
  tempImage =  cvCreateImage(cvGetSize(pImgIpl), IPL_DEPTH_8U, 3);
  cvCvtColor(pImgIpl, tempImage, CV_GRAY2BGR);
  bitmapInfo.bmiHeader.biBitCount=tempImage->depth * tempImage->nChannels;
 }
 if( tempImage != NULL)
 {
  pDC->SetStretchBltMode(COLORONCOLOR);
  ::StretchDIBits(pDC->GetSafeHdc(), rect.left, rect.top, rect.right, rect.bottom, 
   0, 0, tempImage->width, tempImage->height, tempImage->imageData, &bitmapInfo, 
   DIB_RGB_COLORS, SRCCOPY);

  cvReleaseImage(&tempImage);
 }
}


void DrawFunction()
{
     //code is omitted..
     //~~~~
     //~~~~~

     CDC* vDC;
     vDC = m_PicBox.GetDC(); //m_PicBox is PictureBox CStatic variable.
     CRect rect;
     m_PicBox.GetClientRect(&rect);
     DisplayIplImageToPictureBox(img, vDC, rect); //img is IplImage* variable.
     ReleaseDC(vDC);
     //~~~
     //~~~

}



..
Example picture of display(drawing) IplImage * on a dialog


1 comment:

  1. This is a great inspiring article.I am pretty much pleased with your good work.You put really very helpful information. Keep it up. Keep blogging. Looking to reading your next post.
    http://www.imagefidelity.co.uk

    ReplyDelete