我正在尝试制作一个K最近邻分类器,以根据其色调颜色值识别对象。我有两个CSV文件填充了两个单独对象的这些颜色值,每个对象包含大约100个样本。它们都被归一化为包含40个整数,用于描述每行该对象的平均颜色。我使用K值10。
我的分类器总是返回标签“1”,即使我在我准备的训练数据相同的数据上使用它。当我查找函数find_nearest
返回的最近点时,无论输入数据如何,我总能看到相同的10个点:1,1,1,1,1,1,1,1,1,1 。无论是输入数据还是学习数据,似乎都不对,但当我检查CvMat*
个对象的内容时,它们就是应有的。
任何人都可以在下面的代码中看到我做错了什么吗?我通常使用c ++ API,但我发现找到k最近邻居的例子比较困难。我已在openCV和此blog
检查了示例int dataArray[amountCategories_][amountSamples_][dataBandWidth_];
//Fill dataArray with data from csv files here (tested this and looks fine)
CvMat* trainData = cvCreateMat(amountCategories_ * amountSamples_, dataBandWidth_, CV_32FC1);
CvMat* trainClasses = cvCreateMat(amountCategories_ * amountSamples_, 1, CV_32FC1);
for (int categories = 0; categories < 2; categories++) {
for (int samples = 0; samples < amountSamples_; samples++) {
trainClasses->data.fl[samples] = categories;
cout << samples << ": ";
for (int datas = 0; datas < dataBandWidth_; datas++) {
trainData->data.fl[samples * dataBandWidth_ + datas] = (float) dataArray[categories][samples][datas];
cout << trainData->data.fl[samples * dataBandWidth_ + datas] << ", ";
}
cout << " and is type " << trainClasses->data.fl[samples] << endl;
}
}
knn_.train(trainData, trainClasses);
识别对象:
vector<int> lineColors;
//fill lineColors with integers of the hue value
CvMat *playerRow = cvCreateMat(1, 40, CV_32FC1);
CvMat* nearests = cvCreateMat(1, kValue_, CV_32FC1);
cout << "Input vals: ";
for (int c = 0; c < dataBandWidth_; c++) {
playerRow->data.fl[c] = (float) lineColors.at(c);
cout << playerRow->data.fl[c] << ", ";
}
cout << " " << endl;
float outcome = knn_.find_nearest(playerRow, kValue_, 0, 0, nearests, 0);
cout << "Type: " << outcome << endl;