通过后缀名遍历文件夹内的图像

来源:互联网 发布:mac 上给iphone传照片 编辑:程序博客网 时间:2024/06/14 00:01
#ifndef FILELOCATOR_H#define FILELOCATOR_H#include <stdio.h>#include <cstdlib>#include <iostream>#include <string.h>#include <fstream>#include <dirent.h>#include <vector>#include <algorithm>using namespace std;class FileLocator{public:    FileLocator();    vector<string> listFile(string path, string extension);private:};#endif // FILELOCATOR_H

#include "filelocator.h"using namespace std;bool compareNoCase (string first, string second){    int i=0;    while ((i < first.length()) && (i < second.length()))    {        if (tolower (first[i]) < tolower (second[i])) return true;        else if (tolower (first[i]) > tolower (second[i])) return false;        i++;    }    if (first.length() < second.length()) return true;    else return false;}FileLocator::FileLocator(){}vector<string> FileLocator::listFile(string path, string extension) {    vector<string> files;    DIR *pDIR;    struct dirent *entry;    if( pDIR=opendir(path.c_str()) ) {        while(entry = readdir(pDIR)) {            if( strcmp(entry->d_name, ".") != 0 && strcmp(entry->d_name, "..") != 0 && strcmp(entry->d_name+strlen(entry->d_name)-strlen(extension.c_str()),extension.c_str()) == 0 )            {                if(path[strlen(path.c_str())-1] == '/')                    files.push_back(string(path) +  string(entry->d_name));                else                    files.push_back(string(path) + string("/") + string(entry->d_name));            }        }        closedir(pDIR);    }    std::sort(files.begin(), files.end(),compareNoCase);    return files;}
#include <opencv2/opencv.hpp>#include "filelocator.h"using namespace cv;using namespace std;#define EXT ".png"int main(){string pattern = "F:\\Test\\test";vector<string> fn;FileLocator FL;fn = FL.listFile(pattern, EXT);size_t  count = fn.size();cout << "一共有" << count << "张图像需要处理!" << endl;for (int i = 0; i < count; i++){Mat image = imread(fn[i]);imshow("Source", image);waitKey(30);}cout << "处理完成!\n" << endl;return 0;}


阅读全文
0 0