IplImage and CBitmap Conversion

Source: Internet
Author: User

If you use opencv when writing an mfc program, the conversion between the two is certainly used. I have found a lot on the internet, almost all of which are the same. My situation is a little special, and it is not appropriate to find it for a long time. MD, in one breath, tracked CvvImage: DrawToHDC,

Void IplImage2CBitmap (Iplimage * ipg_src)

{

CBitmap * m_pBitmap = new CBitmap;

If (ipg_src-> depth = IPL_DEPTH_8U)

{

Uchar buffer [sizeof (BITMAPINFOHEADER) + 1024];

BITMAPINFO * bmi = (BITMAPINFO *) buffer;

Int BMP _w = ipg_src-> width, BMP _h = ipg_src-> height;

// FillBitmapInfo (bmi, BMP _w, BMP _h, Bpp (), pImage-> origin );

Int width = BMP _w;

Int height = BMP _h;

Int bpp = ipg_src? (Ipg_src-> depth & 255) * ipg_src-> nChannels: 0;

Int origin = ipg_src-> origin;

HBITMAP hBitmap;

BITMAPINFOHEADER * bmih = & (bmi-> bmiHeader );

Memset (bmih, 0, sizeof (* bmih ));

Bmih-> biSize = sizeof (BITMAPINFOHEADER );

Bmih-> biWidth = width;

Bmih-> biHeight = origin? Abs (height):-abs (height );

Bmih-> biPlanes = 1;

Bmih-> biBitCount = (unsigned short) bpp;

Bmih-> biCompression = BI_RGB;

If (bpp = 8)

{

RGBQUAD * palette = bmi-> bmiColors;

Int I;

For (I = 0; I <256; I ++)

{

Palette [I]. rgbBlue = palette [I]. rgbGreen = palette [I]. rgbRed = (BYTE) I;

Palette [I]. rgbReserved = 0;

}

}

}

The above code is modified from opencv. At this point, you can

HBITMAP hbmp = CreateDIBitmap (pdc-> GetSafeHdc (), bmih, CBM_INIT, ipg_src-> imageData, bmi, DIB_RGB_COLORS );

The first parameter must be the handle of the device to display the image. null indicates memory.

Finally: m_pBitmap-> Attach (hBitmap); if you add

If (m_pBitmap-> m_hObject! = NULL)

{

M_pBitmap-> Detach ();

} Better.

Opencv uses: StretchDIBits (device handle ,......), If you use pdc-> StretchBlt (), it cannot be displayed. Why? Due to limited level!

The Internet is just a reference and you must have your own thoughts. Write some transformations you understand. Although they are also copied, they add their own things.

CBitmap m_bitmap;

HBITMAP h_bmp;

H_bmp = m_bitmap.m_hObject; CBitmap => HBITMAP

M_bitmap = CBitmap: FromHandle (h_bmp); HBITMAP => CBitmap

I tried to convert IplImage directly to CBitmap. This is the code on the Internet, but it was not displayed successfully, and I do not know whether the conversion was successful. Below is the code

IplImage-> CBitmap, Cbitmap-> IplImage

CDC * pdc = this-> GetDC ();

IplImage * img = cvCreateImage (cvSize (100,100), 8, 3 );

CBitmap m_bitmap;

M_bitmap.CreateCompatibleBitmap (pdc, 100,100 );

CDC memdc;

Memdc. CreateCompatibleDC (pdc );

CBitmap * pold = memdc. SelectObject (& m_bitmap );

CvvImage cvimg;

Rect rect (0, 0,100,100 );

Cvimg. CopyOf (img,-1 );

Cvimg. DrawToHDC (memdc. m_hDC, & rect );

ReleaseDC (pdc );

IplImage * tempimg = cvCloneImage (img );

M_bitmap.GetBitmapBits (img-> widthStep * img-> height, img-> imageData );

I tried to display m_bitmap to verify whether the conversion was successful.

Memdc. StretchBlt (0, 0, rect. Width (), rect. Height (), & memdc, 0, 0, w, h, SRCCOPY );

But it didn't work! Not yet clear!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.