编程菜鸟,从网上学到了些利用C++批处理图片的函数,在这里记录学习一下;
核心函数是这个
cv::glob(filename, filenames,false); // 将读取的文件存储在容器之中,false表示不读取子文件夹
因为up主要是做图像处理所以应用的也是相关图像相关的算法,下面随便写了一个二值化函数。
void binary_processImage(const char* imagePath, const char* savePath, double& param)
{
// 在此处编写你的图片处理逻辑,可以使用函数参数中的参数值
// 示例代码:对图片进行灰度处理并保存
cv::Mat image = cv::imread(imagePath);
cv::Mat grayImage;
cv::Mat imageOtsu;
cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
threshold(grayImage, imageOtsu, 0, param, cv::THRESH_OTSU); //Opencv Otsu算法
cv::imwrite(savePath, imageOtsu);
}
针对保存图像的命名,我想实现将图片保存到目标文件夹,并在原图片名字的基础上添加处理函数的名字,再累加计数,所以有下面的处理。
int dotindex = filename.find_last_of(".");
int slashindex = filename.find_last_of("\\");
std::string binary = "binary";
std::string symbol = "_";
std::string src_filename = filename.substr(slashindex, dotindex);
std::string tail_filename = filename.substr(dotindex);
std::string savePath = outputFolder+src_filename + symbol+ binary +symbol+std::to_string(count) + tail_filename;
这里注释一下,这个substr函数,在处理字符串名字上面挺好用的,函数第一个参数是初始字符的索引,第二参数是处理字符的长度,通过查找函数找到"\"与“.",即可找定位原文件的文件名字且不包含后缀格式。(这里说明一下,up实在windows系统下运行的,所以文件格式是"\",但是这个在c++编程里面是特殊字符所以需要加两个斜杠。)
完整的执行函数就是这样:
int count = 1;
for (const auto& filename : filenames)
{
int dotindex = filename.find_last_of(".");
int slashindex = filename.find_last_of("\\");
std::string binary = "binary";
std::string symbol = "_";
std::string src_filename = filename.substr(slashindex, dotindex);
std::string tail_filename = filename.substr(dotindex);
std::string savePath = outputFolder+src_filename + symbol+ binary +symbol+std::to_string(count) + tail_filename;
//执行函数
const char* inputPath = filename.c_str(); // 将 string 类的转换为char *
const char* outputPath = savePath.c_str();
binary_processImage(inputPath, outputPath, param);
count++;
}
函数的主干基本完成了,下面附上完整的执行代码。
#include <opencv2/opencv.hpp>
#include <iostream>
#include <vector>
#include <string>
#include <ctime>
void binary_processImage(const char* imagePath, const char* savePath, double& param) {
// 在此处编写你的图片处理逻辑,可以使用函数参数中的参数值
// 示例代码:对图片进行灰度处理并保存
cv::Mat image = cv::imread(imagePath);
cv::Mat grayImage;
cv::Mat imageOtsu;
cv::cvtColor(image, grayImage, cv::COLOR_BGR2GRAY);
threshold(grayImage, imageOtsu, 0, param, cv::THRESH_OTSU); //Opencv Otsu算法
cv::imwrite(savePath, imageOtsu);
}
void batch_process(const char* folderPath, const char* outputFolder,double ¶m)
{
cv::String filename = cv::format("%s*.jpg", folderPath); // 创建一个读取的文件目录
std::vector<cv::String> filenames; // 创建一个容器用来储存读取的目标文件
cv::glob(filename, filenames,false); // 将读取的文件存储在容器之中,false表示不读取子文件夹
int count = 1;
for (const auto& filename : filenames)
{
int dotindex = filename.find_last_of(".");
int slashindex = filename.find_last_of("\\");
std::string binary = "binary";
std::string symbol = "_";
std::string src_filename = filename.substr(slashindex, dotindex - slashindex);
std::string tail_filename = filename.substr(dotindex);
std::string savePath = outputFolder+src_filename + symbol+ binary +symbol+std::to_string(count) + tail_filename;
//执行函数
const char* inputPath = filename.c_str(); // 将 string 类的转换为char *
const char* outputPath = savePath.c_str();
binary_processImage(inputPath, outputPath, param);
count++;
}
}
int main() {
clock_t start_t = clock();
const char* folderPath = "D:/picture/"; // 设置图片所在文件夹路径
const char* outputFolder = "D:/picture/binary_result"; // 设置保存图片的文件夹路径
double param = 255; // 设置处理图片的参数
batch_process(folderPath, outputFolder, param);
//记录程序运行时间
clock_t end_t = clock();
std::cout << "run time : " << (double)(end_t - start_t) / CLOCKS_PER_SEC << "s" << std::endl;
return 0;
}