Docstoc

http://uiucmultitouchscreen.googleco...e%20How-to.doc

Document Sample
http://uiucmultitouchscreen.googleco...e%20How-to.doc Powered By Docstoc
					   How To Install Open CV and Configure it for basic Image Processing (FOR
   WINDOWS ONLY)

   Installing Eclipse

   First Install Eclipse, a development environment for C/C++ that‟s very nice and very
   free
   1. http://www.eclipse.org/downloads/
       -Download Eclipse IDE for C/C++ Developers
       -Unzip the file
       -You can now run Eclipse by double clicking on the eclipse icon in the unzipped
       folder. You don‟t even have to install it.

Note: To run eclipse, you must have the java runtime environment installed. Most
computers already have this installed in order to use Java Applets. If, however, an error
message occurs when you open eclipse stating that you need the Java Runtime
Environment to run eclipse, simple download it from
http://www.java.com/en/download/manual.jsp and follow the installation instructions.

Installing C/C++ compiler

Now your Eclipse won‟t do anything without a C/C++ toolchain to compile and run the
code. So you need to get the program MinGW, the windows port of the unix gcc
compiler.

   1. Download and run the MinGW setup program from:

   http://sourceforge.net/project/downloading.php?groupname=mingw&filename=MinG
   W-5.1.3.exe&use_mirror=superb-west

   2. Select download and install the MinGW base tools and the g++ compiler. You
      may select the Current or Candidate version of these tools. You may also install
      any of the other available compilers as well.

   If you want the gdb debugger and support for Make files (this is optional), follow
   these steps at:
   http://help.eclipse.org/help33/index.jsp?topic=/org.eclipse.cdt.doc.user/concepts/cdt_
   c_before_you_begin.htm

   1. (Optional)The MinGW setup program currently does not install the gdb debugger.
      To install the debugger, download the file from the following location: gdb-
      6.6.tar.bz2
   2. (Optional)Extract the contents of the file gdb-6.6.tar.bz2 to the same location
      where you installed MinGW.
   3. (Optional)If you want to use Makefile projects, download and run the setup
      program from the following location: MSYS-1.0.10.exe. MSYS provides an
      implementation of make and related command line tools. This is not required for
      other types of projects with the MinGW toolchain, which use CDT's internal build
      tools to perform the build.

Installing OpenCV

Now you need to get the OpenCV software.

   1. Go to
      http://sourceforge.net/project/showfiles.php?group_id=22870&package_id=1693
      7
   2. download OpenCV_1.0.exe
   3. Execute OpenCV_1.0.exe. Note the path where it is installed.
   4. See http://opencvlibrary.sourceforge.net/ for OpenCV documentation

Creating a Test Project

Now it is time to create a test project!

Opening Eclipse:
   1. Run Eclipse.exe
   2. Select a workspace such as C:\Documents and Settings\FTIRworkspace,
      anywhere where you want to store your files.

Create A New Project:
   1. File->New-> New C++ Project
   2. Name the project OpenCVTest
   3. Select MINGW toolchain
   4. Press Next and then Finish
   5. File-> New-> Source File
   6. Name it mytest.cpp
   7. Copy the program below into the mytest.cpp window:
