Showing posts with label Video player open cv. Show all posts
Showing posts with label Video player open cv. Show all posts

Thursday, February 27, 2014

Displaying the video with Open CV and C

In previous posts you have seen how we can load and display images with the Open CV and C language. Open CV can be used to process videos as well. Processing the videos is simple as the images with the Open CV. This is because the videos are made with the sequence of images. These images are called frames. Generally the videos has the frame rate of 24-30 called as fps. fps represents frames per seconds.

By using Open CV we will grab the each frame from the video from starting frame. After getting the frame from the video to memory we display it as a normal image. If we want to process the grabbed frame we can pass that frame to the processing function and we get result back.

The fallowing code snippet helps you to under stand this process.


#include "stdafx.h" 
#include "cv.h"
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;
CvCapture * capture;
    char* filename;
    string fullpath,path;
    DIR *dp;
    struct dirent *ep;
    const char* full_path;
class base
{
public:
virtual void input()=0;
virtual void process()=0;
virtual void play()=0;
virtual void destroy()=0;
};

class localfile:public base
{
public:
    localfile(char* file);
void input();
void process();
void play();
void destroy();
};
void localfile::input()
{
capture = cvCreateFileCapture(filename);
}
void localfile::process()
{
for ( ; ; )
    {
        play();
        cvWaitKey(33);
    }
}
void localfile::play()
{
 IplImage * frame = cvQueryFrame(capture);
 cvShowImage("Video",frame);
}
void localfile::destroy()
{
cvDestroyWindow("Video");
}
localfile::localfile(char* file)
{
filename=file;
}

int main(int argc,char** argv)
{
    char* path;
    printf("Enter the path to the Video");
    gets(path);
    localfile obj1(path);
    obj1.input();
    obj1.process();
    obj1.destroy();
    return 0;
}

When we run this code it asks for the path to the video file. Based on the path to the video the CvCapture structure will be initialized with the function cvCreateFileCapture which is available in Open CV . Now this structure helps us to get the frames as well as lot information called meta data of the video.

Open CV provides simple function called cvQueryFrame(); this function takes the CvCapture type variable as the argument. and returns the frame for each call to this function. It automatically returns the next frame from the video. After getting the frame we can display this frame as described in the previous posts.

DC motor control with Pulse Width Modulation Part 1

DC Motor intro DC motor is a device which converts electrical energy into kinetic energy. It converts the DC power into movement. The typica...