opencv读取文件夹下所有文件

72 篇文章 1 订阅

如题。

一、获取完整路径

#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
}

 

评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值