#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>
#include <windows.h>
using namespace std;
// LPCWSTR转string
std::string WChar2Ansi(LPCWSTR pwszSrc)
{
int nLen = WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, NULL, 0, NULL, NULL);
if (nLen <= 0) return std::string("");
char* pszDst = new char[nLen];
if (NULL == pszDst) return std::string("");
WideCharToMultiByte(CP_ACP, 0, pwszSrc, -1, pszDst, nLen, NULL, NULL);
pszDst[nLen - 1] = 0;
std::string strTemp(pszDst);
delete[] pszDst;
return strTemp;
}
// 利用winWIN32_FIND_DATA读取文件下的文件名
void readImgNamefromFile(char* fileName, vector <string> &imgNames)
{
// vector清零 参数设置
imgNames.clear();
WIN32_FIND_DATA file;
int i = 0;
char tempFilePath[MAX_PATH + 1];
char tempFileName[50];
// 转换输入文件名
sprintf_s(tempFilePath, "%s/*", fileName);
// 多字节转换
WCHAR wstr[MAX_PATH] = { 0 };
MultiByteToWideChar(CP_ACP, 0, tempFilePath, -1, wstr, sizeof(wstr));
// 查找该文件待操作文件的相关属性读取到WIN32_FIND_DATA
HANDLE handle = FindFirstFile(wstr, &file);
if (handle != INVALID_HANDLE_VALUE)
{
FindNextFile(handle, &file);
FindNextFile(handle, &file);
// 循环遍历得到文件夹的所有文件名
do
{
sprintf(tempFileName, "%s", fileName);
imgNames.push_back(WChar2Ansi((file.cFileName)));
imgNames[i].insert(0, tempFileName);
i++;
} while (FindNextFile(handle, &file));
}
FindClose(handle);
}
int main()
{
// 设置读入图像序列文件夹的路径
char* fileName = "..\\images\\test\\";
std::vector <string> imgNames;
// 获取对应文件夹下所有文件名
readImgNamefromFile(fileName, imgNames);
// 遍历对应文件夹下所有文件名
for (int i = 0; i < imgNames.size(); i++)
{
cv::Mat img = cv::imread(imgNames[i]);
if (!img.data)
return -1;
/* 可添加图像处理算法code*/
cv::imshow("im", img);
cv::waitKey(0);
}
return 0;
}
转载:http://blog.csdn.net/zhuwei1988