Display the mat image matrix showmatimgtownd (getdlgitem (idc_showimg) and matframe) of opencv in MFC );

Source: Internet
Author: User

Learn first, then transform, standard coders, haha

 

Original article address:

Http://www.cnblogs.com/yuanzfy/archive/2012/06/21/2557957.html

 

Using GDI +, You can conveniently display the image matrix data of opencv In the MFC window.

 

Void bitmattownd (cwnd * WND, CV: mat IMG, crect * ROI) {If (IMG. empty () return; CDC * CDC = WND-> getdc (); CDC memdc; // first define a display device object cbitmap membitmap; // define a bitmap object crect rect, drect; WND-> getclientrect (rect); gdiplus: Bitmap bitmap (IMG. cols, IMG. rows, IMG. cols * IMG. channels (), pixelformat24bpprgb, (byte *) IMG. data); // create a bitmap Bitmap bitmap in GDI + based on the mat matrix if (ROI = NULL) drect = rect; else drect = * roi; // create a memory display device compatible with the screen display. createcompatibledc (CDC); // The following creates a bitmap compatible with the screen display. For the bitmap size, use the window size membitmap. createcompatiblebitmap (CDC, rect. width (), rect. height (); // select the bitmap to the memory display device. // only the memory display device with the bitmap selected has a local drawing, cbitmap * poldbit = memdc. selectObject (& membitmap); // use the background color to clear the bitmap. You can use your own memdc color. fillsolidrect (0, 0, rect. width (), rect. height (), RGB (255,255,255); // plot gdiplus: Graphics g (memdc. m_hdc); gdiplus: Image * II = & bitmap; G. drawimage (II, gdiplus: rect (0, 0, drect. width (), drect. height (); G. releasehdc (memdc. m_hdc); // copy the image in the memory to the screen to display CDC-> bitblt (, drect. width (), drect. height (), & memdc, 0, 0, srccopy); // clear membitmap after drawing. deleteobject (); memdc. deletedc (); // releasedc (CDC); Modify WND-> releasedc (CDC) as follows );
    }

 

Another:

Http://www.cnblogs.com/yuanzfy/archive/2012/06/21/2557908.html

MFC double buffer drawing and GDI + drawing method MFC double buffer drawing:
CDC * CDC = This-> getdc (); CDC memdc; // first define a display device object cbitmap membitmap; // define a bitmap object crect rect; this-> getclientrect (rect); memdc. createcompatibledc (null); // a memory display device compatible with screen display is created later. // a screen display-compatible bitmap is created below. As for the bitmap size, you can use the window size membitmap. createcompatiblebitmap (CDC, gdirect. width (), gdirect. height (); // select the bitmap to the memory display device. // only the memory display device with the bitmap selected has a local drawing, cbitmap * poldbit = memdc. selectObject (& membitmap); // first clear the bitmap with the background color. fillsolidrect (0, 0, gdirect. width (), gdirect. height (), RGB (255,255,255); // Add your own drawing code here. At this time, the drawing is completed in the memory, the operation object is memdc // then copy the image in the memory to the screen to display CDC-> bitblt (0, 0, rect. width ()-20, rect. height (), & memdc, 0, scrollpos * 140, srccopy); // clear the temporary object membitmap after drawing. deleteobject (); memdc. deletedc (); releasedc (CDC );

 

GDI + Plotting Method

By combining the double buffering method with the GDI + drawing method, you can conveniently and efficiently display images in the control.

 

# Include <gdiplus. h> // the header file gdiplus: gdiplusstartupinput enabled; ulong_ptr m_gdiplustoken; gdiplus: gdiplusstartup (& m_gdiplustoken, & m_gdiplusstartupinput, null ); // call this function to enable the GDI + function gdiplus: Graphics g (memdc. m_hdc); // drawing // use GDI + to load other types of resource files. jpg PNG supports gdiplus: Image * II; II = gdiplus: Image :: fromfile ("C: \ img.jpg"); G. drawimage (II, gdiplus: rect (); delete II;} G. releasehdc (memdc. m_hdc); gdiplus: gdiplusshutdown (m_gdiplustoken); // call this function after use to close the GDI + Library

 

Because the Response bar cannot be pasted with too long code, I have to edit it again ....

The 1-or 3-Channel 8-bits images read in mat mode in opencv can now be conveniently displayed in the indicator window. For example, the image is displayed in the picture control with the ID of idc_showimg, you only need to showmatimgtownd (getdlgitem (idc_showimg), matframe); it is convenient and comfortable, and the display stability is better than the drawtohdc () method in the cvvimage class !!!

 

Void showmatimgtownd (cwnd * pwnd, CV: mat IMG) {If (IMG. empty () return; crect drect; pwnd-> getclientrect (drect); // (drect); (& drect); both methods can be used, even cclientdc (pwnd ); HDC = dc. getsafehdc (); // copy the image data in the memory to the screen. byte * bitbuffer = NULL; bitmapinfo * bitmapinfo = NULL; int ichannels = IMG. channels (); If (ichannels = 1) {bitbuffer = new byte [40 + 4*256];} else if (ichannels = 3) {bitbuffer = new byte [sizeof (bitmapinfo )];} Else {return;} If (bitbuffer = NULL) {return;} bitmapinfo = (bitmapinfo *) bitbuffer; bitmapinfo-> bmiheader. bisize = sizeof (bitmapinfoheader); bitmapinfo-> bmiheader. biheight =-IMG. rows; // if the height is positive, the starting position of the bitmap is in the lower left corner. If the height is negative, the start position is in the upper left corner. Bitmapinfo-> bmiheader. biwidth = IMG. cols; bitmapinfo-> bmiheader. biplanes = 1; // the target device level, which must be 1bitmapinfo-> bmiheader. bibitcount = ichannels * 8; // The number of digits required for each pixel, which must be 1 (two-color), 4 (16 colors), 8 (256 colors), or 24 (true color) bitmapinfo-> bmiheader. bicompression = bi_rgb; // bitmap compression type, which must be 0 (not compressed), 1 (bi_rle8 compression type), or 2 (bi_rle4 compression type. bisizeimage = 0; // bitmap size, in bytes: bitmapinfo-> bmiheader. bixpelspermeter = 0; // horizontal bitmap resolution, number of bytes per meter bitmapinfo-> bmiheader. biypelspermeter = 0; // bitmap vertical resolution, number of bytes per meter bitmapinfo-> bmiheader. biclrused = 0; // number of colors in the color table used by the bitmap. bitmapinfo-> bmiheader. biclrimportant = 0; // number of important colors in the bitmap display process if (ichannels = 1) {for (INT I = 0; I <256; I ++) {// value range of color (0-255) bitmapinfo-> bmicolors [I]. rgbblue = bitmapinfo-> bmicolors [I]. rgbgreen = bitmapinfo-> bmicolors [I]. rgbred = (byte) I;} bitmapinfo-> bmiheader. biclrused = 256; // number of colors in the color table actually used by the bitmap} setstretchbltmode (HDC, coloroncolor); stretchdibits (HDC, drect. right, // display the window width drect. bottom, // display the window height (0, 0, IMG. cols, // Image Width IMG. rows, // Image Height IMG. data, bitmapinfo, dib_rgb_colors, srccopy); Delete [] bitbuffer ;}

 

 

One of the following is a brother version: it occupies a piece of memory until the end of the application is reclaimed, but it can provide a better display speed, haha...

 

Void showmatimgtownd (cwnd * pwnd, CV: mat IMG) {If (IMG. empty () return; static bitmapinfo * bitmapinfo = NULL; static bool first = true; If (first) {byte * bitbuffer = new byte [40 + 4*256]; if (bitbuffer = NULL) {return;} First = false; memset (bitbuffer, 0, 40 + 4*256); bitmapinfo = (bitmapinfo *) bitbuffer; bitmapinfo-> bmiheader. bisize = sizeof (bitmapinfoheader); bitmapinfo-> bmiheader. biplanes = 1; // The level of the target device, which must be 1for (INT I = 0; I <256; I ++) {// color value range (0-255) bitmapinfo-> bmicolors [I]. rgbblue = bitmapinfo-> bmicolors [I]. rgbgreen = bitmapinfo-> bmicolors [I]. rgbred = (byte) I ;}} bitmapinfo-> bmiheader. biheight =-IMG. rows; // if the height is positive, the starting position of the bitmap is in the lower left corner. If the height is negative, the start position is in the upper left corner. Bitmapinfo-> bmiheader. biwidth = IMG. cols; bitmapinfo-> bmiheader. bibitcount = IMG. channels () * 8; // number of digits required for each pixel, which must be 1 (two-color), 4 (16 colors), 8 (256 colors), or 24 (true color) one of the crect drect; pwnd-> getclientrect (drect); // (drect); (& drect); both methods can be used, even cclientdc DC (pwnd); HDC = dc. getsafehdc (); setstretchbltmode (HDC, coloroncolor); // do not delete this sentence. // copy the image data in the memory to stretchdibits (HDC, drect. right, // display the window width drect. bottom, // display the window height (0, 0, IMG. cols, // Image Width IMG. rows, // Image Height IMG. data, bitmapinfo, dib_rgb_colors, srccopy );}

 

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.