First time here? Check out the FAQ!

Ask Your Question
0

How to run the code repetitively and save result separately ?

asked Aug 15 '13

Steve gravatar image

updated Aug 15 '13

Hi, I am a beginner in Opencv and C++ programing. According to this tutorial, it is necessary to run the code manually and after all you will have just one pic saved.

I have a very basic problem, I want to run the code every second automatically and in every cycle store the pics somewhere. Could any body help me in this field?

Thank you in advance.

Preview: (hide)

2 answers

Sort by » oldest newest most voted
3

answered Aug 15 '13

updated Aug 15 '13

If you have not very large DB of pics (10-15 pics), the most easiest way to do it, is with bat file, that call your program and parse the input parameters (argc and **argv).

The other way is to use dirrent.h That is small example, that will do the job ;)

int check_ext(string path)   //only images
{
    string ext[]={"BMP","DIB","JPEG","JPG","JPE","PNG","PBM","PGM","PPM","SR","RAS","TIFF","TIF"
            ,"bmp","dib","jpeg","jpg","jpe","png","pbm","pgm","ppm","sr","ras","tiff","tif"};
    for(unsigned int i=0;i<(sizeof(ext)/sizeof(ext[0]));i++)
    {
        if(path.substr(path.find_last_of(".") + 1) == ext[i])return 1;
    }
    return 0;
}

//someware open files
if ((dir = opendir ((path).c_str())) != NULL)
{
    while ((ent = readdir (dir)) != NULL)
    {
        if(check_ext(ent->d_name))Mat img = imread(path+"\\"+ent->d_name);
        //processing               
        imwrite(outputdir+"\\"+ent->d_name,img);
    }
}
Preview: (hide)

Comments

@Spas Hristov Thank you. Since I want to run it consecutively, there would be huge number of images that after a while I should compare them and delete the repetitive. So it seems the second one is better but I am not sure where I can add these to the source code.

Steve gravatar imageSteve (Aug 16 '13)edit
1

answered Aug 15 '13

Moster gravatar image

updated Aug 15 '13

I guess the simplest way would be some while or for loop and put some sleep at the end of the loop, so that it wait until the next execution.

For the different images, you could do it like this. Take a simple image name and always concatenate it with a number in every loop. You could use the loop variable i or just increase an int after every loop.

Could look like this in c++:

    char *imageName = "image"; 
    char *jpgN = ".jpg";
    stringstream s;
    s << imageName << i << jpgN;

Then you can use imwrite (s.str(), image);

Preview: (hide)

Comments

@Moster Thank you man. The loop is the way which I already guessed, but I couldn't implement it in the code. This time I will try again with your idea.

Steve gravatar imageSteve (Aug 16 '13)edit

Question Tools

Stats

Asked: Aug 15 '13

Seen: 527 times

Last updated: Aug 15 '13