imwrite sequence of images in a folder in opencv

You can use std::stringstream to build sequential file names:

First include the sstream header from the C++ standard library.

#include<sstream>

using namespace std;

Then inside your code, you can do the following:

stringstream ss;

string name = "cropped_";
string type = ".jpg";

ss<<name<<(ct + 1)<<type;

string filename = ss.str();
ss.str("");

imwrite(filename, img_cropped);

To create new folder, you can use windows' command mkdir in the system function from stdlib.h:

 string folderName = "cropped";
 string folderCreateCommand = "mkdir " + folderName;

 system(folderCreateCommand.c_str());

 ss<<folderName<<"/"<<name<<(ct + 1)<<type;

 string fullPath = ss.str();
 ss.str("");

 imwrite(fullPath, img_cropped);

    for (int ct = 0; ct < img_SIZE ; ct++){
    char filename[100];
    char f_id[3];       //store int to char*
    strcpy(filename, "cropped_"); 
    itoa(ct, f_id, 10);
    strcat(filename, f_id);
    strcat(filename, ".jpg");

    imwrite(filename, img_cropped); }

By the way, here's a longer version of @sgar91's answer


Try this:

char file_name[100];
sprintf(file_name, "cropped%d.jpg", ct + 1);
imwrite(file_name, img_cropped);

They should just go in the directory where you run your code, otherwise, you'll have to manually specify like this:

sprintf(file_name, "C:\path\to\source\code\cropped%d.jpg", ct + 1);