///////////////////////////////////////////////////////////////////////
/
//
// hello-world.cpp
//
// This is a simple, introductory OpenCV program. The program reads an
// image from a file, inverts it, and displays the result.
//
///////////////////////////////////////////////////////////////////////
/
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <cv.h>
#include <highgui.h>
int main(int argc, char *argv[])
{
  IplImage* img = 0;
  int height,width,step,channels;
  uchar *data;
  int i,j,k;

    if(argc<2){
      printf("Usage: main <image-file-name>\n\7");
      exit(0);
    }

    // load an image
    img=cvLoadImage(argv[1]);
    if(!img){
      printf("Could not load image file: %s\n",argv[1]);
      exit(0);
    }

  // get the image data
  height    = img->height;
  width     = img->width;
  step      = img->widthStep;
  channels = img->nChannels;
  data      = (uchar *)img->imageData;
  printf("Processing a %dx%d image with %d
channels\n",height,width,channels);

    // create a window
    cvNamedWindow("mainWin", CV_WINDOW_AUTOSIZE);
    cvMoveWindow("mainWin", 100, 100);

    // invert the image
    for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
      data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

    // show the image
    cvShowImage("mainWin", img );

    // wait for a key
    cvWaitKey(0);

    // release the image
    cvReleaseImage(&img );
    return 0;
}
     8. Save the Project
     9. Now you need to configure the project properties
           a. Click on MyTest.cpp
           b. Go to Project->Properties
           It should look like this:
        c. Go to C/C++ Build-> Settings on the Left side
        d. In the middle, go to GCC C++ Compiler->Directories and add the
            following paths. You will need to know where your OpenCV is installed.
            In this case it is in C:\Program Files\OpenCV\:
                  i. "C:\Program Files\OpenCV\cxcore\include"
                 ii. "C:\Program Files\OpenCV\cv\include"
               iii. "C:\Program Files\OpenCV\cvaux\include"
                iv. "C:\Program Files\OpenCV\otherlibs\highgui"
                 v. "C:\Program Files\OpenCV\otherlibs\cvcam\include"
        e. Almost there. Now go to MinGW C++ Linker-> Libraries in the middle
        f. On the right, add a Library Search Path of
                  i. "C:\Program Files\OpenCV\lib"
        g. Then add the following libraries:
                  i. cv
                 ii. cvaux
               iii. highgui
                iv. cvcam
                 v. cxcore
10. Now on the right, go to C/C++ Build-> Environment
11. Edit the PATH variable. At the end of the existing PATH variable, add a
    semicolon and then C:\Program Files\OpenCV\bin
    Note: Be careful not to change existing PATH settings
12. Now close Eclipse and restart your computer
13. After restarting, open Eclipse and choose the same workspace
14. You should see your project in the project explorer. Go to Project->Build All
15. Click on the Console window at the bottom of the screen. If there are errors you
    will need to troubleshoot them. If you‟ve got no errors you‟ve compiled
    successfully!
Running your compiled program
   1. Use windows to navigate to your workspace folder. Under your project you
       should see a „debug‟ folder with a file in it named OpenCVTest.exe (assuming
       you named your project OpenCVTest)
   2. Copy a bitmap image (filename.bmp) into the debug folder as a test.
   3. Now go to the start menu->All Programs->Accessories->Command Prompt
          a. You will see the command prompt open up. Navigate to your debug folder
              using the commands „cd‟ to change directory and „dir‟ to see what files are
              in the folder you are looking at. Type „cd ..‟ to change to the directory
              above the one you are looking at
          b. Once in the debug folder, type „dir‟, you should see OpenCVTest.exe and
              your bitmap file.
          c. Type „OpenCVTest filename.bmp‟ this will run the program and display
              the output- congratulations!
Output-




                                       goes to

Let me know if you have any troubles or questions. Email me at ejohns24@uicu.edu
Loading an Image from a Camera
   1. Add a new source file to your project, going to File -> New -> Source File
   2. Name the file testcam.cpp
   3. Copy the following code into your file:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>

// A Simple Camera Capture Framework
int main() {

    CvCapture* capture = cvCaptureFromCAM( CV_CAP_ANY );
    if( !capture ) {
      fprintf( stderr, "ERROR: capture is NULL \n" );
      getchar();
      return -1;
    }

    // Create a window in which the captured images will be presented
    cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

    // Show the image captured from the camera in the window and repeat
    while( 1 ) {
      // Get one frame
      IplImage* frame = cvQueryFrame( capture );
      if( !frame ) {
        fprintf( stderr, "ERROR: frame is null...\n" );
        getchar();
        break;
      }

      cvShowImage( "mywindow", frame );
      // Do not release the frame!

    //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux
version),
    //remove higher bits using AND operator
    //Pressing the ESC key will exit the program
    if( (cvWaitKey(10) & 255) == 27 ) break;
  }

    // Release the capture device housekeeping
    cvReleaseCapture( &capture );
    cvDestroyWindow( "mywindow" );
    return 0;
}

     4. Go to project->Build All
           a. Note: if you have other files with a „main‟ function in it, such as the
               mytest.cpp made earlier, right click on the file and select exclude from
               build. This will ensure that testcam.cpp is the compiled file.
     5. Attach your camera to the USB port or firewire camera
     6. Run your .exe output file.
