Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

to train the SVM (or any other ml classifier), you will need a single float Mat with the image features, each on a row, and a integer Mat with the resp. class ids per image. we'll use cv::glob(), to traverse the image directories:

int loadClass(const String &dir, int label, Mat &trainData, Mat &trainLabels, const Size &siz) {
    vector<String> files;
    glob(dir, files);
    for (size_t i=0; i<files.size(); i++) {
        Mat img = imread(files[i], 0);
        if (img.empty()) continue;
        resize(img, img, siz); // all images need to be of same size
        img.convertTo(img, CV_32F); // float data

        trainData.push_back(img.reshape(1,1)); // add it as a single, flat row
        trainLabels.push_back(label); // integer label
    }
    return (int)trainLabels.size();
}

...

Mat trainData, trainLabels;
Size siz(60,60); // 3600 elements
int child  = loadClass("./images/child/*.png", 0, trainData, trainLabels, siz);
int adult  = loadClass("./images/adult/*.png", 1, trainData, trainLabels, siz);
int senior = loadClass("./images/senior/*.png", 2, trainData, trainLabels, siz);

model->train(ml::TrainData::create(trainData, ml::ROW_SAMPLES, trainLabels));

to train the SVM (or any other ml classifier), you will need a single float Mat with the image features, each on a row, and a integer Mat with the resp. class ids per image. we'll use cv::glob(), to traverse the image directories:

Mat loadImg(const String &name, const Size &siz) {
    Mat img = imread(files[i], 0);
    if (img.empty()) return img;
    resize(img, img, siz); // all images need to be of same size
    img.convertTo(img, CV_32F); // float data
    return img.reshape(1,1); //flattened to a single row
}

int loadClass(const String &dir, int label, Mat &trainData, Mat &trainLabels, const Size &siz) {
    vector<String> files;
    glob(dir, files);
    for (size_t i=0; i<files.size(); i++) {
        Mat img = imread(files[i], 0);
loadImg(files[i], siz);
        if (img.empty()) continue;
        resize(img, img, siz); // all images need to be of same size
        img.convertTo(img, CV_32F); // float data

        trainData.push_back(img.reshape(1,1)); trainData.push_back(img); // add it as a single, flat row
        trainLabels.push_back(label); // integer label
    }
    return (int)trainLabels.size();
}

...

Mat trainData, trainLabels;
Size siz(60,60); // 3600 elements
int child  = loadClass("./images/child/*.png", 0, trainData, trainLabels, siz);
int adult  = loadClass("./images/adult/*.png", 1, trainData, trainLabels, siz);
int senior = loadClass("./images/senior/*.png", 2, trainData, trainLabels, siz);

model->train(ml::TrainData::create(trainData, ml::ROW_SAMPLES, trainLabels));

...

Mat query = loadImg("some/test.png", siz); // same processing for train & test !
int predicted = (int)model->predict(query);

to train the SVM (or any other ml classifier), you will need a single float Mat with the image features, each on a row, and a integer Mat with the resp. class ids per image. we'll use cv::glob(), to traverse the image directories:

Mat loadImg(const String &name, const Size &siz) {
    Mat img = imread(files[i], 0);
imread(name, 0); // load grayscale
    if (img.empty()) return img;
img; // or bust
    resize(img, img, siz); // all images need to be of same size
    img.convertTo(img, CV_32F); // float data
    return img.reshape(1,1); //flattened to a single row
}

int loadClass(const String &dir, int label, Mat &trainData, Mat &trainLabels, const Size &siz) {
    vector<String> files;
    glob(dir, files);
    for (size_t i=0; i<files.size(); i++) {
        Mat img = loadImg(files[i], siz);
        if (img.empty()) continue;
        trainData.push_back(img); // add it as a single, flat row
        trainLabels.push_back(label); // integer label
    }
    return (int)trainLabels.size();
}

...

Mat trainData, trainLabels;
Size siz(60,60); // 3600 elements
int child  = loadClass("./images/child/*.png", 0, trainData, trainLabels, siz);
int adult  = loadClass("./images/adult/*.png", 1, trainData, trainLabels, siz);
int senior = loadClass("./images/senior/*.png", 2, trainData, trainLabels, siz);

model->train(ml::TrainData::create(trainData, ml::ROW_SAMPLES, trainLabels));

...

Mat query = loadImg("some/test.png", siz); // same processing for train & test !
int predicted = (int)model->predict(query);