3
votes

I was looking at an example of the CvNormalBayesClassifier::train in which the input/output matrix is to be a 1D vector.

The example I was looking at achieved this by creating a cv::Mat matrix with 0 rows and 1000 columns using this line:

Mat trainingData(0, 1000, CV_32FC1);

Reading the basic data types in opencv documentation this is what I found for Mat:

There are many different ways to create Mat object. Here are the some popular ones:

using create(nrows, ncols, type) method or

    the similar constructor

Mat(nrows, ncols, type[, fill_value]) constructor.

In any way the first parameter is the rows. The way I look at it is even if we do create a 1000 column matrix it will atleast have 1 row. How can it have 0 rows?

Sorry if this is a very basic question.

update: upon request, here is the complete code.

    #include <vector>
#include <boost/filesystem.hpp>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace boost::filesystem;
using namespace cv;

//location of the training data
#define TRAINING_DATA_DIR "data/train/"
//location of the evaluation data
#define EVAL_DATA_DIR "data/eval/"

//See article on BoW model for details
Ptr<DescriptorMatcher> matcher = DescriptorMatcher::create("FlannBased");
Ptr<DescriptorExtractor> extractor = DescriptorExtractor::create("SURF");
Ptr<FeatureDetector> detector = FeatureDetector::create("SURF");

//See article on BoW model for details
int dictionarySize = 1000;
TermCriteria tc(CV_TERMCRIT_ITER, 10, 0.001);
int retries = 1;
int flags = KMEANS_PP_CENTERS;

//See article on BoW model for details
BOWKMeansTrainer bowTrainer(dictionarySize, tc, retries, flags);
//See article on BoW model for details
BOWImgDescriptorExtractor bowDE(extractor, matcher);

/**
 * \brief Recursively traverses a folder hierarchy. Extracts features from the training images and adds them to the bowTrainer.
 */
void extractTrainingVocabulary(const path& basepath) {
    for (directory_iterator iter = directory_iterator(basepath); iter
            != directory_iterator(); iter++) {
        directory_entry entry = *iter;

    if (is_directory(entry.path())) {

        cout << "Processing directory " << entry.path().string() << endl;
        extractTrainingVocabulary(entry.path());

    } else {

        path entryPath = entry.path();
        if (entryPath.extension() == ".jpg") {

            cout << "Processing file " << entryPath.string() << endl;
            Mat img = imread(entryPath.string());
            if (!img.empty()) {
                vector<KeyPoint> keypoints;
                detector->detect(img, keypoints);
                if (keypoints.empty()) {
                    cerr << "Warning: Could not find key points in image: "
                            << entryPath.string() << endl;
                } else {
                    Mat features;
                    extractor->compute(img, keypoints, features);
                    bowTrainer.add(features);
                }
            } else {
                cerr << "Warning: Could not read image: "
                        << entryPath.string() << endl;
            }

        }
    }
}
}

/**
 * \brief Recursively traverses a folder hierarchy. Creates a BoW descriptor for each image encountered.
 */
void extractBOWDescriptor(const path& basepath, Mat& descriptors, Mat& labels) {
    for (directory_iterator iter = directory_iterator(basepath); iter
            != directory_iterator(); iter++) {
        directory_entry entry = *iter;
        if (is_directory(entry.path())) {
            cout << "Processing directory " << entry.path().string() << endl;
            extractBOWDescriptor(entry.path(), descriptors, labels);
        } else {
            path entryPath = entry.path();
            if (entryPath.extension() == ".jpg") {
                cout << "Processing file " << entryPath.string() << endl;
                Mat img = imread(entryPath.string());
                if (!img.empty()) {
                    vector<KeyPoint> keypoints;
                    detector->detect(img, keypoints);
                    if (keypoints.empty()) {
                        cerr << "Warning: Could not find key points in image: "
                                << entryPath.string() << endl;
                    } else {
                        Mat bowDescriptor;
                        bowDE.compute(img, keypoints, bowDescriptor);
                        descriptors.push_back(bowDescriptor);
                        float label=atof(entryPath.filename().c_str());
                        labels.push_back(label);
                    }
                } else {
                    cerr << "Warning: Could not read image: "
                            << entryPath.string() << endl;
                }
            }
        }
    }
}

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

cout<<"Creating dictionary..."<<endl;
extractTrainingVocabulary(path(TRAINING_DATA_DIR));
vector<Mat> descriptors = bowTrainer.getDescriptors(); //descriptors from training images
int count=0;
for(vector<Mat>::iterator iter=descriptors.begin();iter!=descriptors.end();iter++)
{
    count+=iter->rows;
}
cout<<"Clustering "<<count<<" features"<<endl;
Mat dictionary = bowTrainer.cluster();
bowDE.setVocabulary(dictionary);
cout<<"Processing training data..."<<endl;
Mat trainingData(0, dictionarySize, CV_32FC1);
Mat labels(0, 1, CV_32FC1);
extractBOWDescriptor(path(TRAINING_DATA_DIR), trainingData, labels);

NormalBayesClassifier classifier;
cout<<"Training classifier..."<<endl;

classifier.train(trainingData, labels);

cout<<"Processing evaluation data..."<<endl;
Mat evalData(0, dictionarySize, CV_32FC1);
Mat groundTruth(0, 1, CV_32FC1);
extractBOWDescriptor(path(EVAL_DATA_DIR), evalData, groundTruth);

cout<<"Evaluating classifier..."<<endl;
Mat results;
classifier.predict(evalData, &results);

double errorRate = (double) countNonZero(groundTruth - results) / evalData.rows;
        ;
cout << "Error rate: " << errorRate << endl;

}
1
A 1D vector has 1 row and N columns (or vice versa). - Don Reba
A matrix with 0 rows is like the sound of one hand clapping. Very zen, but not very useful. To put any data into it, you'd need at least one row. - Thomas
Probably the matrix is extended somewhere after that, e.g. by a resize operation or may be it is automatically resized upon filling with data. Show us more code. - Hristo Iliev
Could you post a link to your example? - Andrey Kamaev
code added, thank you for your responses so far. - ipunished

1 Answers

2
votes

It makes sense now that you've posted the code. This 0-row vector is initialized to have 0 rows, but it is created incrementally.

The 0-row matrix gets passed to extractBOWDescriptor(), which itself computes several descriptors and uses cv::Mat.push_back() to add rows to the matrix.

It begins with 0 rows because at the start we have no descriptors to populate the matrix.