Saturday, February 22, 2014

Display Images as Video with Open CV

Till now we have seen how we can use Open CV with the C and C++ to display images. You may wonder is there any way to display the images continuously. Yes definitely we can do. Just we have to place the image display code in a loop. Is this enough... Think for a while...
How to write a loop we can use for loop or while loop or any other loop. But before that we must have knowledge of number of images as well as names of the images moreover we must know the directory where the images are stored. From this we can infer that we must get the names of the images from the desired directory. for this we can use dirent.h header file. Which hides all the implementation details of the process. It returns the file names with the file format as well. You can refer this link for more details.
The fallowing code is useful for this.

#include "stdafx.h"
#include "cv.h"
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;

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 destroy()=0;
};

class imagetovideo:public base
{
public:
imagetovideo(char* folderpath);
void input();
void process();
void destroy();
};

void imagetovideo::input()
{
const char* mpath=path.c_str();
dp=opendir(mpath);
}
void imagetovideo::process()
{
cvNamedWindow("Video",0);
cvResizeWindow("Video",640,320);
int c=0;
while(ep=readdir(dp))
{

c++;
if(c>2)
{
puts(ep->d_name);
fullpath=path+(ep->d_name);
full_path=fullpath.c_str();
puts(full_path);
IplImage* img=cvLoadImage(full_path);
cvShowImage("Video",img);
if(cvWaitKey(1000)==27)
{
cvDestroyWindow("Video");
break;
}
cvReleaseImage(&img);
}
}
}

void imagetovideo::destroy()
{
closedir (dp);
cvDestroyWindow("Video");
}
imagetovideo::imagetovideo(char* folderpath)
{
path=folderpath;
}
int main(int argc,char** argv)
{
char* path;
printf("Enter the path to the image");
gets(path);
imagetovideo obj1(path);
obj1.input();
obj1.process();
obj1.destroy();
return 0;
}

Here we have a base class. Which is implemented as abstract class. Our main class which has code is inherited from the base class. This derived class provides the functionality for our program. It contains three methods and one constructor. The constructor initializes the  path to the folder path. init method initializes the variables available in the dirent header file. The process method gets the file names one by one from the folder and displays the corresponding image in the window. The destroy method releases all the resources used by the Open CV as well as the dirent header file.

Caution: If the folder contains files other than the images the code breaks. Because the dirent header file gets all the files irrespective of the file format even hidden files. We are directly displaying the images with out any filtering. we assumed that the folder contains only images. The reason behind code breaking is the cvload image display only images. If we passed files other than the images this function having no knowledge of the processing. So the code breaks.

No comments:

Post a Comment

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