1、ls /home/xuli/Documents/OpenCV/example/opencv_do-series_pic/data/series/ >te
st.txt
【使用ls命令将目录series(根据实际情况进行更改)中的图片名列举出来,然后利用>将文 件名重定向到test.txt中(.txt的名字可根据需要自定义)
2、cat test.txt
【使用cat命令查看一下上述操作的是否成功,如果成功则会显示出文件名,不成功则啥也不显示
因为我是在data下面写入命令的,所以生成的test.txt是在data文件夹下哈
//背景:某文件夹series下有若干张图片,对每一张图片进行处理,将处理后的图片保存至新的文件夹save下
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main() {
ifstream file("../data/series/test.txt");
int img_index = 0;
while (!file.eof()) {
char txt_cont[200];
file.getline(txt_cont, 200);
char img_file[200], save_file[200];
sprintf(img_file, "../data/series/%s", txt_cont);
sprintf(save_file, "../save/%d.jpg", img_index);
Mat src = imread(img_file);
img_index++;
imwrite(save_file, src);
}
}
//使用SetFileNames()方法读取序列图像。
序列图像命名规则如图:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
int main() {
//先读取1-32张图试试看
for(int i = 1 ; i < 33; i++) {
//使用SetFileNames()方法读取序列图像。
Mat srcGray;
char imgname[200];
sprintf(imgname, "../data/IMG-0005-%05d.jpg", i);//%05d代表文件名的宽度
srcGray = imread(imgname, 0);
cout << i << endl;
imshow("读取", srcGray);
waitKey(122);
}
return 0;
}
下面这个例子是序列图像32一循环,先显示1,33,65,77…这样的,再显示2,34,66,78…这样的
int main() {
//-------------------(1)使用SetFileNames()方法读取序列图像--------------------------------------------
for(int j = 1 ; j < 33; j++) {
for(int i = 0 ; i < 19; i++) {
Mat srcGray;
char imgname[200];
//%05d代表文件名的宽度,因为后面是00001这样的五位数
sprintf(imgname, "../data/IMG-0005-%05d.jpg", j + 32 * i);
srcGray = imread(imgname, 0);
cout << i + 1 << endl;
imshow("读取", srcGray);
waitKey(100);//读取的间隔时间
}
cout << " " << endl;
waitKey(1000);//读取的间隔时间
}
return 0;
}
char path[200];
sprintf(path, "../save/%d.jpg", i);
//处理
Mat edgeMat;
..................................
imwrite(path, edgeMat);
参考:1、https://blog.csdn.net/weixin_40144166/article/details/83624806
2、https://blog.csdn.net/hei_ya/article/details/51387624