c++ - 在 Windows 中使用 OpenCV 3.0 从文件夹加载图像

标签 c++ windows visual-studio-2010 opencv3.0

我正在使用带有 OpenCV 3.0 的 Visual Studio 2010。我正在尝试从文件夹加载一些图像,但我遇到了问题。

首先我没有文件 dirent.h,所以我下载了它以获得 DIR* 和“dirent*”结构来访问这些文件。一切似乎都很好,但是现在当我排队的时候

string fileName = in_file->d_name;

我发现我无法访问文件名。

有人对此有任何想法吗?

这是代码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <opencv2/core/dirent.h>

#ifdef _WIN32
#include <io.h>
#else
#include <unistd.h>
#endif

#include <conio.h>
#include <windows.h>
#include <tchar.h> 
#include <stdio.h>
#include <strsafe.h>
#pragma comment(lib, "User32.lib")

#include <errno.h>
#include <iostream>
#include <time.h>
#include <limits.h>
#include <fstream>
#include <sys/stat.h>

#include <opencv2/core.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/ml.hpp>
#include <opencv2/opencv.hpp>

using namespace cv;
using namespace cv::ml;
using namespace std;

int patchWidth = 15;
int patchHeight = 15;


int main(int, char**)
{
    string imagesPath = "Images";
    string resultsPath = "Patches";

            DIR* FD;
            struct dirent* in_file;

            if (NULL == (FD = opendir (imagesPath.c_str())))
            {
                fprintf(stderr, "Error : Failed to open input directory\n");
                return 0;
            }

            while ((in_file = readdir(FD)))
            {
                    /* On linux/Unix we don't want current and parent directories
                     * If you're on Windows machine remove this two lines
                     */
              //      if (!strcmp (in_file->d_name, "."))
              //          continue;
              //      if (!strcmp (in_file->d_name, ".."))
              //          continue;

                    /* Open directory entry file for common operation */
                    /* TODO : change permissions to meet your need! */

                    string fileName = in_file->d_name;

                    string pathFile = imagesPath;
                    pathFile.append("/");
                    pathFile.append(fileName);
                    //pathFile.append(".jpg");
                    Mat img = imread(pathFile.c_str());

提前致谢。

最佳答案

对于更简单的解决方案,只需使用 cv::glob :

String imagesPath = "Images/*.png"; // it has filters, too !

vector<String> fn;
glob(path, fn, true); // recursive, if you want
for (size_t i=0; i<fn.size(); i++)
{
   Mat img = imread(fn[i]);
   ...
}

关于c++ - 在 Windows 中使用 OpenCV 3.0 从文件夹加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34880010/

相关文章:

c++ - 加载共享库 : libCstd. so.1 时出错:无法打开共享对象文件:没有这样的文件或目录

c++ - Windows 启动时的第二次自动运行

sql - Rails 服务器命令错误

c++ - 链接和编译 pcl

c++ - 为什么 IntelliSense 会显示我没有编写的构造函数?

c++ - MFC 列表框中的复选框在 VS2015 中显示不同

c++ - 在 Objective-C 中使用 .mm 文件有哪些怪癖/惊喜?

c++ - "std::endl"与 "\n"

c++ - 无法使用带有 std::move 的自定义删除器插入 std::unique_ptr

c++ - Windows 注册 I/O Win 8.1 RioCreateRequestQueue 错误 10045