How to display the image loaded with opencv in the window position? -How can I convert an image in any format loaded by opencv into bitmap or Dib, which can be drawn by GDI?

Source: Internet
Author: User

Circulated on the Internet

Void showimageauto (iplimage * pimg, cwnd * WND );
Void showimage (iplimage * pimg, cwnd * WND, bitmapinfo * BMI );
Void fillbitmapinfo (bitmapinfo * BMI, int width, int height, int bpp );

You can use iplimage to draw an image on the MFC interface. However, the function has bugs and does not support single-channel, 8-bit grayscale display.
The main reason is that the color palette is used for 8 bitmap. In the bitmapinfo structure of windows, only one rgbquad space is allocated, and the color in the online code is
The rgbquad structure is assumed to be 256, so the write memory violation!
In addition, there is a CDC resource leakage problem.

I fixed the following:

# Include "stdafx. H "<br/> # include" opencvhelper. H "</P> <p> namespace opencvhelper <br/>{</P> <p> // generates the bitmap information for Windows <br/> void fillbitmapinfo (bitmapinfo * BMI, int width, int height, int bpp) <br/>{< br/> assert (BMI & width> 0 & height> 0 & <br/> (bpp = 8 | bpp = 24 | | bpp = 32 )); </P> <p> bitmapinfoheader * bmih = & (BMI-> bmiheader); </P> <p> memset (bmih, 0, sizeof (* bmih )); </P> <p> bmih-> bisize = sizeof (bitmapinfoheader); <br/> bmih-> biwidth = width; <br/> bmih-> biheight =-ABS (height); <br/> bmih-> biplanes = 1; <br/> bmih-> bibitcount = BPP; <br/> bmih-> bicompression = bi_rgb; </P> <p> If (bpp = 8) <br/>{< br/> rgbquad * palette = BMI-> bmicolors; <br/> int I; <br/> for (I = 0; I <256; I ++) <br/>{< br/> palette [I]. rgbblue = palette [I]. rgbgreen = palette [I]. rgbred = (byte) I; <br/> palette [I]. rgbreserved = 0; <br/>}</P> <p> // automatically draws an iplimage image to the cwnd class. <br/> void showimage (iplimage * pimg, cwnd * WND, bitmapinfo * BMI) <br/>{< br/> CDC * PDC = WND-> getdc (); <br/> HDC = PDC-> getsafehdc (); <br/> crect rect; <br/> WND-> getclientrect (& rect ); </P> <p> If (BMI-> bmiheader ). bibitcount = 8) <br/>{< br/> cpalette pal; <br/> hpalette hpal = NULL; <br/> hpalette holdpal = NULL; <br/>:: setpaletteentries (hpal, 0,256, (lppaletteentry) BMI-> bmicolors); <br/> holdpal =: selectpalette (PDC-> getsafehdc (), hpal, true ); <br/>}< br/>: setstretchbltmode (PDC-> m_hdc, coloroncolor); <br/>: stretchdibits (PDC-> getsafehdc (), <br/> rect. left, <br/> rect. top, <br/> // pimg-> width, pimg-> height, <br/> rect. right-rect.left, rect. bottom-rect.top, <br/> 0, <br/> pimg-> width, pimg-> height, pimg-> imagedata, BMI, dib_rgb_colors, srccopy ); </P> <p> WND-> releasedc (PDC); <br/>}</P> <p> // automatically draws an iplimage image to the cwnd class) <br/> void showimageauto (iplimage * pimg, cwnd * WND) <br/> {<br/> If (0 = pimg | 0 = WND) <br/>{< br/> return; <br/>}</P> <p> bitmapinfo * BMI; <br/> unsigned int buffer [sizeof (bitmapinfoheader) + sizeof (rgbquad) * 256]; <br/> BMI = (bitmapinfo *) buffer; </P> <p> fillbitmapinfo (BMI, pimg-> width, pimg-> height, pimg-> depth * pimg-> nchannels); </P> <p> showimage (pimg, WND, BMI ); <br/>}</P> <p>} // namespace

How to display the image loaded with opencv in the window position? -- How can I convert an image in any format loaded by opencv into bitmap or Dib, which can be drawn by GDI?

Transferred from:
Http://blog.csdn.net/daiyuchao/archive/2007/05/12/1605963.aspx

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.