Display the mat of opencv in MFC/C #

Source: Internet
Author: User

There was a class named cvvimage before opencv 2.2 that can display iplimage in MFC, but this class has disappeared since Version 2.2, and some people continue to use the code of previous versions, however, this is not very convenient. For example, you have to convert mat to iplimage in C ++. For this reason, I found some materials and finally solved the problem of displaying CV: mat in various windows forms, which makes it much easier to use. Here, we will share it with anyone who needs it. This article is original. Please enter the address for reprinting. Thank you ~

Address: http://blog.csdn.net/houston11235/article/details/7678088

The following function is used to display the mat function in the form of MFC or C #. The reason is that no matter what the programming language is, windows Forms all have something called a handle (corresponding to handle in C #). Just pass this as the second parameter of the function.

This function uses the cimage class in ATL and must reference the header file atlimage. h. Note that this should be placed in CV. h. It is best to put it in front of Shenma on Windows. otherwise, 'Min' and 'Max' undefined errors will occur during compilation.

In addition, this function only enables 3-channel image display. For 1-channel images, see the final link of this article.

Int showmat (CV: mat IMG, hwnd hwnddisplay) {If (IMG. channels () <3) {return-1;} rect; getclientrect (hwnddisplay, & rect); CV: mat imgshow (ABS (rect. top-rect. bottom), ABS (rect. right-rect. left), cv_8uc3); resize (IMG, imgshow, imgshow. size (); ATL: cimage ci; int W = imgshow. cols; // width int H = imgshow. rows; // high int channels = imgshow. channels (); // number of channels CI. create (W, h, 8 * channels); uchar * pS; uchar * pimg = (uchar *) CI. getbits (); // obtain the cimage data zone address int step = CI. getpitch (); For (INT I = 0; I 

Reference link: http://www.opencv.org.cn/forum/viewtopic.php? F = 1 & t = 15224

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.