Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

it seems that images must be passed as arrays. What should be the format of these arrays?

for most opencv ml algos you need the traindata as a single Mat, which each sample on a row, and a labels Mat with a single column, and one classid per sample/row. (exception: ANN_MLP, which needs "one-hot-encoded" labels)

Do I make such an array from mats that are concatenated horizontally or vertically?

yes, exactly ;)

you could do it like this:

//1. start with an empty Mat
Mat data, labels; 

//2. for each train image:
Mat m =  imread("something")
// process it, e.g. convert to HSV, resize, etc

//3. convert to float
m = m.convertTo(m, CV_32F);

//4. flatten and add to array:
data.push_back(m.reshape(1,1)); // 1d single row
labels.push_back(some_id); // SVM wants integers here, all others float

//5. there we are ;)
Ptr<ml::TrainData> td = ml::TrainData::create(data, ml::ROW _SAMPLE, labels);