Using openCV in MFC

add virtual function

opencv in MFCPopular tutorials of using openCV to detect objects and showing recognised results are based on a black window with rapid command lines flushing up. As MFC has more powerful functions in interface design than pure interfaces provided by openCV, it is attractive to move openCV application from console to MFC. Here is an tutorial of introducing using openCV in MFC, check it out.

Step 1: Visual C++ wizard, choose MFC App.

opencv in MFC

Step 2: Visual C++, choose single document, to create main MFC app frame.

choose single document

Step 3: Add openCV libs in project properties.

add opencv libs

Step 4: Add variables in Doc class: add a variable m_image, include highgui.h head file in the first beginning, and the variable CImage   m_image.

add variables

Step 5: Add two virtual functions to open and save image file, as shown below.

add virtual function

BOOL CSD|_OpenCVDoc::OnOpenDocument(LPCTSTR LpszPathName){
if(!CDocument::OnOpenDocument(LpszPathName)) return FALSE;
//TODO: Add your specifilised creation code here
return TRUE;

BOOL CSD|_OpenCVDoc::OnSaveDocument(LPCTSTR   LpszPathName){
//TODO: Add your specialised code here and /or call the base class
return   CDocument::OnSaveDocument(LpszPathName);

Step 6: Add codes in View class to show the image

void CSDI_OpenCVView::OnDraw(CDC*pDC){
CSDI_OpenCVDoc* pDoc = GetDocument();
CImage & img = pDoc->m_image;
CRect r;

Step 7: Compile the app, then an image can be open through open menu, here comes the beauty Lena.


Partly this post refers a document of ‘using openCV in MFC‘ by ollydbg23, any offend of copyrights please make me informed.

Author: Andol Li

A HCI researcher, a digital media lecturer, an information product designer, and a python/php/java coder.

9 Comments On “ Using openCV in MFC”

  1. Pretty nice post. I just stumbled upon your blog and wanted to say that I have really enjoyed browsing your blog posts. In any case I’ll be subscribing to your feed and I hope you write again soon!

  2. I am new one in this area using MFC and c++ .
    I have to project on “tracing the motion detection.”
    but i do’t understand how do i do it..
    I tried to make a simple MFC project by working with video from camera.. you can download my project from this link:
    but i do’t understand that how do i extract frames from video and do difference for moving objects./ .

    Or is there any tutorail for MFC c++ work with c++ ./.
    can any one help me please ,
    Can any one Please update my project that should be base for my project. like load video from cam ,extract frames and difference between frames for moving objects. can any one help me please ,. Or provide me any MFC c++ book with open cv . or any project related to it. Please help me thanks

  3. I read a lot of interesting articles here. Probably you spend
    a lot of time writing, i know how to save you a lot of time, there is an online
    tool that creates high quality, SEO friendly articles in seconds, just search in google – laranitas free content source

Leave a comment
Due to technical adjustments, the comment function is shortly closed and will be re-openning soon. Thanks.

Copyrights 2006-2018 © All rights reserved
Theme Tree2, re-designed by Andol Li, powered by WordPress and Bootsrap
WWW.ANDOL.ME | 浙ICP备15040508号-1
公安备案图标 浙公网安备33010602004018号
Back to top