将openCV中的IplImage格式的图片显示到Picture控件上
我在网上查到了两种方法,第一种方法成功了,但是第二种方法没有成功,期待有人能把它改试成功。
方法一:
代码
1 IplImage *img = cvLoadImage("****");
2 BITMAPINFO bmi;
3 FillBitmapInfo(&bmi, img->width, img->height, img->depth*img->nChannels);
4 ShowImage(img, wnd, bmi); // 这里的wnd是目标窗口,必须是CWnd类型的。
5
6 void FillBitmapInfo( BITMAPINFO *bmi, int width, int height, int bpp )
7 {
8 ASSERT( bmi && width > 0 && height > 0 &&
9 (bpp == 8 || bpp == 24 || bpp == 32) );BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);memset( bmih, 0, sizeof(*bmih));
10 bmih->biSize = sizeof(BITMAPINFOHEADER);
11 bmih->biWidth = width;
12 bmih->biHeight = -abs(height);
13 bmih->biPlanes = 1;
14 bmih->biBitCount = bpp;
15 bmih->biCompression = BI_RGB;if( bpp == 8 )
16 {
17 RGBQUAD* palette = bmi->bmiColors;
18 int i;
19 for( i = 0; i < 256; i++ )
20 {
21 palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
22 palette[i].rgbReserved = 0;
23 }
24 }
25 }
26
27 void CQRDlg::ShowImage(IplImage *pImg, CWnd *wnd, BITMAPINFO &bmi)
28 {
29 CDC *pDC = wnd->GetDC();
30 HDC hDC = pDC->GetSafeHdc();
31 CRect rect;
32 wnd->GetClientRect(&rect);
33 if(bmi.bmiHeader.biBitCount== 8)
34 {
35 CPalette pal;
36 HPALETTE hpal=NULL;
37 HPALETTE hOldPal=NULL;
38 ::SetPaletteEntries(hpal,0,256,(LPPALETTEENTRY)bmi.bmiColors);
39 hOldPal = ::SelectPalette(pDC->GetSafeHdc(), hpal, TRUE);
40 }
41 ::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
42 ::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,rect.Width(),rect.Height(),0,0,
43 pImg->width,pImg->height,pImg->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
44
45 }
2 BITMAPINFO bmi;
3 FillBitmapInfo(&bmi, img->width, img->height, img->depth*img->nChannels);
4 ShowImage(img, wnd, bmi); // 这里的wnd是目标窗口,必须是CWnd类型的。
5
6 void FillBitmapInfo( BITMAPINFO *bmi, int width, int height, int bpp )
7 {
8 ASSERT( bmi && width > 0 && height > 0 &&
9 (bpp == 8 || bpp == 24 || bpp == 32) );BITMAPINFOHEADER* bmih = &(bmi->bmiHeader);memset( bmih, 0, sizeof(*bmih));
10 bmih->biSize = sizeof(BITMAPINFOHEADER);
11 bmih->biWidth = width;
12 bmih->biHeight = -abs(height);
13 bmih->biPlanes = 1;
14 bmih->biBitCount = bpp;
15 bmih->biCompression = BI_RGB;if( bpp == 8 )
16 {
17 RGBQUAD* palette = bmi->bmiColors;
18 int i;
19 for( i = 0; i < 256; i++ )
20 {
21 palette[i].rgbBlue = palette[i].rgbGreen = palette[i].rgbRed = (BYTE)i;
22 palette[i].rgbReserved = 0;
23 }
24 }
25 }
26
27 void CQRDlg::ShowImage(IplImage *pImg, CWnd *wnd, BITMAPINFO &bmi)
28 {
29 CDC *pDC = wnd->GetDC();
30 HDC hDC = pDC->GetSafeHdc();
31 CRect rect;
32 wnd->GetClientRect(&rect);
33 if(bmi.bmiHeader.biBitCount== 8)
34 {
35 CPalette pal;
36 HPALETTE hpal=NULL;
37 HPALETTE hOldPal=NULL;
38 ::SetPaletteEntries(hpal,0,256,(LPPALETTEENTRY)bmi.bmiColors);
39 hOldPal = ::SelectPalette(pDC->GetSafeHdc(), hpal, TRUE);
40 }
41 ::SetStretchBltMode(pDC->m_hDC, COLORONCOLOR);
42 ::StretchDIBits(pDC->GetSafeHdc(),rect.left,rect.top,rect.Width(),rect.Height(),0,0,
43 pImg->width,pImg->height,pImg->imageData,&bmi,DIB_RGB_COLORS,SRCCOPY);
44
45 }
方法二:
代码
1 // 用于在控件 ID上显示图片 img
2 void CTestDlg::DrawPicToHDC(IplImage *img, UINT ID)
3 {
4 CDC *pDC = GetDlgItem(ID)->GetDC();
5 HDC hDC= pDC->GetSafeHdc();
6
7 CRect rect;
8 GetDlgItem(ID)->GetClientRect(&rect);
9 // 求出图片空间的位置
10 int rw = rect.left;
11 int rh = rect.top;
12 // 求出图片的宽和高
13 int iw = img->width;
14 int ih = img->height;
15 SetRect(rect,rw,rh,rw+iw,rh+ih);
16
17 CvvImage cimg;
18 cimg.CopyOf(img);
19 cimg.DrawToHDC(hDC,&rect);
20
21 ReleaseDC(pDC);
22 }
23
2 void CTestDlg::DrawPicToHDC(IplImage *img, UINT ID)
3 {
4 CDC *pDC = GetDlgItem(ID)->GetDC();
5 HDC hDC= pDC->GetSafeHdc();
6
7 CRect rect;
8 GetDlgItem(ID)->GetClientRect(&rect);
9 // 求出图片空间的位置
10 int rw = rect.left;
11 int rh = rect.top;
12 // 求出图片的宽和高
13 int iw = img->width;
14 int ih = img->height;
15 SetRect(rect,rw,rh,rw+iw,rh+ih);
16
17 CvvImage cimg;
18 cimg.CopyOf(img);
19 cimg.DrawToHDC(hDC,&rect);
20
21 ReleaseDC(pDC);
22 }
23