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


Comments

Popular posts from this blog

(OpenCV Study) Background subtractor MOG, MOG2, GMG example source code (BackgroundSubtractorMOG, BackgroundSubtractorMOG2, BackgroundSubtractorGMG)

OpenCV Stitching example (Stitcher class, Panorama)

Example source code of extract HOG feature from images, save descriptor values to xml file, using opencv (using HOGDescriptor )

Real-time N camera stitching Class.

Optical Flow sample source code using OpenCV

OpenCV Drawing Example, (line, circle, rectangle, ellipse, polyline, fillConvexPoly, putText, drawContours)

Video Stabilization example source code, (using cvFindHomography, cvWarpPerspective functions in openCV)

SICK LMS511 sensor data acquisition interface (source code, C++/MFC)

8 point algorithm (Matlab source code) / The method to get the Fundamental Matrix and the Essential matrix

Image warping (using opencv findHomography, warpPerspective)