vector <mat> opencv问题

时间:2015-05-23 23:33:55

标签: c++ opencv mat

我正在尝试将大小为19x19的图像读入矢量。我有2429个这样的图像。但是当我运行我的代码时,我确信某些Mat图像不会被读入矢量。这是一个记忆问题吗?如果有,任何人都可以帮助我。在我的代码中声明了声明之后我确认了这一点。谢谢你的帮助。 编辑:我删除了所有if else语句,并将其替换为格式说明符。当我构建设计矩阵 X_train 时,正好在 ex = 1703时,我的断言失败了。我检查了我的图像集围绕那些 ex 值,它们看起来很好。我无法理解我哪里出错了。

#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 个答案:

答案 0 :(得分:0)

我得到了它的工作。我没有在我手动设置19行和19列的图像上循环(假设每个图像为19x19),而是使用了Mat的班级成员&#39;行&#39;和&#39; cols&#39;。我找到的解决方案只是替换为以下内容:

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.*/
}