在 Windows 中使用 OpenCV 3.0 从文件夹中加载图像
Loading an image from a folder using OpenCV 3.0 in Windows
我正在使用Visual Studio 2010和OpenCV 3.0。我正在尝试从文件夹中加载一些图像,但我遇到了问题。
首先,我没有文件 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 directoryn");
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]);
...
}
相关文章:
- 从文件问题加载纹理 Android NDK 和 OpenGL
- 如何从资源文件(.rc 文件)加载位图?
- 将内存保存到文件并加载它而不必解析数据?
- 如何使用 FBX SDK 在 fbx 文件中加载嵌入的纹理
- 如何使用 C/C++ 和 SDL2 将文件字体加载到 RAM 中?
- 从不同的文件夹加载具有相同依赖 DLL 的相同 DLL
- 从电子项目运行的可执行文件未加载资源
- NDK c++从文件Android加载着色器
- Visual Studio - C++ - 以 32 位或 64 位构建和运行应用程序 - 从适当的程序文件目录加载 D
- Qt qrc 资源文件 - 无法加载图标
- 有效地从数据文件中加载数字
- 从具有两列的文件中加载二维阵列
- 有没有办法通过程序在特定时间获取文件部分加载内容的大小C++
- Unicode 字符未从文件正确加载
- 在Visual Studio中加载文本文件资源加载的内容超过了应有的水平
- 从"Resource Files"文件夹加载文件C++ Windows 应用程序 (win32)
- 使用本机DLL从同一文件夹加载程序集
- 我无法从文件夹加载dll
- 如何使OS X Cocoa应用程序从它所在的文件夹加载自定义框架
- android/opencv-从NDK中的应用程序文件夹加载级联文件