C++とopencvの下でフォルダの下のすべての画像を遍歴して保存する方法
C++
およびopencv
フォルダの下のすべての画像を巡回して保存する方法第一方式:採用
glob
関数Opencv
中glob
関数ファイルパス取得、以下のチェーン参照https://blog.csdn.net/GeorgeAI/article/details/81035422
// VS2019
#include
#include
#include
#include
#include
#include
#include
#include < fstream>
#define _CRT_SECURE_NO_DEPRECATE
using namespace std;
using namespace cv;
/* 0000-0001-...... */
int main(int argc, char** argv)
{
string path = "I:\\Multest_2_1\\";//
string write_path = "I:\\Multest_2_1\\";// ,
vector src_name;
glob(path, src_name, false);// opncv glob , path, src_name, /home/chuyi/xingqiu/homework/homework1/rgb/001.png
if (src_name.size() == 0)
{
cerr << "That's no file in " << path << endl;
exit(1);
}
for (int i = 0; i < src_name.size(); ++i)
{
Mat image = imread(src_name[i]);
if (image.empty())
{
cerr << "Read image " << src_name[i] << " failed!";
exit(1);
}
//imshow("test", image);
//waitKey(0);
string new_name = write_path + format("/%04d", i) + ".jpg";// 4 0
//cout << new_name <
第二の方式:採用
sprintf_S
sprintf_s
()はsprintf
()のセキュリティバージョンであり、バッファ長を指定することで回避sprintf
()に存在するオーバーフローリスクsprintf
参考``https://blog.csdn.net/yishizuofei/article/details/78195255` コード2を添付:
// sprintf_s
/* */
int main()
{
char input_filename[255]; // 256 input_filename
char output_filename[255];
int file_numbeers = 24; //
for (int i = 1; i <= file_numbeers; i++)
{
//input 24
sprintf_s(input_filename, "%s\\%d.jpg", "I:\\Multest_2_1\\", i);
Mat image = imread(input_filename);
Mat imageGray;
if (image.channels() == 3)
{
cvtColor(image, imageGray, CV_BGR2GRAY);
}
else
{
image.copyTo(imageGray);
}
//imshow("img", imageGray);
Mat res;
resize(image, res, Size(imageGray.cols/4, imageGray.rows/4),INTER_NEAREST);
//
sprintf_s(output_filename, "%s\\%d.jpg", "I:\\Multest_2_1_temp\\", i);
imwrite(output_filename, res);
cout << "resize " << i << " " << endl;
}
}
3つ目の方法:画像ファイル名を変更する必要はありません
https://blog.csdn.net/duiwangxiaomi/article/details/88911917
//
void getFiles(string path, vector& files,vector& filenames)
{
intptr_t hFile = 0;//intptr_t uintptr_t :typedef long int/ typedef unsigned long int
struct _finddata_t fileinfo;
string p;
//assign , 。
if ((hFile = _findfirst(p.assign(path).append("\\*").c_str(), &fileinfo)) != -1)
{
do
{
if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
// , , . .. , ? !!!
{
files.push_back(p.assign(path).append("\\").append(fileinfo.name));
filenames.push_back(fileinfo.name);
}
} while (_findnext(hFile, &fileinfo) == 0);
_findclose(hFile);
}
}
//
int main()
{
char windowname[10];
string filePath = "I:\\Multest_2_1\\"; //
string dst_filePath = "I:\\Multest_2_1_temp\\"; //
vector files;
vector filenames;
getFiles(filePath, files, filenames);
int number = files.size();//
for (int i = 0; i < number; i++)
{
string saveFilename = dst_filePath + filenames[i]; //
Mat img = imread(files[i]);
imwrite(saveFilename, img); //
// imshow
//sprintf_s(windowname, "%d_picture", i);
//imshow(windowname, img);
//waitKey(1000);
//cvDestroyAllWindows();
}
cout << " " << files.size() << " " << endl;
}
期間中に他の記事と投稿が参照されます.リンクは次のとおりです.
https://www.cnblogs.com/long5683/p/11192546.html
https://blog.csdn.net/weixin_39583302/article/details/105725600?utm_medium=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control&depth_1-utm_source=distribute.pc_relevant.none-task-blog-OPENSEARCH-3.control