How to traverse all the pictures under the folder (pyhton&c++) __c++

Source: Internet
Author: User
Tags glob
Preface

Although this article is about traversing pictures, it is also possible to traverse other files.

In the process of image processing, most of the time only need to deal with the single picture. But once you combine image processing with machine learning, or do something a little larger, you often have to deal with a lot of pictures. And here, one of the most fundamental questions is how to traverse these pictures.

People who have done face recognition with OPENCV should know that the project did not traverse the picture, but instead used a helper scheme to generate a file containing all the path of the picture at.txt, and then read all the pictures through this path. And this auxiliary file not only contains the path of the picture, but also contains the corresponding label of the picture. So in the training of the time directly through the auxiliary file to read the training of the pictures and tags.

In fact, if you go to the tutorial, you will find that the At.txt generation is implemented through Python code. So today, let's take a look at how to use C + + to implement all the pictures in the folder traversal.

Before that, of course, the code for Python traversal is given before it can be used. python traversal

In the previous Sudoku project, the image processing used to traverse the folder all the pictures. The main use of the Glob module. Glob is one of Python's own file-manipulation-related modules with little content that you can use to find files that fit your purpose.

# encoding:utf-8
Import glob as GB
import cv2

#Returns a list of all folders with participant numbers
img _path = Gb.glob ("numbers\\*.jpg") 
for Path in Img_path:
    img  = cv2.imread (path) 
    cv2.imshow (' img ', img )
    Cv2.waitkey (1000)
C + + traversal 1. OpenCV with function glob () traversal

OpenCV with a function glob () can traverse the file, if you use this function, traversing the file is very simple. This function is very powerful and it should be more convenient to use this function in face recognition than in at.txt. A reference example is shown below.

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

using namespace std;
using namespace CV;

Vector<mat> Read_images_in_folder (cv::string pattern);

int main ()
{
    cv::string pattern = "g:/temp_picture/*.jpg";
    vector<mat> images = Read_images_in_folder (pattern);

    return 0;   
}

Vector<mat> Read_images_in_folder (cv::string pattern)
{
    vector<cv::string> fn;
    Glob (Pattern, FN, false);

    vector<mat> images;
    size_t count = Fn.size (); Number of PNG files in Images folder for
    (size_t i = 0; i < count; i++)
    {
        images.push_back imread (fn[i ]));
        Imshow ("img", Imread (Fn[i));
        Waitkey (1000);
    }
    return images;
}

It should be noted that the path and pattern here are cv::string. 2. Write a function to traverse a folder yourself

Under Windows, no dirent.h is available, but you can write a traversal function based on windows.h. This is a bit like the glob principle and implementation of the above.

#include <opencv2\opencv.hpp> #include <iostream> #include <windows.h>//For Windows systems using
namespace Std;

using namespace CV; void Read_files (std::vector<string> &filepaths,std::vector<string> &filenames, const string

&directory);
    int main () {string folder = ' G:/temp_picture/';
    Vector<string> Filepaths,filenames;
    Read_files (filepaths,filenames, folder);
        for (size_t i = 0; i < filepaths.size (); ++i) {//mat src = imread (filepaths[i));
        Mat src = imread (folder + filenames[i]);
        if (!src.data) cerr << "Problem loading image!!!" << Endl;
        Imshow (Filenames[i], SRC);
    Waitkey (1000);

return 0; } void Read_files (std::vector<string> &filepaths, std::vector<string> &filenames, const string
    &directory) {HANDLE dir;

    Win32_find_data File_data; if (dir = findfirstfile (Directory + "/*"). C_STR (), &file_data) = = = Invalid_handle_value) return;
        * No files found/do {const string file_name = File_data.cfilename;
        Const string File_path = Directory + "/" + file_name;

        const BOOL Is_directory = (File_data.dwfileattributes & file_attribute_directory)!= 0;
            if (file_name[0] = = '. ')

        Continue

        if (is_directory) continue;
        Filepaths.push_back (File_path);
    Filenames.push_back (file_name);

    while (FindNextFile (dir, &file_data));
FindClose (dir); 
 }
3. Boost based

If the computer is configured with boost library, using boost library to achieve this function is relatively simple. In order to use this I also specialized completely compiles the boost.

However, only the filesystem was used.

#include <boost/filesystem.hpp> #include <iostream> #include <opencv2\opencv.hpp> using namespace CV
;
using namespace Std;

using namespace Boost::filesystem;

void Readfilenamesboost (vector<string> &filenames, const string &folder);
    int main () {string folder = ' G:/temp_picture/';
    Vector<string> filenames;
    Readfilenamesboost (filenames, folder);

        for (size_t i = 0; i < filenames.size (); ++i) {Mat src = imread (folder + filenames[i]);
        if (!src.data) cerr << "Problem loading image!!!" << Endl;
        Imshow ("img", SRC);
    Waitkey (1000);
return 0;
    } void Readfilenamesboost (vector<string> &filenames, const string &folder) {Path directory (folder);
    Directory_iterator ITR (directory), End_itr;

    String current_file = Itr->path (). String (); for (; Itr!= end_itr; ++itr) {if (Is_regular_file ()) {Itr->pathG filename = Itr->path (). FileName (). String ();
        Returns just filename filenames.push_back (filename); }
    }
}

The various methods are recorded here so that they can be searched later.

Public number cvpy, sharing the actual content of OpenCV and Python. Each piece will emit the complete code. Welcome attention.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.