SLAM数据集序列图片如何批量处理
原创:SLAM爬坑之行
引言:在SLAM学习过程中,有时候需要对大量的数据集比如TUM/KITTI等图片序列进行批量处理,这个时候需要用到opencv中的函数对图像进行批量处理的话,会大大减少我们的时间,提高工作效率。
详细流程:
1、数据集下对大批量图片文件名的导出
我们采用的是TUM实验室中的rgbd_dataset_freiburg2_pioneer_slam的RGB图像序列,每当看见这些密密麻麻的数据图像的时候,就会有一种无力感,所以需要批量处理。[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-W9k03k7b-1592542839980)(D:\SLAM\SLAM基础\文章\rgbseires.png)]
-
1、使用ls >命令将目录RGB(根据实际情况进行更改)中的图片名列举出来,然后利用>将文 件名重定向到test.txt中(.txt的名字可根据需要自定义),效果入下图,可以看见rgb中的图像序列已经被保存,终端输入下列指令后结果如下图所示
ls rgb >text.txt
#include <iostream>
#include<opencv2/opencv.hpp>
#include<iostream>
#include<time.h>
#include<fstream>
#include<string>
int main(){
//! 指定文件夹路径
std::string path = "C:\\Users\\12692\\Source\\Repos\\picture_process\\seires.txt";
std::ifstream file(path);
int img_index = 0;
char img_infile[100], img_savefile[100];
//!函数file.eof(),如果没到文件末尾,返回值为0;当读取两百个图像时,停止读取
while (!file.eof() && img_index < 200) {
//图像名称
char img_name[500];
//getline()读取一行,内容存放在img_name[0],读取的最大字节数为500
file.getline(img_name, 500);
//将字符串img_name标准化打印到img_infile中
sprintf_s(img_infile, "../rgb/%s", img_name);
sprintf_s(img_savefile, "../save/tum%03d.png", img_index);
cv::Mat src = cv::imread(img_infile);
cv::Mat imgGray;
//将彩色图像处理成灰度图
cvtColor(src, imgGray, CV_BGR2GRAY);
img_index++;
//写入序列图片
cv::imwrite(img_savefile, imgGray);
}
}
下图是处理后的图像,这样就可以我们就可以看见原文件rgb中的200张彩色图像被批量处理成灰度图并且重命名。
3、常用举例按规律读取并显示
数据集命名规则如下所示,下列程序将按照序列号名称顺序读取
#include<opencv2/core/core.hpp>
#include<stdio.h>
#include<stdlib.h>
#include<opencv2/imgproc/imgproc.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<iostream>
#include <fstream>
using namespace std;
using namespace cv;
int main() {
//先读取100张图试试看
for(int i = 1 ; i < 100; i++) {
//使用SetFileNames()方法读取序列图像。
Mat srcGray;
char img_name[200];
sprintf(img_name, "../PATH/TUM%03d.jpg", i);//%05d代表文件名的宽度
srcGray = imread(imgname, 0);
cout << i << endl;
imshow("读取", srcGray);
waitKey(122);
}
return 0;
}
4、写入序列图片
char path[200];
sprintf(path, "../save/%d.jpg", i);
Mat edgeMat;
imwrite(path, edgeMat);
[参考]:https://blog.csdn.net/weixin_44723106/article/details/103034846?utm_medium=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase&depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3.nonecase