A step-by-step guide to the use of the Intel OpenCV 1 library

Creating a Dialog-based Application

All applications presented here will be simple dialog-based applications. This kind of applications can easily be created with Visual C++ and constitutes a good way to obtain a pleasant interactive application. On you Visual C++ menu bar, select the File|New|Project... option, choose MFC Application and select a name for your application (here cvision).

With Visual C++, you can construct a solution made of several projects (each project is basically one program). So if you build a multi-program application, for example a client and a server application, solutions are very useful because you can group your projects together and have those sharing files and libraries. Usually you create one master directory for your solution that contains all the directories of your projects. In our case, we will incrementally build one project, so I choose to uncheck the Create directory for solution option which means that the solution and the unique project will be put into one single directory. All along this tutorial, you will have access to the different versions of this project. When you become more familiar with VC++, you should take advantage of creating multi-project solutions.

Once you click OK, you will then be brought to the MFC Application Wizard that will let you select different options for your GUI. At this point simply select the Dialog-based option. Other options ore available and I invite you to explore them but for now the standard settings are mostly okay. Note however that the Unicode option that is turned on by default in VC++ 2005 should be unchecked. If you let that option checked, your application will use the larger 16-bit Unicode character set (and give you access to the many international caracters); this could be useful but as many functions requires char * (ASCII strings), you will have to convert your string from Unicode to ASCII which can be painful. Therefore, it is simpler to turn off that option for now. Remember that if you get this kind of errors:

      cannot convert parameter 1 from 'CString' to 'const char *'
      cannot convert from 'const char [11]' to 'LPCWSTR'
this means that you have conversion problems between unicode and multi-byte strings.

VC++ should create a simple OK/Cancel Dialog for you. The class with a name ending by Dlg (here cvisionDlg) will contain the member functions that control the widget of the dialog. Never touch the other files.

The first task will be to open and display an image. To do this, we will first add a button that will allow us to select the file that contains the image. Go under the Resource and drag a button onto the dialog. You can also resize the dialog and take the time to look at all the widget available in the toolbox. Change the caption of the button (see the Properties panel) to Open Image. Right click on the button and select Add Event Handler...; this will allow you to specify the name of the handler method that will be called when the user will click on this button.

Now if you compile and start the application, the dialog should now looks like this:

You probably compiled and ran the application under the Debug mode; this is why a Debug directory has been created inside your project directory. The Debug mode is there to help you to create and debug your application. It is a more protected environment but that generate slower executable files. Once your application will be ready, do not forget to compile it under the Release mode which will produce the real executable that will be used by your users. When you will do so, a Release directory will appear inside your project.

Let us now use the CFileDialog class in order to create a file dialog. This one will show up by adding the following code to the OnOpen member function

void CcvisionDlg::OnOpen()
  CFileDialog dlg(TRUE, _T("*.bmp"), NULL,
    _T("image files (*.bmp; *.jpg) |*.bmp;*.jpg
                                   |All Files (*.*)|*.*||"),NULL);
  dlg.m_ofn.lpstrTitle= _T("Open Image");
  if (dlg.DoModal() == IDOK) {
    CString path= dlg.GetPathName(); // the selected filename
Note how the extensions of interest (here .bmp and .jpg) for the files to be opened are specified using the fourth argument of the CFileDialog constructor. Just for your information, in C++, when you write "Open Image" you generate a regular narrow ASCII string, therefore if you want instead a wide Unicode string to be generated you have to use L"Open Image". There is however a better solution: if you use _T"Open Image" then you inform the compiler that you want your string to be formatted using the current character set which is a much more flexible solution. In our case, we use the ASCII byte format so the _T conversions are not required.

Now, by clicking on the Open Image button, the following dialog appears:

Top of the page


(c) Robert Laganiere 2011