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

Loading and displaying an image

Now that we learnt how to select a file, lets load and display the corresponding image. The Intel libraries will help us to accomplish this task. In particular, the HighGui component of OpenCV will be put to contribution. This one contains the required functions to load, save and display images under the Windows environment.

Since we will be using these libraries in all the examples to follow, we will first see how to setup adequately our VC++ projects in order to have the libraries linked to our application. One option would be to go under Project|Properties... but then you would have to repeat this sequence for all your projects. It is therefore a good idea to setup your environement such that it will always remember where to find the OpenCV files to include and to link to your projects. You then go under Tool|Option... Toolption. Select the VC++ Directories tab and the category Include Files. Add the following directories to additional include directories:

  • C:\Program Files\OpenCV\cv\include
  • C:\Program Files\OpenCV\cxcore\include
  • C:\Program Files\OpenCV\otherlibs\highgui
  • C:\Program Files\OpenCV\filters\ProxyTrans (you will need this ProxyTrans when you will process video sequences)

Select now the Library Files Tab and add this library path:

  • C:\Program Files\OpenCV\lib

With these global settings, only the names of the library modules need to be specified when a new project is created. You go under project Project|Properties... and you enter the names of the three main components of the OpenCV library:

  • cxcore.lib that contains mainly the basic data structure;
  • cv.lib containing the computer vision functions;
  • highgui.lib that is a basic tool for displaying and saving images.

Now if we want to create an application that will use the OpenCV classes and functions, you must first include the header file highgui.h to your xxxDlg.h file. Note that the file highgui.h already includes cxcore.h. Now here is a modified version of the OnOpen handler:

void CcvisionDlg::OnOpen()
{
  CFileDialog dlg(TRUE, _T("*.bmp"), NULL,
    OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST|OFN_HIDEREADONLY,
    _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();  // contain the selected filename
 
      IplImage *image;                      // This is image pointer
 
      image= cvLoadImage(path);             // load the image
      cvShowImage("Original Image", image); // display it
 
  }
}
The function names starting with cv are OpenCV functions. IplImage is the data structure that contains the image under OpenCV. With this modification, your program should load and display an image. Note that the cvNamedWindow function that creates the window should be called only once which means that you can put it in the OnInitDialog method of your xxxDlg.cpp file.
BOOL CcvisionDlg::OnInitDialog()
{
      CDialog::OnInitDialog();
 
      .
      .
      .
 
      // TODO: Add extra initialization here
      cvNamedWindow( "Original Image"); // create the window on which
                                       // the image will be displayed
 
      return TRUE;  // return TRUE  
                    // unless you set the focus to a control
}
Also, for your program to terminate cleanly, you must add a call to cvDestroyAllWindows() which will close all the highgui windows that you could have created. This call should be associated with the OK and Cancel button handlers (you create these by double-clicking on the widget in the resource view).
void CcvisionDlg::OnBnClickedOk()
{
      // TODO: Add your control notification handler code here
      cvDestroyAllWindows();
      OnOK();
}
 
void CcvisionDlg::OnBnClickedCancel()
{
      // TODO: Add your control notification handler code here
      cvDestroyAllWindows();
      OnCancel();
}
When running this application and selecting an image, you should obtain:

Check point #1: source code of the above example.

Top of the page

 

(c) Robert Laganiere 2011