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(name, 0); // load grayscale
if (img.empty()) return 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);
how does your folder structure look like ? how many classes are there ?
is that "your own data" ? or a "well known dataset" (like mnist, for which actual receipes might exist) ?
It might alsof be useful to know what you are actually using as classifier...
I am using SVM , I have tree classes which are child , adult and senior . Each class has a dedicated folder filled with images