Ask Your Question
0

Using pictures as input to ML module?

asked 2020-08-10 17:17:36 -0600

CitationNotNeeded gravatar image

I'd like to compare the performance of all the various machine learning algorithms on a visual classification problem. I have my own training and test images but I'm not sure how to pass them to the ML algorithms. Some examples I have seen just use imported xml files. How could pictures possibly be converted to xml? Should I somehow convert my jpg images to xml? Or am I supposed to make an xml of features (such as grayscale values)? I came across this Member Function Documentation and it seems that images must be passed as arrays. What should be the format of these arrays? According to this the arrays can be mat or vectors but since my training data will have multiple images, there will be multiple mats/vectors so does that mean I pass an array of mats (or an array of vectors)? Do I make such an array from mats that are concatenated horizontally or vertically? What if I want to pass HSV values for the machine learning algorithms? Every sample of training data would have three dimensions then so what would the format of the input array be then?

If there's a guide, example or a tutorial I should look at, please let me know.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
0

answered 2020-08-11 02:03:40 -0600

berak gravatar image

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);
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2020-08-10 17:17:36 -0600

Seen: 573 times

Last updated: Aug 11 '20