Showing posts with label OpenCV. Show all posts
Showing posts with label OpenCV. Show all posts

Monday, November 8, 2010

OpenCV – 02


Working with Videos in OpenCV


#include <stdio.h>
#include <cv.h>
#include <highgui.h>

int main(void)

{
    /* Create an object that decodes the input video stream. */
    CvCapture *input_video = cvCaptureFromFile("VideoPlay_Demo.avi");
    if (input_video == NULL)
    {
        /* Either the video didn't exist OR codec doesn't support */
        fprintf(stderr, "Error: Can't open video.\n");
        return -1;
    }


    /* Read the video's frame size out of the AVI. */
    CvSize frame_size;
    frame_size.height = (int) cvGetCaptureProperty(input_video, CV_CAP_PROP_FRAME_HEIGHT);
    frame_size.width = (int) cvGetCaptureProperty(input_video, CV_CAP_PROP_FRAME_WIDTH);


    /* Determine the number of frames in the AVI. */
    long number_of_frames;
    number_of_frames = (int) cvGetCaptureProperty(input_video, CV_CAP_PROP_FRAME_COUNT);


    /* Create a windows called "VideoPlay_Demo" for output.
     * window automatically change its size to match the output. */
    cvNamedWindow("VideoPlay_Demo", CV_WINDOW_AUTOSIZE);


    long current_frame = 0;
    while(true)
    {
        static IplImage *frame = NULL;
        
        /* Go to the frame we want */
        cvSetCaptureProperty( input_video, CV_CAP_PROP_POS_FRAMES, current_frame );


        /* Get the next frame of the video */
        frame = cvQueryFrame( input_video );
        if (frame == NULL)
        {
            fprintf(stderr, "Error: Hmm. The end came sooner than we thought.\n");
            return -1;
        }
        
        /* Now display the image */
        cvShowImage("VideoPlay_Demo", frame);
        
        /* And wait for. If the argument is 0 then it waits forever otherwise it waits that number of milliseconds */
        cvWaitKey(60);
        current_frame++;
        if (current_frame < 0)
            current_frame = 0;
        if (current_frame >= number_of_frames - 1)
            current_frame = number_of_frames - 2;
    }
}


.

Thursday, October 14, 2010

OpenCV - 01

OpenCV Customization with VS2008

  1. Installation
OpenCV is an open source library for computer vision development. There are some very interesting sample applications included with this library. Before we can start we need to be able to build the library.
  1. Requirements
  • OpenCV Library
  • Visual Studio 2008 Express C++
  • Windows Platform SDK
  1. Optional
  • Visual Studio 2008 Express C++ SP1
  1. Procedure
    1. Step 1 - Installing Visual C++ and Platform SDK
When you get to Step 3, add this line to "Include Files" (assume default install location) C:\Program Files\Microsoft Platform SDK for Windows \Include\mfc this is because OpenCV requires some files that are inside that folder.
  1. Step 2 - Update Visual C++ with Service Pack 1 (Optional)
Service Packs for the each of the Visual Studio products are available on one page from Microsoft. Visual Studio 2008 Express SP1.
  1. Step 3 - Install OpenCV
Get OpenCV Library and download OpenCV 2.1 for Windows http://sourceforge.net/projects/opencvlibrary/files/opencv-win/2.1/ Installation is straight forward.
  1. Step 4 - Customising Visual C++ for use with OpenCV
We need to setup the directories for Open Visual C++
  • Open Visual C++
  • Choose menu "Tools" and select "Options"
  • In "Projects and Solutions" and go to "VC++ Directories"
  • Show directories for "Library Files"
  • Add "C:\Program Files\OpenCV2.1\lib" to the list of directories

(The important library files are: cv.lib, cvaux.lib, cxcore.lib, cvcam.lib, highgui.lib)
Similarly,
  • Show directories for "Include Files"
  • Add "C:\Program Files\OpenCV2.1\include\opencv" to the list of directories
(The important include files are: cvaux.h, cxcore.h, cv.h, highgui.h, cvcam.h)
And,
  • Show directories for "Source Files"
  • Add "C:\Program Files\OpenCV2.1\src" to the list of directories
Finally,
  • Show directories for "Executable Files"
  • Add "C:\Program Files\OpenCV2.1\bin" to the list of directories

  1. Step 5 - Open "OpenCV Workspace .NET 2008"
In the start menu a folder called "Open CV" was created when you installed OpenCV. Inside the folder "OpenCV" there is a shortcut called "OpenCV Workspace .NET 2008" Open "OpenCV Workspace .NET 2008" This will load OpenCV solution within Visual C++ There is one last thing you need to do, to be able to compile OpenCV.
  • Choose menu "Build" and Select "Configuration Manager"
  • Change "Active solution configuration" to either "Debug" or "Release"
  1. Step 6 - Build
Now that everything is installed and setup; building OpenCV should be a breeze.
  • Choose menu "Build" and Select "Build Solution"
  1. Creating Application at A Glance
Now, we will try to create application based on OpenCV at a glance. First, we will try to create console application (without Graphical User Interface / GUI)
  • Create new console project by following the picture below

Right click and add new file

  1. OpenCV Application with Graphical User Interface
  • Click File >> New >> Project >> Visual C++ >> Windows Forms Application (don't forget to enter your application's name)
  • Set additional directories and libraries, as you do in creating console application (step 3 previous sections).
  • The main important thing set your general properties of Common Language Runtime Support. To do this, select your project at the left windows, right click >> Properties >> Configuration Properties >> General >> Common Language Runtime Support 
  • Change from "No Common Language Runtime Support" to "Common Language Runtime Support" …….and happy coding!
  1. Reference

.