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

Processing an image

Now lets try to call one of the OpenCV function. First, as mentioned above, the computer vision functions are found in the cv.lib component. The header file cv.h must therefore be included. And we rewrite the handler function as follows:

 
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 the image pointer
 
      image= cvLoadImage(path);              // load the image
      cvErode(image,image,0,3);              // process it
      cvShowImage("Processed Image", image); // display it
  }
}
In this example, the processing consists in the application of a simple morphological, the erosion (cvErode). And the result is:

This example is particularly simple because, in the case of the operator used, the processing is done in-place (the same image is used for input and output). In general, you need to have a distinct output image. Therefore, when you process an image, you will typically i) open an image; ii) create an output image of the same size and; iii) process the image and write the result into the output image. In addition, you must release the memory you have dynamically allocated when creating the output image. In our example, this procedure is realized as follows:
  
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 the image pointer (input)
      IplImage *output;   // This is the image pointer (output)
 
      image= cvLoadImage(path);              // load the image
      cvShowImage("Original Image", image);  // display it
 
      // output image memory allocation
      output= cvCreateImage(cvSize(image->width,image->height),
                        image->depth, image->nChannels);
 
      cvErode(image,output,0,3);                    // process it
      cvShowImage("Processed Image", output); // display it
 
      cvReleaseImage(&output);
  }
}
When you create an image with OpenCV, you have to specify three parameters. The first one is the image size specified using a struct called CvSize and that contains the width and the height. The second one is the depth of the image which specifies the data type that will be associated with each pixel. Normally, an image is made of 8-bit pixel (i.e. from 0 to 255), but you can also create images made of integers or of floating point values. The different types are specified using defined constants, the main one being IPL_DEPTH_8U (unsigned char), IPL_DEPTH_16S (signed integer) and IPL_DEPTH_32F (single precision floating point number). Finally, the third parameter is the number of channels which is 1 in the color of a gray image and 3 for a color image. In the case of a color image, the channels are interleaved meaning that the image data is arranged such that the 3 channel values of a pixel are given in sequence, i.e. Blue channel of pixel 0, Green channel of pixel 0, Red channel of pixel 0, followed by B of 1, G of 1, R of 1, and B of 2, G of 2, R of 2, etc. In the code above, the image is formatted to be identical to the input image.

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

Top of the page

 

(c) Robert Laganiere 2011