Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

imwrite sequence of images in a folder in opencv

Using VS 2010 in C++ and tried to put this in a for loop

String filename = "cropped_" + (ct+1);
imwrite(filename + ".jpg", img_cropped);

These are the filenames that came out:

ropped_.jpg
opped_.jpg
pped_.jpg

How do I do it? And how do I put them in a folder in the same directory as my source code?

like image 931
Masochist Avatar asked Jan 31 '13 03:01

Masochist


3 Answers

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);
like image 118
sgarizvi Avatar answered Oct 08 '22 07:10

sgarizvi


    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

like image 35
Masochist Avatar answered Oct 08 '22 07:10

Masochist


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);
like image 2
Radford Parker Avatar answered Oct 08 '22 06:10

Radford Parker