2015-05-23 98 views
-1

我试图将大小19x19的图像读入矢量。我有2429这样的图像数量。但是当我运行我的代码时,我确定一些Mat图像不会读入矢量中。这是一个记忆问题。如果是的话,任何人都可以帮助我。我在我的代码中声明了声明后证实了这一点。感谢您的帮助。 编辑:我删除了所有if else语句,并用格式说明符替换它。当我在 = 1703建筑设计矩阵X_train,正是我的断言失败。我检查了我在ex值附近的图像集,它们看起来很好。我无法理解我出错的地方。矢量<Mat> opencv问题

#include <iostream> 
#include <vector> 
#include <istream> 
#include <fstream> 
#include <random> 
#include <algorithm> 

#include "opencv2/opencv.hpp" 
#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp" 
#include "opencv2/imgproc/imgproc.hpp" 

#define NO_OF_IMAGES 2429 

using namespace std; 
using namespace cv; 
static int colSize = 0; 

vector<Mat> read_faces() { 
    vector<Mat> training_images; 
    string images_path = "images/train/face"; 
    string suffix = ".pgm"; 
    Mat img(19, 19, CV_8UC1); 
    for (int i = 0; i < NO_OF_IMAGES; i++) { 
     img = imread(cv::format("%s%05d.pgm", images_path.c_str(), i), 0); 
     training_images.push_back(img); 
    } 
    return training_images; 
} 

vector<Mat> extract_train_test_set(
    vector<Mat> faces/**< [in] vector of faces or matrices*/, 
    vector<Mat> &test_set /**< [out] 10% of images*/) { 

    /** 
    * Randomly select 90% of these images and collect them into a set training_set and 
    * the rest 10% in test_set. 
    */ 

    int percentage_train = (0.9f * NO_OF_IMAGES); 
    vector<Mat> training_set; 

    for (int i = 0; i < percentage_train; i++) { 
     Mat img = faces[i]; 
     assert(img.empty() == false); 
     training_set.push_back(img); 
    } 

    for (int i = percentage_train; i < NO_OF_IMAGES; i++) { 
     Mat img = faces[i]; 
     assert(img.empty() == false); 
     test_set.push_back(img); 
    } 

    return training_set; 
} 

int main(int argc, char **argv) { 

    vector<Mat> faces = read_faces(); /**< Reading faces into a vector of matrices. */ 

    random_shuffle(faces.begin(), faces.end()); /**< Shuffle the faces vector for creating a training set*/ 
    cout << faces.size() << endl; /**< Size of the vector of faces is 2429*/ 

    vector<Mat> training_set; /**< 90% images i.e 2186 are test images. */ 
    vector<Mat> test_set; /**< 10% images i.e 243 are test images. */ 

    training_set = extract_train_test_set(faces, test_set); 

    cout << " Training set size " << training_set.size() << endl; 
    cout << " Test set size " << test_set.size() << endl; 

    int dim = training_set[0].rows * training_set[0].cols; /**< 361 dimension vector. */ 

    Mat X_train(dim, training_set.size(), CV_8UC1); /**< 361 rows and 2186 columns.*/ 

    Mat m(19, 19, CV_8UC1); 
    int ex = 0; /**< Counter for indexing the images */ 

    while (ex < training_set.size()) { 
     m = training_set[ex];/**< Retrieve the image from training vector. */ 
     for (int i = 0; i < 19; i++) { 
      for (int j = 0; j < 19; j++) { 
       assert(m.empty() == false); 
       X_train.at<uchar>(colSize, ex) = m.at<uchar>(i, j); //each image is a 361 element vector 
       colSize++; 
      } 
     } 
     ex++; /**< Continue to next image. */ 
     colSize = 0; /**< Set to zero so as to continue to next image. That is a reset row index for next image.*/ 
    } 

    ofstream file_handle("images/train.dat", ios::trunc); 

    file_handle << X_train; 

    file_handle.close(); 

    cout << "Height " << X_train.rows << " Width " << X_train.cols << endl; 

    waitKey(0); 

    return 0; 
} 
+1

你如果/ elseif的东西是恐怖...也许你错过了一些情况 – Micka

+0

你得到什么问题? – CroCo

+0

使用重塑 http://docs.opencv.org/modules/core/doc/basic_structures.html?highlight=reshape#mat-reshape –

回答

0

我明白了。我使用Mat的类成员的'rows'和'cols',而不是在手动设置19行和19列的图像上进行循环(假设每幅图像为19x19)。解决方案我发现仅仅是替换为以下几点:

while (ex < training_set.size()) { 
    m = training_set[ex];/**< Retrieve the image from training vector. */ 
    cout << "Fine!! " << ex << endl; 
    assert(m.empty() == false); 
    for (int i = 0; i < m.rows; i++) { 
     for (int j = 0; j < m.cols; j++) { 
      X_train.at<uchar>(colSize, ex) = m.at<uchar>(i, j); //each image is a 361 element vector 
      colSize++; 
     } 
    } 
    ex++; /**< Continue to next image. */ 
    colSize = 0; /**< Set to zero so as to continue to next image. That is a reset row index for next image.*/ 
}