What did you expect this to do? You always write the same image called "images.jpg". You could simply achieve your task via stringstreams (#include <sstream>)
if (response==1)
{
stringstream file;
file << "image" << count_4 << ".jpg";
count_4++;
cv::imwrite(file.str(), pre_img);
}
If you want to write to a certain folder, you could try this. Im not 100% sure this works on windows. On linux it worked with the linux folder structure.
file << "D:\images\image" << count_4 << ".jpg";
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);
کدهای خودم برای ذخیره متوالی چند عکس:
#include<sstream>
stringstream file;
int z=0;
int main()
vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
while (true)
file << "Alef-" << z << ".png";
imwrite(file.str(),imgLines,compression_params);
z++;
file.str("");
//اگر خط بالا نباشد حرف الف-1 بعد از ذخیره عکس اول حذف نمیشود و عکس بعدی با عنوان
//الف-1 الف-2 ذخیره میشود. یعنی پشت سرهم میافتند
از این کد به نظرم استفاده شود بهتر است:
#include<sstream>
int z=0;
int main()
vector<int> compression_params;
compression_params.push_back(IMWRITE_PNG_COMPRESSION);
while (true)
stringstream file;
file << "Alef-" << z << ".png";
imwrite(file.str(),imgLines,compression_params);
z++;
Setting Up OpenCV on Windows with Eclipse, MinGW and Cmake