C++ 17 遍历文件夹图片文件进行循环操作
#include <filesystem>
using namespace std;
namespace fs = std::filesystem;
string path = filepath; // 替换为指定目录的路径 for (const auto& entry : fs::directory_iterator(path)) { if (entry.is_regular_file() && entry.path().extension() == ".jpg") { //std::cout << entry.path() << std::endl; // 输出jpg文件路径 fs::path dPath = entry.path(); string ddPath = dPath.string(); cv::Mat m = cv::imread(ddPath, 1); if (m.empty()) { fprintf(stderr, "cv::imread %s failed\n", imagepath); return -1; } double t1 = (double)cv::getTickCount(); std::vector<lineMLSD> lines; detect_mlsd(m, lines); //////////draw///////////////// cv::Mat image = m.clone(); for (size_t i = 0; i < lines.size(); i++) { const lineMLSD& obj = lines[i]; fprintf(stderr, "%d %d %d x %d\n", obj.sp.x, obj.sp.y, obj.ep.x, obj.ep.y); cv::line(image, obj.sp, obj.ep, cv::Scalar(0, 0, 255), 2); } float fps = (double)cv::getTickFrequency() / (cv::getTickCount() - t1); cv::putText(image, "FPS : " + std::to_string(int(fps)), cv::Point(50, 50), cv::FONT_HERSHEY_SIMPLEX, 1.5, cv::Scalar(50, 150, 50), 4); cv::imwrite("out/" + entry.path().filename().string(), image); } }