C++与OpenCV同时批量处理图像数据

在单张调试图像效果时,可以将算法单次运行,以调试效果。但是,如果图像多到几万张时,我们就需要掌握批量处理图像的方法。在前辈的基础上做了微调。

string gallery_path = "D:\\1.jpg";//批量处理图像的路径
string probe_path = "D:\\……";//处理之后需要批量存入的路径
vector gallery_folders = listDir(gallery_path);
for (int g = 0; g < gallery_folders.size(); g++)
    {
        //vector image_list = listFile(gallery_path + "\\" + gallery_folders.at(g), "png");
        vector image_list = listFile( gallery_folders.at(g), "png");
        for (int f = 0; f < image_list.size(); f++)
        {
            int m = image_list.size();
            cout << m << endl;
            /*cout << gallery_path + "\\" + gallery_folders.at(g) + "\\" + image_list.at(f) << endl;*/
            string ga = gallery_folders.at(g);
            cout << "gallery_folders.at(g):  " << ga << endl;
            string ss = image_list.at(f);//this is picture number
            cout << "image_list.at(f)" << ss << endl;
            //string filename = gallery_path + "\\" + gallery_folders.at(g) + "\\" + image_list.at(f);
            string filename = gallery_folders.at(g) ; 
            Mat srcImage, grayImage;
            srcImage = imread(filename);
            //srcImage.convertTo(srcImage, CV_16UC1);
            //Show_Function(srcImage);
            //Show_Function1(srcImage, image_list.at(f));
            string filename_end = probe_path+"\\" + image_list.at(f);
            imwrite(filename_end, OutputImage);//grayImage    dst img1

        }
    }
vector listFile(string path, string format) {
    vector files;
    //文件句柄    
    intptr_t hFile = 0;
    //文件信息    
    struct _finddata_t fileinfo;
    string p;
    //if ((hFile = _findfirst(p.assign(path).append("\\*." + format).c_str(), &fileinfo)) != -1)
        if ((hFile = _findfirst(p.assign(path).c_str(), &fileinfo)) != -1)
    {
        do
        {
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                {
                    //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
                    GetAllFormatFiles(p.assign(fileinfo.name), files, format);
                }
            }
            else
            {
                files.push_back(p.assign(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\\").append(fileinfo.name)  
            }
        } while (_findnext(hFile, &fileinfo) == 0);
        _findclose(hFile);
    }
    return files;
}
//获取特定格式的文件名 
void GetAllFormatFiles(string path, vector& files, string format) {
    files.clear();
    //文件句柄    
    intptr_t hFile = 0;
    //文件信息    
    struct _finddata_t fileinfo;
    string p;
    if ((hFile = _findfirst(p.assign(path).append("/*." + format).c_str(), &fileinfo)) != -1)
    {
        do
        {
            if ((fileinfo.attrib &  _A_SUBDIR))
            {
                if (strcmp(fileinfo.name, ".") != 0 && strcmp(fileinfo.name, "..") != 0)
                {
                    //files.push_back(p.assign(path).append("\\").append(fileinfo.name) );  
                    GetAllFormatFiles(p.assign(path).append("/").append(fileinfo.name), files, format);
                }
            }
            else
            {
                files.push_back(p.assign(fileinfo.name));  //将文件路径保存,也可以只保存文件名:    p.assign(path).append("\\").append(fileinfo.name)  
            }
        } while (_findnext(hFile, &fileinfo) == 0);

        _findclose(hFile);
    }
}

https://blog.csdn.net/lingmengxiaotong/article/details/87266648?utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1.no_search_link&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EBlogCommendFromBaidu%7Edefault-1.no_search_link

你可能感兴趣的:(C/opencv,opencv,c++,计算机视觉)