2016-10-12 19 views
0

私はベクトルにファイルフォルダから画像を読み込もうとしています。 しかし、私のイメージマットがベクターにpush_backするのに失敗したようです。Opencv push_back imageベクトルにマッチ<Mat>

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; 
    } 

} 
+0

あなたは 'imgs.emplace_back(imread(imgPath));'を試したことがありますか? – 1201ProgramAlarm

+0

まだ試したが、まだ何もしていない。あなたの提案をありがとう! – user4029119

+0

と 'cout <<"の出力img: "<< img.rows <<" "<< img.cols << endl;'は "img:0 0" 'ではありません? – Micka

答えて

0

は、ここに私のコードです。私は今問題がどこから来たのか知っています!

imgs[0]の行と列を印刷したので、ベクトルのマットが空であると思っていました。 しかし、最初の2つのファイルを無視して、ファイルディレクトリを読むのを忘れてしまいます。 最初の2つのファイルは画像ではなく、./../です。だから、これらの2つのファイルをスキップするだけで私の問題は解決されます。

関連する問題