【发布时间】:2016-10-12 03:43:10
【问题描述】:
我尝试将图像从文件夹读取到矢量中。 但是,我的图像 Mat 似乎无法 push_back 到向量中。
cout << "img vec:" << imgs[0].rows << " " << imgs[0].cols << endl; 的结果是img vec:0 0。
imgs.push_back(img.clone()); 和 imgs.push_back(img); 我都试过了。
这是我的代码:
#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include <algorithm>
#include <dirent.h>
#include <opencv/cv.h>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;
using namespace std;
void readImageFiles(string dirName, vector<cv::Mat> &imgs);
int _tmain(int argc, _TCHAR* argv[]){
vector<cv::Mat> imgs;
readImageFiles("training/", imgs);
cout << imgs.size() << endl;
cout << imgs[0] << endl;
/*
for(int i = 0; i < imgs.size(); i++){
imshow("img", imgs[i]);
waitKey();
}
*/
system("pause");
return 0;
}
void readImageFiles(string dirName, vector<cv::Mat> &imgs){
DIR *dir;
dir = opendir(dirName.c_str());
struct dirent *ent;
if (dir != NULL) {
while ((ent = readdir (dir)) != NULL) {
string imgPath(dirName + ent->d_name);
Mat img = imread(imgPath);
//cvtColor(img,img,CV_BGR2GRAY);
imgs.push_back(img.clone());
cout << "img:" << img.rows << " " << img.cols << endl;
cout << "img vec:" << imgs[0].rows << " " << imgs[0].cols << endl;
}
}else{
cout << "NULL" << endl;
}
}
【问题讨论】:
-
你试过
imgs.emplace_back(imread(imgPath));吗? -
刚试过,还是不行。感谢您的建议!
-
cout << "img:" << img.rows << " " << img.cols << endl;的输出不是 "img:0 0"`? -
cout << "img:" << img.rows << " " << img.cols << endl;的输出是img:128 128