Archive for the ‘OpenCV’ Category

AVIFile API to capture video frames in Microsoft Windows

Posted: 21st March 2011 by Adnan Noon in OpenCV

To develop computer vision applications which require video processing , the most important step is to read video frame by frame. OpenCV is great C++ API for such applications. OpenCV function cvQueryFrame can grab and return a frame from camera or file but it doesn’t support all video formats. DirectShow API is another option to perform frame capture but it is too complex for simple applications. Microsoft provides another relatively simple simple API for reading videos named AVIFile API.  It contains set of powerful functions to read, create and edit AVI files. It is part of a larger API called VFW  (Video for Windows). The VfW AVI support should use the same DirectShow facility with dynamically configurable filter/codec support in Windows. It means that if you get an avi file with unsupported encoding, installing the corresponding codec to Windows should make the application written with AVIFile API work immediately without any code change. Recently I developed a computer vision application to remove noise from low quality/resolution night videos using AVIFile API. I was unable to read H.264 videos in my application untill I installed H.264 codec using ffdshow.

There is very good C# Wrapper available for AVIFile functions on codeproject.

Information on AVI File Formats is available here.

The only drawback of AVIFile API is that the code is not portable and works only on windows in contrast to OpenCV which works on Linux as well.

If you are developing an image processing application using OpenCV and you want to display pixel values of images, it might not be possible to display all pixels on console due to large size of images. Following code snippet dumps the pixel values of OpenCV image to a text file in readable format. You can then open the created file in any text editor and analyze the pixel values of image.
Recently i was developing an image processing application and my output file was DLL rather than executable. One of the functions in code was returning black image, to debug my code i wrote this function and dumped intermediate images to files and eventually i was able to fix the bug.

#include<stdio.h>
/*
Function takes OpenCV image as input and dumps its pixels to a file specified by filename in function arguments.
*/
int WritePixelsToFile(IplImage *img,const char *filename)
{
  /*open a file in text mode with write permissions.*/
 FILE *file = fopen(filename, "wt");
 if(file==NULL)
 {
  /*If unable to open the specified file display error and return.*/
  perror("Unable to open specified file");
  return -1;
  }
  int i,j;
  CvScalar s;
  CvSize size=cvGetSize(img);
  for(i=0;i< size.height;i++)
     {
      for(j=0;j< size.width;j++)
        {
         s=cvGet2D(img,i,j); // get the (i,j) pixel value
         fprintf (file,"%f\t",s.val[0]); // dump the (i,j) pixel value in file 
        }                         
      fprintf(file,"\n");//write new line in file to seperate rows.         
     }
     /*release the file pointer.*/
 fclose(file);
 return 1;
} 

Installing OpenCV2.0 with MS Visual Studio 2008

Posted: 19th July 2010 by Adnan Noon in OpenCV

OpenCV is very usefull library to develop professional and real time image/video processing applications. It was originally developed by Intel and therefore applications developed with OpenCV run very fast on Intel processors. I have recently started working with OpenCV and found it really great for implementation of  Computer Vision advanced algorithms. This tutorial will explain the basic installation procedure of OpenCV library and how it can be integrated with Microsoft Visual Studio 2008.

  1. Download OpenCV2.0 from here
  2. Install to some folder for example C:\OpenCV2.0
  3. Now this ( C:\OpenCV2.0 ) folder contains source files for OpenCV. These source files need to be build for specific development environment which in our case is Microsoft Visual Studio 2008. CMake utility can be used to generate build files for Microsoft Visual Studio 2008.
  4. Download and Install CMake utility. downlaod here
  5. Open CMake and select the source directory for OpenCV source files i.e ( C:\OpenCV2.0). Select the build directory. for example C:\OpenCV2.0\Build.
  6. Once source and build directories are selected. Press Configure button and specify generator i.e Microsoft Visual Studio 9  (2008)  and hit finish.
  7. Once you hit finish button CMake will read all source files and display all variables used in these files. All variables are displayed in red for the first time, press Configure again untill there are no red entries.
  8. Press Generate button and build files for Microsoft Visual Studio 2008 will be generated in C:\OpenCV2.0\Build.
  9. After Verifying that build files have been generated, start Microsoft Visual Studio 2008 and open solution C:\OpenCV2.0\Build\OpenCV.sln. Build this solution using Build->Build Solution in MS Visual Studio 2008, if build is successful it will generate required binaries of OpenCV and you are done.

Create your first OpenCV project and follow this tutorial to configure your project. I will explain this in my next tutorial on How to create and configure first OpenCV project.