OpenCV C++ 遍历文件夹下所有文件

 

如题。

一、获取完整路径

#include <opencv2\opencv.hpp>
#include <string>

int main(int argc, char* argv[]) {

    std::string folder_path = "D:\\database\\test\\*.*"; //path of folder, you can replace "*.*" by "*.jpg" or "*.png"
    std::vector<cv::String> file_names;  
    cv::glob(folder_path, file_names);   //get file names

    cv::Mat img;   //try show it when file is a picture

    for (int i = 0; i < file_names.size(); i++) {
        std::cout << file_names[i] << std::endl;
        img = cv::imread(file_names[i]);
        if (!img.data) {
            continue;        
        }
        //do some work with the img you readed
        cv::imshow("img", img);
        cv::waitKey(300);
    }
    std::getchar();  //check your output on terminal
}

终端输出:

 

二、 获取文件名称

#include <opencv2\opencv.hpp>
#include <string>

int main(int argc, char* argv[]) {

    std::string folder_path = "D:\\database\\test\\*.*"; //path of folder, you can replace "*.*" by "*.jpg" or "*.png"
    std::vector<cv::String> file_names;  
    cv::glob(folder_path, file_names);   //get file names

    cv::Mat img;   //try show it when file is a picture

    for (int i = 0; i < file_names.size(); i++) {

        int pos = file_names[i].find_last_of("\\");
        std::string img_name(file_names[i].substr(pos + 1));
        std::cout << img_name << std::endl;

        img = cv::imread(file_names[i]);
        if (!img.data) {
            continue;        
        }
        //do some work with the img you readed
        cv::imshow("img", img);
        cv::waitKey(300);
    }
    std::getchar();  //check your output on terminal
}

 

可以使用 C++文件操作库 `dirent.h` 和图像处理库 `opencv` 来遍历文件夹中的图片。 首先,需要包含头文件: ```cpp #include <dirent.h> #include <opencv2/opencv.hpp> ``` 然后,可以使用 `opendir` 和 `readdir` 函数遍历文件夹,使用 `cv::imread` 函数读取图片: ```cpp DIR *dir; struct dirent *ent; if ((dir = opendir("your_folder_path")) != NULL) { while ((ent = readdir(dir)) != NULL) { std::string filename(ent->d_name); if (filename.find(".jpg") != std::string::npos || filename.find(".png") != std::string::npos) { // read image cv::Mat image = cv::imread("your_folder_path/" + filename); // do something with image } } closedir(dir); } else { // handle error } ``` 注意,在读取图片时,需要判断文件名是否以 `.jpg` 或 `.png` 结尾。如果需要处理其他类型的图片,可以相应地修改判断条件。 完整代码示例: ```cpp #include <iostream> #include <dirent.h> #include <opencv2/opencv.hpp> int main() { DIR *dir; struct dirent *ent; if ((dir = opendir("your_folder_path")) != NULL) { while ((ent = readdir(dir)) != NULL) { std::string filename(ent->d_name); if (filename.find(".jpg") != std::string::npos || filename.find(".png") != std::string::npos) { // read image cv::Mat image = cv::imread("your_folder_path/" + filename); if (image.empty()) { std::cout << "Failed to read image " << filename << std::endl; continue; } // do something with image cv::imshow("Image", image); cv::waitKey(0); } } closedir(dir); } else { std::cout << "Failed to open directory" << std::endl; return 1; } return 0; } ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值