Importing a video file
   1. Add a new source file to your project, going to File -> New -> Source File
   2. Name the file testvid.cpp
   3. Copy the following code into your file, this will load the video and invert the
      colors of the video:
#include "cv.h"
#include "highgui.h"
#include <stdio.h>

// A Simple Video Capture Framework to load a video and invert the
output
int main() {

//image data variables
        int height,width,step,channels;
        uchar *data;
        int i,j,k;

//change ‘test.avi’ to the name of your test video file
  CvCapture* capture = cvCaptureFromAVI( "test.avi" );
  if( !capture ) {
    fprintf( stderr, "ERROR: capture is NULL \n" );
    getchar();
    return -1;
  }

  // Create a window in which the captured images will be presented
  cvNamedWindow( "mywindow", CV_WINDOW_AUTOSIZE );

  // Show the image captured from the camera in the window and repeat
  while( 1 ) {
    // Get one frame
    IplImage* frame = cvQueryFrame( capture );
    if( !frame ) {
      fprintf( stderr, "ERROR: frame is null...\n" );
      getchar();
      break;
    }
    // get the image data
    height     = frame->height;
    width      = frame->width;
    step       = frame->widthStep;
    channels = frame->nChannels;
    data       = (uchar *)frame->imageData;

     // invert the image for display
     for(i=0;i<height;i++) for(j=0;j<width;j++) for(k=0;k<channels;k++)
       data[i*step+j*channels+k]=255-data[i*step+j*channels+k];

     cvShowImage( "mywindow", frame );
     // Do not release the frame!

    //If ESC key pressed, Key=0x10001B under OpenCV 0.9.7(linux
version),
         //remove higher bits using AND operator
         //pressing ESC will exit program.
         if( (cvWaitKey(10) & 255) == 27 ) break;
    }

    // Release the video, housekeeping
    cvReleaseCapture( &capture );
    cvDestroyWindow( "mywindow" );
    return 0;
}

        4. Go to project->Build All
                a. Note: if you have other files with a „main‟ function in it, such as the
                    mytest.cpp made earlier, right click on the file and select exclude from
                    build. This will ensure that testcam.cpp is the compiled file.
        5. Move your video file (named test.avi) to the Debug folder with your output .exe
           file in it.
        6. Run OpenCVTest.exe, a window should appear with your inverted video.

Installing OpenCV Blob Detection
   1. Download the library at http://opencvlibrary.sourceforge.net/cvBlobsLib
   2. Install and configure Microsoft Visual C++ Express Edition 2008 (see Touchlib
        HowtoV1.doc)
   3. Unzip the cvBlobsLib directory
   4. Open the cvblobslib solution file (.sln) in Visual C++ Express Edition (There may
        be conversion warnings)
   5. Go to Tools->Options->Projects and Solutions->VC++ directories
   6. Choose “Include Files” in the upper right. Make sure there is a path to your
        OpenCV settings: cv\include, cvaux\include, cxcore\include
   7. Add C:\Program Files\Microsoft Platform SDK\Include\mfc to your include path
   8. Choose “Library Files” in the upper right.
   9. Add C:\Program Files\OpenCV\lib if it isn‟t already there
   10. Build the project!

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:25
posted:5/9/2010
language:English
pages:8