Some time you want to show the picture and the result picture that process by OpenCV (IplImage*) on MFC form, so you must convert IplImage* to HBITMAP. I think have many way to do that, but this is a simplest way, you can try it:
HBITMAP ImageShower::ConvertIplImage2HBITMAP(IplImage* pImage)
{
IplImage* image = (IplImage*)pImage;
bool imgConverted = false;
/*if(pImage->nChannels!=3)
{
IplImage* imageCh3 = cvCreateImage(cvGetSize(pImage),
8, 3);
if(pImage->nChannels==1)
cvCvtColor(pImage, imageCh3, CV_GRAY2RGB);
image = imageCh3;
imgConverted = true;
}*/
int bpp = image->nChannels * 8;
assert(image->width >= 0 && image->height >= 0 &&
(bpp == 8 || bpp == 24 || bpp == 32));
CvMat dst;
void* dst_ptr = 0;
HBITMAP hbmp = NULL;
unsigned char buffer[sizeof(BITMAPINFO) + 255*sizeof(RGBQUAD)];
BITMAPINFO* bmi = (BITMAPINFO*)buffer;
BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);
ZeroMemory(bmih, sizeof(BITMAPINFOHEADER));
bmih->biSize = sizeof(BITMAPINFOHEADER);
bmih->biWidth = image->width;
bmih->biHeight = image->origin ? abs(image->height) :
-abs(image->height);
bmih->biPlanes = 1;
bmih->biBitCount = bpp;
bmih->biCompression = BI_RGB;
if (bpp == 8) {
RGBQUAD* palette = bmi->bmiColors;
int i;
for (i = 0; i < 256; i++) {
palette[i].rgbRed = palette[i].rgbGreen = palette
[i].rgbBlue
= (BYTE)i;
palette[i].rgbReserved = 0;
}
}
hbmp = CreateDIBSection(NULL, bmi, DIB_RGB_COLORS, &dst_ptr, 0,
0);
cvInitMatHeader(&dst, image->height, image->width, CV_8UC3,
dst_ptr, (image->width * image->nChannels + 3) & -4);
cvConvertImage(image, &dst, image->origin ? CV_CVTIMG_FLIP : 0);
if(imgConverted)
cvReleaseImage(&image);
return hbmp;
}