利用cxImage进行批量图像处理,可以按照以下步骤进行:
以下是一个简单的示例代码,展示如何使用cxImage库对一个文件夹内的所有图像进行批量处理(例如,调整大小并保存为新文件):
#include "cxImage.h"
#include <iostream>
#include <filesystem>
namespace fs = std::filesystem;
int main() {
std::string inputDir = "path/to/input/directory"; // 输入图像文件夹路径
std::string outputDir = "path/to/output/directory"; // 输出图像文件夹路径
int newWidth = 800; // 新宽度
int newHeight = 600; // 新高度
// 创建输出目录(如果不存在)
fs::create_directories(outputDir);
// 遍历输入目录中的所有图像文件
for (const auto& entry : fs::directory_iterator(inputDir)) {
if (entry.is_regular_file() && (entry.path().extension() == ".jpg" || entry.path().extension() == ".png")) {
try {
// 加载图像
cxImage img;
if (!img.Load(entry.path().string().c_str())) {
std::cerr << "Failed to load image: " << entry.path() << std::endl;
continue;
}
// 调整图像大小
img.ResizeImage(newWidth, newHeight, CXIMAGE_QUALITY_HIGH);
// 生成输出文件路径
std::string outputPath = outputDir + "/" + fs::path(entry.path()).filename().string();
// 保存图像
if (!img.Save(outputPath.c_str(), "JPEG")) { // 或者 "PNG",根据需要选择格式
std::cerr << "Failed to save image: " << outputPath << std::endl;
} else {
std::cout << "Processed and saved: " << outputPath << std::endl;
}
} catch (const std::exception& e) {
std::cerr << "Error processing image: " << entry.path() << " - " << e.what() << std::endl;
}
}
}
return 0;
}
std::filesystem库遍历输入目录中的所有文件,并检查文件扩展名是否为.jpg或.png。cxImage对象加载图像,调整其大小,并保存到指定的输出目录。通过以上步骤,你可以利用cxImage库轻松实现批量图像处理功能。