Click here to Skip to main content
15,867,308 members
Articles / Desktop Programming / MFC

OpenCV in MFC

Rate me:
Please Sign up or sign in to vote.
4.75/5 (10 votes)
11 Sep 2018CPOL2 min read 30K   2K   21   6
A way to use OpenCV in MFC project

Image 1

Project (OneDrive): https://onedrive.live.com/embed?cid=DEDCB6EF190B8FD4&resid=DEDCB6EF190B8FD4%21411&authkey=ANtICXVGKPsQ030

Introduction

Several times, I have read questions regarding how to use OpenCV in MFC projects, so I decided to made a simple application demo, a VS2010 project, that reveals a way to use computer vision library inside of VC/MFC project, developed in an MDI application. Through my findings, as far as I remember, I saw somewhere a CDialog based app that embeds OpenCV ... here is another approach: multi-document interface, where you can load multiple media files.

Background

This article does not treat the OpenCV compilation, it does contain the compiled OpenCV library, ready to go, inside of two folders: include, and res86, all of them are included inside the project. On the internet, you can easily find the way in which to compile OpenCV, this is not the purpose of this article.

Step to Setup the Project

1. Put "input" and "x86" folders inside your project:

Image 2

2. Setup project settings as follows:

Additional include directories: C:\Flaviu\Tempo\include;%(AdditionalIncludeDirectories) (replace "C:\Flaviu\" with your path).

Image 3

Additional library directories: C:\Flaviu\Tempo\x86\vc10\lib;%(AdditionalLibraryDirectories) (replace "C:\Flaviu\" with your path).

Image 4

Additional dependencies:

opencv_calib3d341d.lib
opencv_core341d.lib
opencv_features2d341d.lib
opencv_flann341d.lib
opencv_highgui341d.lib
opencv_imgcodecs341d.lib
opencv_imgproc341d.lib
opencv_ml341d.lib
opencv_objdetect341d.lib
opencv_photo341d.lib
opencv_shape341d.lib
opencv_stitching341d.lib
opencv_superres341d.lib
opencv_video341d.lib
opencv_videoio341d.lib
opencv_videostab341d.lib

in Debug case, and the same libs for Release but without "d" from file names. Example: opencv_calib3d341.lib as opencv_calib3d341d.lib.

Image 5

Using the Code

The structure of the project is pretty simple: the document class contains a cv::VideoCapture object, used to read video files, a cv::Mat object that is needed to convert cv::VideoCapture into cv::Mat object, and a BITMAPINFO structure used to convert cv::Mat object into bitmap object in order to be used for CView rendering. This conversion could be found on CTempoDoc::SetupBitmapInfo method.

C++
void CTempoDoc::SetupBitmapInfo(cv::Mat& mat)
{
    if(NULL != m_pBmi)
    {
        delete m_pBmi;
        m_pBmi = NULL;
    }
    m_pBmi = new BITMAPINFO;
    BITMAPINFOHEADER* pHeader = &m_pBmi->bmiHeader;
    pHeader->biSize             = sizeof(BITMAPINFOHEADER);
    pHeader->biPlanes           = 1;
    pHeader->biCompression      = BI_RGB;
    pHeader->biXPelsPerMeter    = 100;
    pHeader->biYPelsPerMeter    = 100;
    pHeader->biClrUsed          = 0;
    pHeader->biClrImportant     = 0;
    pHeader->biWidth            = m_Mat.cols;
    pHeader->biHeight           = -m_Mat.rows;
    pHeader->biBitCount         = 24;
    m_pBmi->bmiHeader.biSizeImage = 0;
}

I should say that CTempoDoc has a tricky method: ResizeMat(cv::Mat& mat) ... this method corrects the cv::Mat that does not have the right format (actually, add a new column to matrix OpenCV object retrieved from cv:imread, when it is needed).

The rendering is handled inside CView class. Here, you can spot the well known OnDraw method, which renders the content of CTempoDoc::BITMAPINFO content, double-buffered. And a timer is implemented for the case when the loaded file is a video stream, and this timer will know when to load and render the next frame from video. Also, this class contains another handler that takes care of how the content is erased and drawn on CView, when the size of the window has been changed or when the next frame of video is ready to render, and some methods that scale the images inside window. All of them can be explored inside the sample project.

Here, you have an application that can load static images or all kinds of videos into an MDI app. Enjoy it!

Points of Interest

The computer vision library is used more and more, and I offer a way here to include your OpenCV algorithms inside a real life desktop application.

History

  • 11th September, 2018 - Published the article

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Romania Romania
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionI can't download from onedriver Pin
yousse9-Oct-19 18:26
yousse9-Oct-19 18:26 
AnswerRe: I can't download from onedriver Pin
_Flaviu1-Jan-20 8:34
_Flaviu1-Jan-20 8:34 
QuestionCannot implement grayed image with openCV Pin
ptpham15-May-19 22:30
ptpham15-May-19 22:30 
Questioncompile failed Pin
wallollaw12-Sep-18 14:46
wallollaw12-Sep-18 14:46 
AnswerRe: compile failed Pin
_Flaviu12-Sep-18 21:21
_Flaviu12-Sep-18 21:21 
Questionhave you consider to post this as a tip? Pin
Nelek11-Sep-18 9:11
protectorNelek11-Sep-18 9:11 
I think it would fit better that cathegory. For more information about the types please read:
Code Project Article FAQ - Article[^]
Code Project Article FAQ - Tip[^]

If you need information about how to change your submission to a tip, please read:
Code Project Article FAQ - change to tip[^]
M.D.V. Wink | ;)

If something has a solution... Why do we have to worry about?. If it has no solution... For what reason do we have to worry about?
Help me to understand what I'm saying, and I'll explain it better to you
Rating helpful answers is nice, but saying thanks can be even nicer.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.