In previous post we have seen how we can display the sequence of images as video. But in that process we have seen a problem which can break our code. The basic condition we posed on our code is the folder contains only images. It does not have any other files which are not supported by Open CV.
In this post I will try to explain how we can get rid of this problem by using the regular expressions. These regular expressions are available now in c++ as well.To use this feature we need to include the regex header file. and we have to use the name space std::tr1
#include "stdafx.h"
#include "cv.h"
#include <regex>
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;
using namespace std::tr1;
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()
{
std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");
cmatch result;
cvNamedWindow("Video",0);
cvResizeWindow("Video",640,320);
while(ep=readdir(dp))
{
regex_search(ep->d_name, result, rgx);
if(!result.empty())
{
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;
}
If you observe the above code nothing changed from the previous post code snippet. Only we have added the few lines of code which can control the flow of file paths to the cvShowImage function. Now the code is robust. This code will process only the image files. The regular expression filters out the other files which are not images.
The input for the path must be like D:/x/ in this D is the local disk drive number and the x is the folder containing the images. If the folder path is not specified in this manner code will not work.
In this code I used regular expressions to identify the file format. If the file is image then only file is passed to the cvShowImage function. I will explain the regular expression code lines used in this post.
std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");
observe the above line. You can see the extensions for the images as well as they ended with the dollar symbol. and they are separated with the | symbol. Here $ symbol indicates that in the given string search for the .jpg or .png or .jpeg etc at the end of the string.
cmatch result;
here result is the resul coming out from the regex_search(ep->d_name, result, rgx); expression. This expression processes the string passed in the first argument the third argument is the regular expression and the second one is the cmatch type.
If this method finds the match it will store that in the cmatch type variable. If the match is found the match will be stored here. So if this not empty the file is image file. else the file is not image.
In this post I will try to explain how we can get rid of this problem by using the regular expressions. These regular expressions are available now in c++ as well.To use this feature we need to include the regex header file. and we have to use the name space std::tr1
#include "stdafx.h"
#include "cv.h"
#include <regex>
#include "highgui.h"
#include <string>
#include <dirent.h>
using namespace std;
using namespace std::tr1;
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()
{
std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");
cmatch result;
cvNamedWindow("Video",0);
cvResizeWindow("Video",640,320);
while(ep=readdir(dp))
{
regex_search(ep->d_name, result, rgx);
if(!result.empty())
{
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;
}
If you observe the above code nothing changed from the previous post code snippet. Only we have added the few lines of code which can control the flow of file paths to the cvShowImage function. Now the code is robust. This code will process only the image files. The regular expression filters out the other files which are not images.
The input for the path must be like D:/x/ in this D is the local disk drive number and the x is the folder containing the images. If the folder path is not specified in this manner code will not work.
In this code I used regular expressions to identify the file format. If the file is image then only file is passed to the cvShowImage function. I will explain the regular expression code lines used in this post.
std::tr1::regex rgx("(.jpg$)|(.png$)|(.jpeg$)|(.bmp$)|(.gif)");
observe the above line. You can see the extensions for the images as well as they ended with the dollar symbol. and they are separated with the | symbol. Here $ symbol indicates that in the given string search for the .jpg or .png or .jpeg etc at the end of the string.
cmatch result;
here result is the resul coming out from the regex_search(ep->d_name, result, rgx); expression. This expression processes the string passed in the first argument the third argument is the regular expression and the second one is the cmatch type.
If this method finds the match it will store that in the cmatch type variable. If the match is found the match will be stored here. So if this not empty the file is image file. else the file is not image.