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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值