实验环境:VS2010 + OpenCV2.4.9
#include <iostream>
#include <afx.h>
#include <vector>
#include <string>
#include "cv.h"
#include "highgui.h"
using namespace std;
using namespace cv;
vector<Mat> tMat;
void loadImage()
{
CString path;
path = L"test\\*.jpg";
CFileFind finder;
bool selectFlag = finder.FindFile(path);
while(selectFlag)
{
selectFlag = finder.FindNextFileW();
if(selectFlag)
{
CString str = finder.GetFilePath();
CT2A pszConvertedAnsiString(str);
std::string fileName(pszConvertedAnsiString);
//加载为灰度图
Mat img = imread(fileName,CV_LOAD_IMAGE_GRAYSCALE);
tMat.push_back(img);
cout << "Load Image Successful!" << endl;
}
}
}
int main(int argc, char** argv)
{
loadImage();
//cout << tMat.size() << endl;
//vector<string> wndName(tMat.size());
//int count = 0;
//for(int i = 0;i < tMat.size();i++)
//{
// if(tMat.at(i).empty())
// {
// cout << "!!!" << endl;
// continue;
// }
// else
// {
// wndName.at(i) = i;
// namedWindow(wndName.at(i),CV_WINDOW_AUTOSIZE);
// imshow(wndName.at(i),tMat.at(i));
// ++count;
// }
//}
为什么这种方法读出来的图片数目多一个,
但是第一个和第二个为空。why?也就是说总的能显示出来的图片少了一个
tMat也是从第二个开始显示的
//cout << "count is: " << count << endl;
vector<string> wndName(tMat.size());
for(int i = 0;i < tMat.size();i++)
{
string typex= "Type: ";
char name = 'i';
putText(tMat[i],typex,Point( tMat[i].rows/2,tMat[i].cols/4),CV_FONT_HERSHEY_COMPLEX, 1, Scalar(255, 255, 255));
wndName[i] = i;
namedWindow(wndName[i],CV_WINDOW_AUTOSIZE);
imshow(wndName[i],tMat[i]); //显示图片
}
waitKey();
system("pause");
return 1;
}
主函数中注释掉的code和下面的code实则是一样的,只是保留了调试的代码而已。但是的确通过这种方法加载读入并显示的的时候,少了一张图片,不知道为什么????╮(╯▽╰)╭