Reprint Link: Http://wiki.opencv.org.cn/index.php/MFC%E4%B8%AD%E5%BF%AB%E9%80%9F%E5%BA%94%E7%94%A8OpenCV
Introduction and Origins
The original discussion topic of this tutorial, please see "Original" MFC in the rapid application of OPENCV tutorial, the purpose of this tutorial is to facilitate the majority of users under the Windows MFC user can easily in the MFC window view class, the implementation of OPENCV format picture display.
Introduction to Basic OpenCV and MFC operationsHow to apply menus, and examples of canny operators in MFCCreate an SDI project
I'm using Visual C + + 6.0, so this tutorial describes what to do on VC6, and if you use a different version of VC, I think it should be the same. First, follow the VC Wizard, produce a default MFC project, note, select the SDI single document support, and preferably choose "Use MFC as a static library" (To prevent MFC using OPENCV memory leak problem) specifically, see the following two pictures shown
After all the way down, the basic MFC framework is created.
Join OPENCV Library Support
Now join the OPENCV required header file and library file. As shown, we add the library file: cxcore.lib cv.lib highgui.lib
adding variables to the Doc class
Find ***doc is also the definition of the document class , at the beginning plus HighGui.h header file contains, add a variable CImage m_image;.
Note that if you are not using VC6, follow my tutorial, may compile cannot pass (my tutorial, the m_image is set to protected type), the workaround is to set the type of m_image to public, It is recommended that you use the public modifier later. For specific bug reports and analysis, see here: The public modifier is recommended
Add a virtual function
Add two functions, respectively, to open the picture file and save the picture file, add the process to see the right-click menu.
Add functions OnOpenDocument and OnSaveDocument, respectively, as shown in the following code
BOOL csdi opencvdoc::onopendocument (lpctstr lpszpathname) { if (! Cdocument::onopendocument (lpszPathName)) return FALSE; Todo:add your specialized creation code here m_image. Load (lpszpathname); return TRUE;}
BOOL csdi opencvdoc::onsavedocument (lpctstr lpszpathname) { //Todo:add your specialized code here and/or call the BAS E class M_image. Save (lpszpathname); return TRUE; Return cdocument::onsavedocument (lpszPathName);}
Note that the above would have been return cdocument::onsavedocument (lpszPathName); But later found that there is a problem, save the picture, will cause the size of the saved image to become 0, instead of return TRUE; there is no problem, please see the forum discussion: http://www.opencv.org.cn/forum/viewtopic.php?f= 1&t=4932&p=18215#p18215
Add display image code to the view class
Add the necessary picture display code in the middle of the view class, mainly inside the OnDraw.
void Csdi_opencvview::ondraw (cdc* pDC) {csdi_opencvdoc* PDoc = GetDocument (); Assert_valid (PDOC);//Todo:add Draw code for native data Herecimage & img = PDoc->m_image; CRect R; GetClientRect (&R); img. DRAWTOHDC (PDC->GETSAFEHDC (), r);}
By compiling the entire program, you can open the image from the Open menu and display it in the view class of MFC.
2010-12-28 Important Updates
Because OpenCV2.2 inside, the original Cvvimage the entire class to remove, so in the use of MFC to bring a lot of inconvenience, we can opencv2.1 in advance by the code of the method to solve (get an H file and a CPP file, and then put in your project to compile together). Specific methods and file content, see: re:opencv2.2 cimage (cvvimage) cancellation, MFC display pictures less a good way
thanks and PostScript as well as related discussions
- The document was written by User:ollydbg23, in addition, I organized the essence of this forum, also in the form of wikis, placed here in the Forum essence area.
- If in doubt, you can go to the original topic and post a reply to participate in the discussion of "original" MFC Rapid Application OPENCV Tutorial
- For a solution that is too big to fit into the view, see Feixiaolin's post OPENCV data read and write operation + image Noise + MFC OpenCV Source code and how to insert scroll bar
- A brief explanation of OpenCV drawing function Drawtohdc
- In the opencv1.0 version, using the canny method described above, there is a single channel picture can not be opened error, for specific reasons please see here, this is mainly due to a bug in opencv1.0. Reason
- In the OPENCV 1.1 version of the use, may cause MFC program can not quit, the solution please see here program does not exit normally
- Note that the CImage is actually cvvimage these two can be replaced directly using.
- Questions about OLE error dialog box, see How to solve the problem of OLE forum discussion
Network Other OPENCV resources
- Example Mango Editor using OpenCV in the Wxwidget GUI interface
- Cvpreprocessor that can automatically generate OPENCV code
Rapid application of OPENCV (RPM) in MFC