【问题标题】:Loading an image from a folder using OpenCV 3.0 in Windows在 Windows 中使用 OpenCV 3.0 从文件夹加载图像
【发布时间】:2016-01-19 14:57:35
【问题描述】:

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

首先我没有文件dirent.h,所以我下载了它以便让DIR* 和“dirent*”结构访问这些文件。一切似乎都很好,但现在当我到达线路时

字符串文件名 = 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());

提前致谢。

【问题讨论】:

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


    【解决方案1】:

    对于更简单的解决方案,只需使用 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]);
       ...
    }
    

    【讨论】:

    • 这不仅更简单,而且避免了需要在 OP 中注释掉的特定于平台的行。
    • @berak 我尝试了您的建议,但甚至进入了“for”循环。我知道“路径”实际上是“图像路径”,不是吗?
    • @JoseleMG "path" 是一个带有可选图像类型过滤器的文件夹,例如 *.jpg,如果您使用过滤器,它也会忽略(讨厌)隐藏的东西,例如 'thumbs.db'窗户
    • @berak 非常感谢您的帮助!最后我已经加载了图像!
    • @berak 我还有一个问题:如何根据他的框架从保存图像的代码中创建一个文件夹?例如,我想自动创建文件夹“Frame1”,文件夹“Frame2”并实时保存图像。
    猜你喜欢
    • 2016-12-28
    • 2012-10-20
    • 2011-07-07
    • 2016-04-01
    • 2012-07-28
    • 2012-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多