Showing posts with label Image processing with open cv and visula studio. Show all posts
Showing posts with label Image processing with open cv and visula studio. Show all posts

Saturday, May 10, 2014

Extract frame from video when user presses a key in Open CV

Capturing the frames from video using Open CV is very simple. Just we need to use cvSaveImage method from Open CV library. But some times we need to capture the images interactively like when the user presses a key. For doing this we can use the function kbhit(). this function returns non zero values when there is something in the keyboard buffer. So by using this we can interactively extract the frames.

By using the fallowing code we can accomplish this.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <conio.h>

void main()
{
IplImage * img;
CvCapture * v;
v = cvCreateFileCapture("E:/v.mp4");
int k = 0;
char text[10];
while(true)
{
if(_kbhit())
{
sprintf(text,"%s%d%s","E:/Image",k,".jpeg");
cvSaveImage(text,img);
getch();
}
else
{
img= cvQueryFrame(v);
cvWaitKey(0);
k++;
}
}
_getch();
}

we have one while loop which continuously reads the frames from the video. The if block will be executed when the user presses a key from keyboard else else block gets executed. In else block we are extracting the frames from the video we have one delay loop as well else the the computer reads all the frames one by one with in fraction of seconds.

To change the saved image file name every time we are using char buffor. By using sprintf we are changing the file name. cvSaveImage saves the image on the specifed path supplied as first argument and the image to be saved as second argument. getch() is used to clear the buffor else the if block executed continuously.

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...